crud_api_endpoint/
types.rs1use darling::{ast::NestedMeta, FromMeta};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct VecStringWrapper {
7 pub v: Vec<String>,
8 pub c: Vec<char>, }
10
11impl From<Vec<String>> for VecStringWrapper {
12 fn from(v: Vec<String>) -> Self {
13 VecStringWrapper {
14 c: v
15 .iter()
16 .map(|s| s.chars().next().unwrap())
17 .collect::<Vec<char>>(),
18 v,
19 }
20 }
21}
22impl From<VecStringWrapper> for Vec<String> {
23 fn from(val: VecStringWrapper) -> Self {
24 val.v
25 }
26}
27
28impl FromMeta for VecStringWrapper {
29 fn from_nested_meta(item: &NestedMeta) -> darling::Result<Self> {
30 (match *item {
31 NestedMeta::Lit(ref lit) => Self::from_value(lit),
32 NestedMeta::Meta(ref mi) => Self::from_meta(mi),
33 })
34 .map_err(|e| e.with_span(item))
35 }
36
37 fn from_meta(item: &syn::Meta) -> darling::Result<Self> {
38 (match *item {
39 syn::Meta::Path(_) => Self::from_word(),
40 syn::Meta::List(ref value) => {
41 Self::from_list(&NestedMeta::parse_meta_list(value.tokens.clone())?[..])
42 }
43 syn::Meta::NameValue(ref value) => Self::from_expr(&value.value),
44 })
45 .map_err(|e| e.with_span(item))
46 }
47
48 fn from_none() -> Option<Self> {
49 None
50 }
51
52 fn from_word() -> darling::Result<Self> {
53 Err(darling::Error::unsupported_format("word"))
54 }
55
56 fn from_list(_items: &[NestedMeta]) -> darling::Result<Self> {
57 Err(darling::Error::unsupported_format("list"))
58 }
59
60 fn from_value(value: &syn::Lit) -> darling::Result<Self> {
61 (match *value {
62 syn::Lit::Bool(ref b) => Self::from_bool(b.value),
63 syn::Lit::Str(ref s) => Self::from_string(&s.value()),
64 syn::Lit::Char(ref ch) => Self::from_char(ch.value()),
65 _ => Err(darling::Error::unexpected_lit_type(value)),
66 })
67 .map_err(|e| e.with_span(value))
68 }
69
70 fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
71 match *expr {
72 syn::Expr::Lit(ref lit) => Self::from_value(&lit.lit),
73 syn::Expr::Group(ref group) => Self::from_expr(&group.expr),
74 _ => Err(darling::Error::unexpected_expr_type(expr)),
75 }
76 .map_err(|e| e.with_span(expr))
77 }
78
79 fn from_char(_value: char) -> darling::Result<Self> {
80 Err(darling::Error::unexpected_type("char"))
81 }
82
83 fn from_string(value: &str) -> darling::Result<Self> {
84 Ok(VecStringWrapper {
85 v: value
86 .split(',')
87 .map(|s| s.to_string())
88 .collect::<Vec<String>>(),
89 c: value
90 .split(',')
91 .map(|s| s.chars().next().unwrap_or_default())
92 .collect::<Vec<char>>(),
93 })
94 }
95
96 fn from_bool(_value: bool) -> darling::Result<Self> {
97 Err(darling::Error::unexpected_type("bool"))
98 }
99}