-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathargs.rs
More file actions
218 lines (191 loc) · 5.66 KB
/
Copy pathargs.rs
File metadata and controls
218 lines (191 loc) · 5.66 KB
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
use crate::utils::to_type_string;
use darling::ast::NestedMeta;
use darling::{FromAttributes, FromDeriveInput, FromMeta};
use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use std::ops::Deref;
use syn::{Expr, Ident};
#[derive(Clone, Copy, Debug)]
pub enum SerdeIoDirection {
From, // read / deserialize
To, // write / serialize
}
#[derive(Clone, Debug)]
pub enum SerdeTagFormat {
Untagged,
External,
Internal(String),
Adjacent(String, String),
// Special case for unit only enums
Unit,
}
// #[serde(rename = "name")]
// #[serde(rename(deserialize = "de_name", serialize = "ser_name"))]
#[derive(Debug, Default, PartialEq)]
pub struct SerdeRenameArg {
pub deserialize: Option<String>,
pub serialize: Option<String>,
}
impl FromMeta for SerdeRenameArg {
fn from_string(value: &str) -> darling::Result<Self> {
Ok(Self {
deserialize: Some(value.into()),
serialize: Some(value.into()),
})
}
fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
#[derive(Default, FromMeta)]
#[darling(default)]
struct Rename {
deserialize: Option<String>,
serialize: Option<String>,
}
impl From<Rename> for SerdeRenameArg {
fn from(value: Rename) -> Self {
Self {
deserialize: value.deserialize,
serialize: value.serialize,
}
}
}
Rename::from_list(items).map(SerdeRenameArg::from)
}
}
impl SerdeRenameArg {
pub fn get_name(&self, dir: SerdeIoDirection) -> Option<&str> {
match dir {
SerdeIoDirection::From => self.deserialize.as_deref(),
SerdeIoDirection::To => self.serialize.as_deref(),
}
}
pub fn get_meta(&self, key: &str) -> TokenStream {
match (self.deserialize.as_deref(), self.serialize.as_deref()) {
(Some(de), Some(ser)) => {
if de == ser {
quote! { #key = #de }
} else {
quote! { #key(deserialize = #de, serialize = #ser) }
}
}
(None, Some(ser)) => quote! { #key(serialize = #ser) },
(Some(de), None) => quote! { #key(deserialize = #de) },
_ => quote! {},
}
}
}
// #[serde()]
#[derive(Debug, Default, FromDeriveInput)]
#[darling(default, allow_unknown_fields, attributes(serde))]
pub struct SerdeContainerArgs {
pub default: bool,
pub deny_unknown_fields: bool,
// struct
pub rename: Option<SerdeRenameArg>,
pub rename_all: Option<SerdeRenameArg>,
pub rename_all_fields: Option<SerdeRenameArg>,
// enum
pub content: Option<String>,
pub expecting: Option<String>,
pub tag: Option<String>,
pub untagged: bool,
}
// #[serde()]
#[derive(Debug, Default, FromAttributes)]
#[darling(default, allow_unknown_fields, attributes(serde))]
pub struct SerdeFieldArgs {
#[darling(multiple)]
pub alias: Vec<String>,
pub default: bool,
pub flatten: bool,
pub rename: Option<SerdeRenameArg>,
pub skip: bool,
pub skip_deserializing: bool,
pub skip_deserializing_if: Option<String>,
pub skip_serializing: bool,
pub skip_serializing_if: Option<String>,
// variant
pub other: bool,
pub untagged: bool,
}
// #[setting(partial)]
#[derive(Debug, Default)]
pub struct PartialArg {
meta: Vec<NestedMeta>,
}
impl ToTokens for PartialArg {
fn to_tokens(&self, tokens: &mut TokenStream) {
let attrs: Vec<_> = self.meta.iter().map(|m| m.to_token_stream()).collect();
if !attrs.is_empty() {
tokens.extend(quote! {#[#(#attrs),*]});
}
}
}
impl FromMeta for PartialArg {
fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
Ok(Self {
meta: items.to_vec(),
})
}
}
// #[setting(nested)]
#[derive(Debug)]
pub enum NestedArg {
Detect(bool),
Ident(Ident),
}
impl NestedArg {
pub fn is_nested(&self) -> bool {
match self {
NestedArg::Detect(inner) => *inner,
NestedArg::Ident(_) => true,
}
}
}
impl FromMeta for NestedArg {
// #[setting(nested)]
fn from_word() -> darling::Result<Self> {
Ok(Self::Detect(true))
}
// #[setting(nested = true)]
fn from_bool(value: bool) -> darling::Result<Self> {
Ok(Self::Detect(value))
}
// #[setting(nested = NestedConfig)]
fn from_expr(expr: &Expr) -> darling::Result<Self> {
match expr {
Expr::Lit(lit) => Self::from_value(&lit.lit),
Expr::Path(path) => {
if path.path.segments.len() > 1 {
Err(darling::Error::custom(format!(
"Too many segments for `{}`, only a single identifier is allowed.",
to_type_string(path.to_token_stream())
)))
} else {
Ok(Self::Ident(
path.path.segments.last().unwrap().ident.to_owned(),
))
}
}
_ => Err(darling::Error::unexpected_expr_type(expr)),
}
.map_err(|e| e.with_span(expr))
}
}
// #[setting(validate)]
#[derive(Debug)]
pub struct ValidateArg(Expr);
impl FromMeta for ValidateArg {
fn from_expr(expr: &Expr) -> darling::Result<Self> {
match expr {
Expr::Call(_) | Expr::Path(_) => Ok(Self(expr.to_owned())),
_ => Err(darling::Error::unexpected_expr_type(expr)),
}
.map_err(|e| e.with_span(expr))
}
}
impl Deref for ValidateArg {
type Target = Expr;
fn deref(&self) -> &Self::Target {
&self.0
}
}