atat_derive 0.24.1

Derive macro for atat
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use proc_macro2::Span;
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::{
    Attribute, Data, DataEnum, DataStruct, DeriveInput, Expr, ExprLit, ExprPath, Fields, Generics,
    Ident, Lit, LitByteStr, Path, Type,
};

#[derive(Clone)]
pub struct ParseInput {
    pub ident: Ident,
    pub generics: Generics,
    pub at_cmd: Option<CmdAttributes>,
    pub at_enum: Option<EnumAttributes>,
    pub variants: Vec<Variant>,
}

/// Parsed attributes of `#[at_cmd(..)]`
#[derive(Clone)]
pub struct CmdAttributes {
    pub cmd: String,
    pub resp: Path,
    pub parse: Option<Path>,
    pub timeout_ms: Option<u32>,
    pub attempts: Option<u8>,
    pub abortable: Option<bool>,
    pub reattempt_on_parse_err: Option<bool>,
    pub response_code: Option<bool>,
    pub value_sep: bool,
    pub cmd_prefix: String,
    pub termination: String,
    pub quote_escape_strings: bool,
}
/// Parsed attributes of `#[at_arg(..)]`
#[derive(Clone)]
pub struct ArgAttributes {
    pub value: Option<i64>,
    pub position: Option<usize>,
    pub len: Option<usize>,
    pub default: bool,
}

/// Parsed attributes of `#[at_urc(..)]`
#[derive(Clone)]
pub struct UrcAttributes {
    pub code: LitByteStr,
    pub parse: Option<Path>,
}

/// Parsed attributes of `#[at_enum(..)]`
#[derive(Clone)]
pub struct EnumAttributes {
    pub repr: Ident,
}

/// Parsed field level attributes
#[derive(Clone)]
pub struct FieldAttributes {
    pub at_urc: Option<UrcAttributes>,
    pub at_arg: Option<ArgAttributes>,
}

#[derive(Clone)]
pub struct Variant {
    /// Ident will be set on named variants, and None on unnamed variants
    pub ident: Option<Ident>,
    /// Type of a struct variant
    pub ty: Option<Type>,
    /// Fields of an enum variant
    pub fields: Option<Fields>,
    /// Parsed contents on `#[at_arg(..)]` and `#[at_urc(..)]`
    pub attrs: FieldAttributes,
}

/// Parse valid field attributes
pub fn parse_field_attr(attributes: &[Attribute]) -> Result<FieldAttributes> {
    let mut attrs = FieldAttributes {
        at_urc: None,
        at_arg: None,
    };
    for attr in attributes {
        if attr.path().is_ident("at_arg") {
            attrs.at_arg = Some(attr.parse_args()?);
        } else if attr.path().is_ident("at_urc") {
            attrs.at_urc = Some(attr.parse_args()?);
        }
    }
    Ok(attrs)
}

fn sorted_variants(data: Data) -> Result<Vec<Variant>> {
    let mut variants = match data {
        Data::Struct(DataStruct { fields, .. }) => {
            let unwrapped_fields = match fields {
                Fields::Named(fields) => fields.named.iter().cloned().collect(),
                Fields::Unnamed(fields) => fields.unnamed.iter().cloned().collect(),
                Fields::Unit => Vec::new(),
            };

            unwrapped_fields
                .into_iter()
                .enumerate()
                .map(|(i, f)| {
                    Ok((
                        i,
                        Variant {
                            ident: f.ident,
                            ty: Some(f.ty),
                            fields: None,
                            attrs: parse_field_attr(&f.attrs)?,
                        },
                    ))
                })
                .collect::<Result<Vec<(usize, Variant)>>>()?
        }
        Data::Enum(DataEnum { variants, .. }) => variants
            .into_iter()
            .enumerate()
            .map(|(i, v)| {
                Ok((
                    i,
                    Variant {
                        ident: Some(v.ident.clone()),
                        ty: None,
                        fields: Some(v.fields.clone()),
                        attrs: parse_field_attr(&v.attrs)?,
                    },
                ))
            })
            .collect::<Result<Vec<(usize, Variant)>>>()?,
        Data::Union(_) => {
            return Err(Error::new(Span::call_site(), "union types are unsupported"));
        }
    };

    variants.sort_by(|(ai, a), (bi, b)| {
        let ap = if let Some(ArgAttributes {
            position: Some(p), ..
        }) = a.attrs.at_arg
        {
            p
        } else {
            *ai
        };

        let bp = if let Some(ArgAttributes {
            position: Some(p), ..
        }) = b.attrs.at_arg
        {
            p
        } else {
            *bi
        };

        ap.cmp(&bp)
    });

    Ok(variants.into_iter().map(|t| t.1).collect())
}

impl Parse for ArgAttributes {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut attrs = Self {
            value: None,
            position: None,
            len: None,
            default: false,
        };

        while {
            match input.parse::<syn::Meta>()? {
                syn::Meta::NameValue(name_value) if name_value.path.is_ident("value") => {
                    match name_value.value.clone() {
                        Expr::Lit(ExprLit {
                            lit: Lit::Int(v), ..
                        }) => attrs.value = Some(v.base10_parse().unwrap()),
                        _ => {
                            return Err(Error::new(
                                Span::call_site(),
                                "value argument must be an integer",
                            ))
                        }
                    }
                }
                syn::Meta::NameValue(name_value) if name_value.path.is_ident("position") => {
                    match name_value.value.clone() {
                        Expr::Lit(ExprLit {
                            lit: Lit::Int(v), ..
                        }) => attrs.position = Some(v.base10_parse().unwrap()),
                        _ => {
                            return Err(Error::new(
                                Span::call_site(),
                                "position argument must be a positive integer",
                            ))
                        }
                    }
                }
                syn::Meta::NameValue(name_value) if name_value.path.is_ident("len") => {
                    match name_value.value.clone() {
                        Expr::Lit(ExprLit {
                            lit: Lit::Int(v), ..
                        }) => attrs.len = Some(v.base10_parse().unwrap()),
                        _ => {
                            return Err(Error::new(
                                Span::call_site(),
                                "len argument must be a positive integer",
                            ))
                        }
                    }
                }
                syn::Meta::NameValue(name_value) if name_value.path.is_ident("default") => {
                    return Err(Error::new(
                        Span::call_site(),
                        "default does not have a value. Eg #[at_arg(default)]",
                    ))
                }
                syn::Meta::Path(path) if path.is_ident("default") => {
                    attrs.default = true;
                }
                _ => return Err(Error::new(Span::call_site(), "unknown argument!")),
            }

            input.parse::<syn::token::Comma>().is_ok()
        } {}

        Ok(attrs)
    }
}

impl Parse for UrcAttributes {
    fn parse(input: ParseStream) -> Result<Self> {
        let code = match input.parse::<syn::Lit>() {
            Ok(Lit::ByteStr(b)) => b,
            Ok(Lit::Str(s)) => LitByteStr::new(s.value().as_bytes(), input.span()),
            _ => {
                return Err(Error::new(
                    input.span(),
                    "expected string value for `at_urc`",
                ))
            }
        };

        let mut at_urc = Self { code, parse: None };

        while input.parse::<syn::token::Comma>().is_ok() {
            let optional = input.parse::<syn::MetaNameValue>()?;
            if optional.path.is_ident("parse") {
                match optional.value {
                    Expr::Path(ExprPath { path, .. }) => {
                        at_urc.parse = Some(path);
                    }
                    _ => return Err(Error::new(input.span(), "expected function for 'parse'")),
                }
            }
        }

        Ok(at_urc)
    }
}

impl Parse for CmdAttributes {
    fn parse(input: ParseStream) -> Result<Self> {
        let cmd = input.parse::<syn::LitStr>()?;
        let _comma = input.parse::<syn::token::Comma>()?;
        let response_ident = input.parse::<Path>()?;

        let mut at_cmd = Self {
            cmd: cmd.value(),
            resp: response_ident,
            parse: None,
            timeout_ms: None,
            attempts: None,
            abortable: None,
            reattempt_on_parse_err: None,
            response_code: None,
            value_sep: true,
            cmd_prefix: String::from("AT"),
            termination: String::from("\r"),
            quote_escape_strings: true,
        };

        while input.parse::<syn::token::Comma>().is_ok() {
            let optional = input.parse::<syn::MetaNameValue>()?;
            if optional.path.is_ident("timeout_ms") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Int(v), ..
                    }) => {
                        at_cmd.timeout_ms = Some(v.base10_parse().unwrap());
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected integer value for 'timeout_ms'",
                        ))
                    }
                }
            } else if optional.path.is_ident("attempts") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Int(v), ..
                    }) => {
                        at_cmd.attempts = Some(v.base10_parse().unwrap());
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected integer value for 'attempts'",
                        ))
                    }
                }
            } else if optional.path.is_ident("parse") {
                match optional.value {
                    Expr::Path(ExprPath { path, .. }) => {
                        at_cmd.parse = Some(path);
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected function for 'parse'",
                        ))
                    }
                }
            } else if optional.path.is_ident("reattempt_on_parse_err") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Bool(v), ..
                    }) => {
                        at_cmd.reattempt_on_parse_err = Some(v.value);
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected bool value for 'reattempt_on_parse_err'",
                        ))
                    }
                }
            } else if optional.path.is_ident("abortable") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Bool(v), ..
                    }) => {
                        at_cmd.abortable = Some(v.value);
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected bool value for 'abortable'",
                        ))
                    }
                }
            } else if optional.path.is_ident("value_sep") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Bool(v), ..
                    }) => {
                        at_cmd.value_sep = v.value;
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected bool value for 'value_sep'",
                        ))
                    }
                }
            } else if optional.path.is_ident("cmd_prefix") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Str(v), ..
                    }) => {
                        at_cmd.cmd_prefix = v.value();
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected string value for 'cmd_prefix'",
                        ))
                    }
                }
            } else if optional.path.is_ident("termination") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Str(v), ..
                    }) => {
                        at_cmd.termination = v.value();
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected string value for 'termination'",
                        ))
                    }
                }
            } else if optional.path.is_ident("quote_escape_strings") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Bool(v), ..
                    }) => {
                        at_cmd.quote_escape_strings = v.value;
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected bool value for 'quote_escape_strings'",
                        ))
                    }
                }
            } else if optional.path.is_ident("response_code") {
                match optional.value {
                    Expr::Lit(ExprLit {
                        lit: Lit::Bool(v), ..
                    }) => {
                        at_cmd.response_code = Some(v.value);
                    }
                    _ => {
                        return Err(Error::new(
                            Span::call_site(),
                            "expected bool value for 'response_code'",
                        ))
                    }
                }
            }
        }

        Ok(at_cmd)
    }
}

impl Parse for ParseInput {
    fn parse(input: ParseStream) -> Result<Self> {
        let derive_input = DeriveInput::parse(input)?;

        let mut at_cmd = None;
        let mut at_enum = None;

        // Parse valid container attributes
        for attr in derive_input.attrs {
            if attr.path().is_ident("at_cmd") {
                at_cmd = Some(attr.parse_args()?);
            } else if attr.path().is_ident("at_enum") {
                at_enum = Some(EnumAttributes {
                    repr: attr.parse_args()?,
                });
            }
        }

        Ok(Self {
            ident: derive_input.ident,
            generics: derive_input.generics,
            at_cmd,
            at_enum,
            variants: sorted_variants(derive_input.data)?,
        })
    }
}