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
use darling;
use derive_builder_core::Block;
mod darling_opts;
pub use self::darling_opts::Options;
#[derive(Debug, Clone)]
pub enum DefaultExpression {
Explicit(String),
Trait,
}
impl DefaultExpression {
pub fn parse_block(&self, no_std: bool) -> Block {
let expr = match *self {
DefaultExpression::Explicit(ref s) => {
if s.is_empty() {
panic!(r#"Empty default expressions `default = ""` are not supported."#);
}
s
}
DefaultExpression::Trait => {
if no_std {
"::core::default::Default::default()"
} else {
"::std::default::Default::default()"
}
}
};
expr.parse()
.expect(&format!("Couldn't parse default expression `{:?}`", self))
}
}
impl darling::FromMeta for DefaultExpression {
fn from_word() -> darling::Result<Self> {
Ok(DefaultExpression::Trait)
}
fn from_string(value: &str) -> darling::Result<Self> {
if value.is_empty() {
Err(darling::Error::unknown_value(""))
} else {
Ok(DefaultExpression::Explicit(value.into()))
}
}
}