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
use darling::{ast::NestedMeta, FromMeta};
use serde::{Deserialize, Serialize};

/// Wrapper for Vec<String> to implements FromMeta
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VecStringWrapper {
  pub v: Vec<String>,
  pub c: Vec<char>, // also stores the first char of each string
}

impl From<Vec<String>> for VecStringWrapper {
  fn from(v: Vec<String>) -> Self {
    VecStringWrapper {
      c: v
        .iter()
        .map(|s| s.chars().next().unwrap())
        .collect::<Vec<char>>(),
      v,
    }
  }
}
impl From<VecStringWrapper> for Vec<String> {
  fn from(val: VecStringWrapper) -> Self {
    val.v
  }
}

impl FromMeta for VecStringWrapper {
  fn from_nested_meta(item: &NestedMeta) -> darling::Result<Self> {
    (match *item {
      NestedMeta::Lit(ref lit) => Self::from_value(lit),
      NestedMeta::Meta(ref mi) => Self::from_meta(mi),
    })
    .map_err(|e| e.with_span(item))
  }

  fn from_meta(item: &syn::Meta) -> darling::Result<Self> {
    (match *item {
      syn::Meta::Path(_) => Self::from_word(),
      syn::Meta::List(ref value) => {
        Self::from_list(&NestedMeta::parse_meta_list(value.tokens.clone())?[..])
      }
      syn::Meta::NameValue(ref value) => Self::from_expr(&value.value),
    })
    .map_err(|e| e.with_span(item))
  }

  fn from_none() -> Option<Self> {
    None
  }

  fn from_word() -> darling::Result<Self> {
    Err(darling::Error::unsupported_format("word"))
  }

  fn from_list(_items: &[NestedMeta]) -> darling::Result<Self> {
    Err(darling::Error::unsupported_format("list"))
  }

  fn from_value(value: &syn::Lit) -> darling::Result<Self> {
    (match *value {
      syn::Lit::Bool(ref b) => Self::from_bool(b.value),
      syn::Lit::Str(ref s) => Self::from_string(&s.value()),
      syn::Lit::Char(ref ch) => Self::from_char(ch.value()),
      _ => Err(darling::Error::unexpected_lit_type(value)),
    })
    .map_err(|e| e.with_span(value))
  }

  fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
    match *expr {
      syn::Expr::Lit(ref lit) => Self::from_value(&lit.lit),
      syn::Expr::Group(ref group) => Self::from_expr(&group.expr),
      _ => Err(darling::Error::unexpected_expr_type(expr)),
    }
    .map_err(|e| e.with_span(expr))
  }

  fn from_char(_value: char) -> darling::Result<Self> {
    Err(darling::Error::unexpected_type("char"))
  }

  fn from_string(value: &str) -> darling::Result<Self> {
    Ok(VecStringWrapper {
      v: value
        .split(',')
        .map(|s| s.to_string())
        .collect::<Vec<String>>(),
      c: value
        .split(',')
        .map(|s| s.chars().next().unwrap_or_default())
        .collect::<Vec<char>>(),
    })
  }

  fn from_bool(_value: bool) -> darling::Result<Self> {
    Err(darling::Error::unexpected_type("bool"))
  }
}