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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Derive crate for ATAT
//!
//! This crate provides derive macros for automatically deriving
//! [`atat::AtatCmd`], [`atat::AtatResp`], [`atat::AtatUrc`], [`atat::AtatEnum`]
//! and [`atat::AtatLen`]
//!
//! [`atat::AtatCmd`]: ../atat/trait.AtatCmd.html
//! [`atat::AtatResp`]: ../atat/trait.AtatResp.html
//! [`atat::AtatUrc`]: ../atat/trait.AtatUrc.html
//! [`atat::AtatEnum`]: ../atat/trait.AtatEnum.html
//! [`atat::AtatLen`]: ../atat/derive/trait.AtatLen.html
//!
//! # Examples
//!
//! ### `AtatCmd`
//! See [`AtatCmd`] for descriptions and documentation on required and allowed
//! attributes
//!
//! [`AtatCmd`]: derive.AtatCmd.html
//!
//! ```ignore
//! // Serializing the following struct, results in `AT+USORD=<socket>,<length>\r`
//! #[derive(AtatCmd)]
//! #[at_cmd("+USORD", SocketData)]
//! pub struct ReadSocketData {
//! #[at_arg(position = 0)]
//! pub socket: u8,
//! #[at_arg(position = 1)]
//! pub length: usize,
//! }
//! ```
// #![deny(warnings)]
extern crate proc_macro;
extern crate proc_macro2;
use crateTokenStream;
/// Automatically derive [`atat::AtatResp`] trait
///
/// [`atat::AtatResp`]: ../atat/trait.AtatResp.html
/// Automatically derive [`atat::AtatUrc`] trait
///
/// [`atat::AtatUrc`]: ../atat/trait.AtatUrc.html
///
/// ### Field attribute (`#[at_urc(..)]`)
/// The `AtatUrc` derive macro comes with a required field attribute
/// `#[at_urc(..)]`, that is used to specify the URC token to match for.
///
/// The first argument is required, and must be either a string or a byte
/// literal, specifying the URC token to match for.
///
/// Allowed optionals for `at_urc` are:
/// - `parse`: **function** Function that should be used to parse for the URC
/// instead of using default `atat::digest::parser::urc_helper` function. The
/// passed functions need to have a valid `nom` signature `(arguments) -> impl Fn(Input) -> IResult<Input, Output, Error>`.
/// Automatically derive [`atat::AtatEnum`] trait
///
/// [`atat::AtatEnum`]: ../atat/trait.AtatEnum.html
/// [`atat::AtatLen`]: ../atat/trait.AtatLen.html
///
/// This trait implementation is equivalent to using
/// [`serde_repr`](https://docs.rs/serde_repr/0.1.5/serde_repr/), thus removing
/// the need for this package in the Atat context.
///
/// Furthermore it automatically implements [`atat::AtatLen`], based on the data
/// type given in the container attribute.
///
/// **NOTE**: When using this derive macro with struct or tuple variants in the
/// enum, one should take extra care to avoid large size variations of the
/// variants, as the resulting `AtatLen` of the enum will be the length of the
/// representation (see `#[at_enum(..)]`) together with the largest sum of field
/// values in the variant.
///
/// Eg.
/// ```ignore
/// use heapless::String;
///
/// #[derive(AtatEnum)]
/// pub enum LargeSizeVariations {
/// #[at_arg(value = 0)]
/// VariantOne,
/// #[at_arg(value = 1)]
/// VariantTwo(u8),
/// #[at_arg(value = 2)]
/// VariantThree(String<1024>)
/// #[at_arg(value = 2)]
/// VariantFour(String<10>, String<10>, String<10>)
/// }
/// ```
/// will result in `<LargeSizeVariations as AtatLen>::LEN == 1026`, even for
/// `LargeSizeVariations::VariantOne`
///
/// ### Container attribute (`#[at_enum(..)]`)
/// The `AtatEnum` derive macro comes with an option of annotating the struct
/// with a container attribute `#[at_enum(..)]`.
///
/// The container attribute only allows specifying a single parameter, that is
/// non-optional if the container attribute is present. The parameter allows
/// describing the underlying data type of the enum, and thus the maximum
/// allowed value of the fields. Only integer types are allowed (`u8`, `u16`,
/// `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`, `usize`, `isize`).
/// Eg. `#[at_enum(u16)]`.
///
/// **Note**: `at_enum` defaults to `u8`
///
/// ### Field attribute (`#[at_arg(..)]`)
/// The `AtatEnum` derive macro comes with an optional field attribute
/// `#[at_arg(..)]`, that can be specified for some or all of the fields.
///
/// Allowed options for `at_arg` are:
/// - `value`: **integer** The value of the serialized field
/// Automatically derive [`atat::AtatCmd`] trait
///
/// [`atat::AtatCmd`]: ../atat/trait.AtatCmd.html
///
///
/// ### Container attribute (`#[at_cmd(..)]`)
/// The `AtatCmd` derive macro comes with a requirement of annotating the struct
/// with a container attribute `#[at_cmd(..)]`.
///
/// This container attribute requires specifying at least a command and an
/// expected response struct as: `#[at_cmd("+USORD", SocketData)]` where
/// `SocketData` is any type implementing `AtatResp`.
///
/// Furthermore the container attribute allows specifying some additional
/// options to tweak the command. All optional attributes takes the form `<key>
/// = <value>`, eg. `#[at_cmd("+USORD", SocketData, timeout_ms = 10000)]`
///
/// Allowed options are:
/// - `timeout_ms`: **integer** The maximum timeout in milliseconds of the
/// command
/// - `abortable`: **bool** Whether or not the command can be aborted
/// - `value_sep`: **bool** Disable the seperator between the command and any
/// parameters (default true). Useful to create "fixed" commands, eg.
/// `#[at_cmd("+UDCONF=1", NoResponse, value_sep = false)]`.
/// - `cmd_prefix`: **string** Overwrite the prefix of the command (default
/// 'AT'). Can also be set to '' (empty).
/// - `termination`: **string** Overwrite the line termination of the command
/// (default '\r'). Can also be set to '' (empty).
/// - `quote_escape_strings`: **bool** Whether to escape strings in commands
/// (default true).
/// - `parse`: **function** Function that should be used to parse the response
/// instead of using default `atat::serde_at::from_slice` function. The
/// passed functions needs to have a signature `Result<Response, E>` where
/// `Response` is the type of the response passed in the `at_cmd`
///
/// ### Field attribute (`#[at_arg(..)]`)
/// The `AtatCmd` derive macro comes with an optional field attribute
/// `#[at_arg(..)]`, that can be specified on some or all of the fields.
///
/// Allowed options for `at_arg` are:
/// - position: **integer** The index of the field in the resulting command
/// string. (eg. for command `AT+CMD=a,b`, field `a` would have `position = 1`
/// and field `b` would have `position = 2`) (defaults to order of the fields
/// in the struct)
/// Automatically derive [`atat::AtatLen`] trait
///
/// [`atat::AtatLen`]: ../atat/derive/trait.AtatLen.html
///
/// This requires all of the fields to also implement [`atat::AtatLen`]