forked from locka99/opcua
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
291 changed files
with
483 additions
and
11,367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
|
||
use crate::utils::{EmptyAttribute, EncodingFieldAttribute, StructItem}; | ||
|
||
pub type BinaryStruct = StructItem<EncodingFieldAttribute, EmptyAttribute>; | ||
|
||
pub fn parse_binary_struct(input: DeriveInput) -> syn::Result<BinaryStruct> { | ||
BinaryStruct::from_input(input) | ||
} | ||
|
||
pub fn generate_binary_encode_impl(strct: BinaryStruct) -> syn::Result<TokenStream> { | ||
let mut byte_len_body = quote! {}; | ||
let mut encode_body = quote! {}; | ||
|
||
for field in strct.fields { | ||
if field.attr.ignore { | ||
continue; | ||
} | ||
|
||
let ident = field.ident; | ||
byte_len_body.extend(quote! { | ||
size += self.#ident.byte_len(ctx); | ||
}); | ||
encode_body.extend(quote! { | ||
size += self.#ident.encode(stream, ctx)?; | ||
}); | ||
} | ||
let ident = strct.ident; | ||
|
||
Ok(quote! { | ||
impl opcua::types::BinaryEncodable for #ident { | ||
#[allow(unused)] | ||
fn byte_len(&self, ctx: &opcua::types::Context<'_>) -> usize { | ||
let mut size = 0usize; | ||
#byte_len_body | ||
size | ||
} | ||
#[allow(unused)] | ||
fn encode<S: std::io::Write + ?Sized>( | ||
&self, | ||
stream: &mut S, | ||
ctx: &opcua::types::Context<'_>, | ||
) -> opcua::types::EncodingResult<usize> { | ||
let mut size = 0usize; | ||
#encode_body | ||
Ok(size) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
pub fn generate_binary_decode_impl(strct: BinaryStruct) -> syn::Result<TokenStream> { | ||
let mut decode_impl = quote! {}; | ||
let mut decode_build = quote! {}; | ||
|
||
let mut has_context = false; | ||
for field in strct.fields { | ||
if field.attr.ignore { | ||
continue; | ||
} | ||
|
||
let ident = field.ident; | ||
let ident_string = ident.to_string(); | ||
if ident_string == "request_header" { | ||
decode_impl.extend(quote! { | ||
let request_header: opcua::types::RequestHeader = opcua::types::BinaryDecodable::decode(stream, ctx)?; | ||
let __request_handle = request_header.request_handle; | ||
}); | ||
decode_build.extend(quote! { | ||
request_header, | ||
}); | ||
has_context = true; | ||
} else if ident_string == "response_header" { | ||
decode_impl.extend(quote! { | ||
let response_header: opcua::types::ResponseHeader = opcua::types::BinaryDecodable::decode(stream, ctx)?; | ||
let __request_handle = response_header.request_handle; | ||
}); | ||
decode_build.extend(quote! { | ||
response_header, | ||
}); | ||
has_context = true; | ||
} else if has_context { | ||
decode_build.extend(quote! { | ||
#ident: opcua::types::BinaryDecodable::decode(stream, ctx) | ||
.map_err(|e| e.with_request_handle(__request_handle))?, | ||
}); | ||
} else { | ||
decode_build.extend(quote! { | ||
#ident: opcua::types::BinaryDecodable::decode(stream, ctx)?, | ||
}); | ||
} | ||
} | ||
|
||
let ident = strct.ident; | ||
|
||
Ok(quote! { | ||
impl opcua::types::BinaryDecodable for #ident { | ||
#[allow(unused_variables)] | ||
fn decode<S: std::io::Read + ?Sized>(stream: &mut S, ctx: &opcua::types::Context<'_>) -> opcua::types::EncodingResult<Self> { | ||
#decode_impl | ||
Ok(Self { | ||
#decode_build | ||
}) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use gen::{generate_binary_decode_impl, generate_binary_encode_impl, parse_binary_struct}; | ||
use proc_macro2::TokenStream; | ||
use syn::DeriveInput; | ||
|
||
mod gen; | ||
pub fn derive_binary_encode_inner(input: DeriveInput) -> syn::Result<TokenStream> { | ||
let struct_data = parse_binary_struct(input)?; | ||
generate_binary_encode_impl(struct_data) | ||
} | ||
|
||
pub fn derive_binary_decode_inner(input: DeriveInput) -> syn::Result<TokenStream> { | ||
let struct_data = parse_binary_struct(input)?; | ||
generate_binary_decode_impl(struct_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.