1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
mod code_gen;
pub mod error;
mod model_parse;
mod model_types;
pub mod packetrs_read;
mod syn_helpers;
use code_gen::generate_enum;
use model_parse::parse_enum;
use proc_macro2::TokenStream;
use syn::DeriveInput;
pub use ::anyhow::*;
pub use ::bit_cursor::*;
pub use self::ux;
use crate::{code_gen::generate_struct, model_parse::parse_struct};
#[doc(hidden)]
pub fn derive_packetrs_read(item: TokenStream) -> std::result::Result<TokenStream, syn::Error> {
let ast: DeriveInput = syn::parse2(item)?;
match ast.data {
syn::Data::Struct(ref s) => {
let parsed = parse_struct(&ast.ident, &ast.attrs, s);
Ok(generate_struct(&parsed)).map_err(|e: anyhow::Error| syn::Error::new_spanned(ast, e))
}
syn::Data::Enum(ref e) => {
let parsed = parse_enum(&ast.ident, &ast.attrs, e);
Ok(generate_enum(&parsed)).map_err(|e: anyhow::Error| syn::Error::new_spanned(ast, e))
}
_ => Err(syn::Error::new_spanned(
ast,
"Packetrs is only supported on structs and enums",
)),
}
}