libgir/codegen/
property_body.rs

1use crate::{chunk::Chunk, env::Env, nameutil::use_gtk_type};
2
3pub struct Builder<'a> {
4    name: String,
5    in_trait: bool,
6    var_name: String,
7    is_get: bool,
8    is_child_property: bool,
9    type_: String,
10    env: &'a Env,
11}
12
13impl<'a> Builder<'a> {
14    pub fn new(env: &'a Env) -> Self {
15        Self {
16            env,
17            name: Default::default(),
18            in_trait: Default::default(),
19            var_name: Default::default(),
20            is_get: Default::default(),
21            is_child_property: Default::default(),
22            type_: Default::default(),
23        }
24    }
25
26    pub fn new_for_child_property(env: &'a Env) -> Self {
27        Self {
28            is_child_property: true,
29            env,
30            name: Default::default(),
31            in_trait: Default::default(),
32            var_name: Default::default(),
33            is_get: Default::default(),
34            type_: Default::default(),
35        }
36    }
37
38    pub fn name(&mut self, name: &str) -> &mut Self {
39        self.name = name.into();
40        self
41    }
42
43    pub fn in_trait(&mut self, value: bool) -> &mut Self {
44        self.in_trait = value;
45        self
46    }
47
48    pub fn var_name(&mut self, name: &str) -> &mut Self {
49        self.var_name = name.into();
50        self
51    }
52
53    pub fn is_get(&mut self, value: bool) -> &mut Self {
54        self.is_get = value;
55        self
56    }
57
58    pub fn type_(&mut self, type_: &str) -> &mut Self {
59        self.type_ = type_.into();
60        self
61    }
62
63    pub fn generate(&self) -> Chunk {
64        let chunks = if self.is_get {
65            self.chunks_for_get()
66        } else {
67            self.chunks_for_set()
68        };
69        Chunk::BlockHalf(chunks)
70    }
71
72    fn chunks_for_get(&self) -> Vec<Chunk> {
73        if self.is_child_property {
74            let self_ = if self.in_trait {
75                "self.as_ref()"
76            } else {
77                "self"
78            };
79
80            vec![Chunk::Custom(format!(
81                "{}::child_property({}, &item.clone().upcast(),\"{}\")",
82                use_gtk_type(self.env, "prelude::ContainerExtManual"),
83                self_,
84                self.name,
85            ))]
86        } else {
87            let self_ = if self.in_trait {
88                "self.as_ref()"
89            } else {
90                "self"
91            };
92
93            vec![Chunk::Custom(format!(
94                "ObjectExt::property({}, \"{}\")",
95                self_, self.name
96            ))]
97        }
98    }
99
100    fn chunks_for_set(&self) -> Vec<Chunk> {
101        if self.is_child_property {
102            let self_ = if self.in_trait {
103                "self.as_ref()"
104            } else {
105                "self"
106            };
107
108            vec![Chunk::Custom(format!(
109                "{}::child_set_property({}, &item.clone().upcast(),\"{}\", &{})",
110                use_gtk_type(self.env, "prelude::ContainerExtManual"),
111                self_,
112                self.name,
113                self.var_name
114            ))]
115        } else {
116            let self_ = if self.in_trait {
117                "self.as_ref()"
118            } else {
119                "self"
120            };
121
122            vec![Chunk::Custom(format!(
123                "ObjectExt::set_property({},\"{}\", {})",
124                self_, self.name, self.var_name
125            ))]
126        }
127    }
128}