-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattributes.rs
135 lines (121 loc) · 5.16 KB
/
attributes.rs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::*;
///
/// This file contains a list of valid derive attributes that simple_parse supports
///
/* Enums */
#[derive(Default, Debug, PartialEq)]
#[derive(FromDeriveInput)]
#[darling(attributes(sp))]
/// Attributes that can be use on the top level enum declaration
pub(crate) struct EnumAttributes {
/// The type used to parse the variant id
#[darling(default)]
pub id_type: Option<String>,
/// Specifies the default endiannesss for the whole enum.
#[darling(default)]
pub endian: Option<String>,
}
/// Attributes that can be use on each enum variant.
#[derive(Default, Debug, PartialEq)]
#[derive(FromVariant)]
#[darling(attributes(sp))]
pub(crate) struct VariantAttributes {
/// The numerical representation of this variant.
/// When not specified, C style ids are used (First variant starts at 0, subsequent are [prev + 1])
#[darling(default)]
pub id: Option<usize>,
/// Specifies the default endiannesss for the whole Variant
#[darling(default)]
pub endian: Option<String>,
}
/* Structs */
#[derive(Default, Debug, PartialEq)]
#[derive(FromDeriveInput)]
#[darling(attributes(sp))]
/// Attributes that can be use on the top level struct declaration
pub(crate) struct StructAttributes {
/// Specifies the default endiannesss for the whole struct
#[darling(default)]
pub endian: Option<String>,
}
#[derive(Default, Debug, PartialEq)]
#[derive(FromField)]
#[darling(attributes(sp))]
/// Attributes that can be use on each field.
pub(crate) struct FieldAttributes {
/// Points to the field name/index that contains the number of items to parse the dynamically size type
/// e.g.
/// ```Rust
/// struct Test {
/// num_options: u8,
/// #[sp(len="num_options")]
/// options: Vec<Options>,
/// }
/// ```
#[darling(default)]
pub len: Option<String>,
/// Allows for custom validation code to run directly after parsing or before writing a field.
/// The provided value will be parsed as a comma seperated function name optionnaly followed by field names.
/// When reading, any reference to fields __after__ the current field will be passed as `None` as their contents have not yet been parsed.
/// When writing, any reference to fields __after__ the current field will be passed as `Some(&T)`.
///
/// This functions first parameter will always be a reference to the field it annotates.
/// For example :
/// ```Rust
/// struct MyStruct {
/// some_field: bool,
/// #[sp(validate="validate_magic, some_field")]
/// magic_value: u32
/// field_after: String,
/// }
/// ```
/// The `validate_magic` function must have a signature :
/// fn validate_magic(this: &u32, some_field: &bool, field_after: Option<&String>, ctx: &mut SpCtx) -> Result<(), SpError>
///
/// When called as part of a Read :
/// ctx.is_reading = true;
/// validate_magic(&magic_value, &some_field, None, ctx)?;
/// When called as part of a Write :
/// ctx.is_reading = false;
/// validate_magic(&self.magic_value, &self.some_field, Some(self.field_after), ctx)?;
#[darling(default)]
pub validate: Option<String>,
/// Allows for custom parsing functions to populate the annotated field.
/// The provided String will be parsed as a comma seperated function name followed by field names that have already been populated.
/// For example :
/// ```Rust
/// struct MyStruct {
/// some_field: bool,
/// #[sp(reader="custom_parser, some_field")]
/// optionnal: Option<usize>,
/// }
/// ```
/// Will end up generating code that calls a function with signature :
/// fn custom_parser(some_field: &bool, src: &mut Read, ctx: &mut SpCtx) -> Result<T, SpError>
#[darling(default)]
pub reader: Option<String>,
/// Allows for custom writing functions to convert the annotated field into bytes.
/// The provided String will be parsed as a comma seperated function name optionally followed by any field name in the struct.
/// This function's first parameter will always be a reference to the field it annotates.
/// For example :
/// ```Rust
/// struct MyStruct {
/// some_field: bool,
/// #[sp(writer="custom_writer")]
/// optionnal: Option<usize>,
/// }
/// ```
/// Will end up generating code that calls a function with signature :
/// fn custom_writer(this: &Option<usize>, ctx: &mut SpCtx, dst: &mut Write) -> Result<usize, SpError>
#[darling(default)]
pub writer: Option<String>,
/// Specifies the endiannes of the specific field. The data will
/// be converted to the native endianness when necessary.
#[darling(default)]
pub endian: Option<String>,
/// Specifies whether this field's type is variably sized
///
/// This should only be required when a custom type has a variable size.
#[darling(default)]
pub var_size: Option<()>
}