/
property_body.rs
128 lines (113 loc) · 3.24 KB
/
property_body.rs
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
use crate::{chunk::Chunk, env::Env, nameutil::use_gtk_type};
pub struct Builder<'a> {
name: String,
in_trait: bool,
var_name: String,
is_get: bool,
is_child_property: bool,
type_: String,
env: &'a Env,
}
impl<'a> Builder<'a> {
pub fn new(env: &'a Env) -> Self {
Self {
env,
name: Default::default(),
in_trait: Default::default(),
var_name: Default::default(),
is_get: Default::default(),
is_child_property: Default::default(),
type_: Default::default(),
}
}
pub fn new_for_child_property(env: &'a Env) -> Self {
Self {
is_child_property: true,
env,
name: Default::default(),
in_trait: Default::default(),
var_name: Default::default(),
is_get: Default::default(),
type_: Default::default(),
}
}
pub fn name(&mut self, name: &str) -> &mut Self {
self.name = name.into();
self
}
pub fn in_trait(&mut self, value: bool) -> &mut Self {
self.in_trait = value;
self
}
pub fn var_name(&mut self, name: &str) -> &mut Self {
self.var_name = name.into();
self
}
pub fn is_get(&mut self, value: bool) -> &mut Self {
self.is_get = value;
self
}
pub fn type_(&mut self, type_: &str) -> &mut Self {
self.type_ = type_.into();
self
}
pub fn generate(&self) -> Chunk {
let chunks = if self.is_get {
self.chunks_for_get()
} else {
self.chunks_for_set()
};
Chunk::BlockHalf(chunks)
}
fn chunks_for_get(&self) -> Vec<Chunk> {
if self.is_child_property {
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};
vec![Chunk::Custom(format!(
"{}::child_property({}, &item.clone().upcast(),\"{}\")",
use_gtk_type(self.env, "prelude::ContainerExtManual"),
self_,
self.name,
))]
} else {
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};
vec![Chunk::Custom(format!(
"ObjectExt::property({}, \"{}\")",
self_, self.name
))]
}
}
fn chunks_for_set(&self) -> Vec<Chunk> {
if self.is_child_property {
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};
vec![Chunk::Custom(format!(
"{}::child_set_property({}, &item.clone().upcast(),\"{}\", &{})",
use_gtk_type(self.env, "prelude::ContainerExtManual"),
self_,
self.name,
self.var_name
))]
} else {
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};
vec![Chunk::Custom(format!(
"ObjectExt::set_property({},\"{}\", {})",
self_, self.name, self.var_name
))]
}
}
}