from_attr/
flag_or_value.rs

1use proc_macro2::Span;
2
3use crate::{flag_or_type::FlagOrType, ConvertParsed, PathValue};
4
5/// Represents the 3 cases parsed from the [`meta`](syn::meta::ParseNestedMeta).
6#[derive(Clone, Copy, Debug, Default)]
7pub enum FlagOrValue<T> {
8    /// No value.
9    #[default]
10    None,
11    /// Only the flag.
12    Flag {
13        /// The path of the meta.
14        path: Span,
15    },
16    /// The path and the value.
17    Value {
18        /// The path of the meta.
19        path: Span,
20        /// The value parsed from the meta.
21        value: T,
22    },
23}
24
25impl<T> ConvertParsed for FlagOrValue<T>
26where
27    T: ConvertParsed,
28{
29    type Type = FlagOrType<T::Type>;
30
31    fn convert(path_value: PathValue<Self::Type>) -> syn::Result<Self> {
32        let PathValue { path, value } = path_value;
33
34        match value {
35            FlagOrType::Flag => Ok(Self::Flag { path }),
36            FlagOrType::Type(value) => Ok(Self::Value {
37                path,
38                value: T::convert(PathValue { path, value })?,
39            }),
40        }
41    }
42
43    fn default() -> Option<Self> {
44        Some(Self::None)
45    }
46
47    fn flag() -> Option<Self::Type> {
48        Some(FlagOrType::Flag)
49    }
50}