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
use syn;
use derive_builder_core::{DeprecationNotes, BuilderPattern, Setter, Initializer, BuilderField,
Block, Bindings};
use options::DefaultExpression;
/// These field options define how the builder interacts with the field.
#[derive(Debug, Clone)]
pub struct FieldOptions {
/// Enables code generation for this setter.
pub setter_enabled: bool,
/// How the setter method takes and returns `self` (e.g. mutably).
pub builder_pattern: BuilderPattern,
/// The setter name.
pub setter_ident: syn::Ident,
/// Visibility of the setter, e.g. `syn::Visibility::Public`.
pub setter_visibility: syn::Visibility,
/// Visibility of the field, e.g. `syn::Visibility::Public`.
pub field_visibility: syn::Visibility,
/// Default expression for the field, e.g. `#[builder(default="42u32")]` (default to None).
pub default_expression: Option<DefaultExpression>,
/// Whether the build_method defines a default struct.
pub use_default_struct: bool,
/// The field name, may deviate from `setter_ident`.
pub field_ident: syn::Ident,
/// The field type.
pub field_type: syn::Ty,
/// Make the setter generic over `Into<_>`.
pub setter_into: bool,
/// Emit deprecation notes to the user,
/// e.g. if a deprecated attribute was used in `derive_builder`.
pub deprecation_notes: DeprecationNotes,
/// Setter attributes, e.g. `#[allow(non_snake_case)]`.
pub attrs: Vec<syn::Attribute>,
/// Bindings to libstd or libcore.
pub bindings: Bindings,
/// Enables code generation for the TryInto setter.
pub try_setter: bool,
}
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 FieldOptions {
/// Returns a `Setter` according to the options.
pub fn as_setter<'a>(&'a self) -> Setter<'a> {
Setter {
enabled: self.setter_enabled,
try_setter: self.try_setter,
visibility: &self.setter_visibility,
pattern: self.builder_pattern,
attrs: &self.attrs,
ident: &self.setter_ident,
field_ident: &self.field_ident,
field_type: &self.field_type,
generic_into: self.setter_into,
deprecation_notes: &self.deprecation_notes,
bindings: self.bindings,
}
}
/// Returns an `Initializer` according to the options.
///
/// # Panics
///
/// if `default_expression` can not be parsed as `Block`.
pub fn as_initializer<'a>(&'a self) -> Initializer<'a> {
Initializer {
setter_enabled: self.setter_enabled,
field_ident: &self.field_ident,
builder_pattern: self.builder_pattern,
default_value: self.default_expression
.as_ref()
.map(|x| { x.parse_block(self.bindings.no_std) }),
use_default_struct: self.use_default_struct,
bindings: self.bindings,
}
}
/// Returns a `BuilderField` according to the options.
pub fn as_builder_field<'a>(&'a self) -> BuilderField<'a> {
BuilderField {
field_ident: &self.field_ident,
field_type: &self.field_type,
setter_enabled: self.setter_enabled,
field_visibility: &self.field_visibility,
attrs: &self.attrs,
bindings: self.bindings,
}
}
}