gir_parser/
traits.rs

1use crate::{
2    attribute::Attribute,
3    documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
4    version::Version,
5    Parameters, ReturnValue, Stability,
6};
7
8pub trait Documentable {
9    fn doc(&self) -> Option<&Documentation>;
10    fn doc_deprecated(&self) -> Option<&DocDeprecated>;
11    fn doc_stability(&self) -> Option<&DocStability>;
12    fn doc_version(&self) -> Option<&DocVersion>;
13    fn source_position(&self) -> Option<&SourcePosition>;
14}
15
16pub trait Attributable {
17    fn attributes(&self) -> &[Attribute];
18
19    fn element_type(&self) -> Option<&str> {
20        self.attributes()
21            .iter()
22            .find(|a| a.name() == "element-type")
23            .map(|a| a.value())
24    }
25
26    fn gtk_property_get(&self) -> Option<&str> {
27        self.attributes()
28            .iter()
29            .find(|a| a.name() == "org.gtk.Property.get")
30            .map(|a| a.value())
31    }
32
33    fn gtk_method_get_property(&self) -> Option<&str> {
34        self.attributes()
35            .iter()
36            .find(|a| a.name() == "org.gtk.Method.get_property")
37            .map(|a| a.value())
38    }
39
40    fn gtk_property_set(&self) -> Option<&str> {
41        self.attributes()
42            .iter()
43            .find(|a| a.name() == "org.gtk.Property.set")
44            .map(|a| a.value())
45    }
46
47    fn gtk_method_set_property(&self) -> Option<&str> {
48        self.attributes()
49            .iter()
50            .find(|a| a.name() == "org.gtk.Method.set_property")
51            .map(|a| a.value())
52    }
53}
54
55pub trait Info: Documentable + Attributable {
56    fn is_introspectable(&self) -> bool;
57    fn is_deprecated(&self) -> bool;
58    fn version(&self) -> Option<&Version>;
59    fn deprecated_version(&self) -> Option<&Version>;
60    fn stability(&self) -> Option<Stability>;
61}
62
63pub trait Callable: Info {
64    fn name(&self) -> &str;
65    fn c_identifier(&self) -> Option<&str>;
66    fn shadows(&self) -> Option<&str>;
67    fn shadowed_by(&self) -> Option<&str>;
68    fn moved_to(&self) -> Option<&str>;
69    fn async_func(&self) -> Option<&str>;
70    fn finish_func(&self) -> Option<&str>;
71    fn sync_func(&self) -> Option<&str>;
72}
73
74macro_rules! impl_documentable {
75    ($rust_type:ident) => {
76        impl Documentable for $rust_type {
77            fn doc(&self) -> Option<&Documentation> {
78                self.doc.as_ref()
79            }
80            fn doc_deprecated(&self) -> Option<&DocDeprecated> {
81                self.doc_deprecated.as_ref()
82            }
83            fn doc_stability(&self) -> Option<&DocStability> {
84                self.doc_stability.as_ref()
85            }
86            fn doc_version(&self) -> Option<&DocVersion> {
87                self.doc_version.as_ref()
88            }
89            fn source_position(&self) -> Option<&SourcePosition> {
90                self.source_position.as_ref()
91            }
92        }
93    };
94}
95
96macro_rules! impl_attributable {
97    ($rust_type:ident) => {
98        impl Attributable for $rust_type {
99            fn attributes(&self) -> &[Attribute] {
100                &self.attributes
101            }
102        }
103    };
104}
105
106macro_rules! impl_info {
107    ($rust_type:ident) => {
108        impl Info for $rust_type {
109            fn is_introspectable(&self) -> bool {
110                self.introspectable.unwrap_or(true)
111            }
112
113            fn is_deprecated(&self) -> bool {
114                self.deprecated.unwrap_or(false)
115            }
116
117            fn version(&self) -> Option<&Version> {
118                self.version.as_ref()
119            }
120
121            fn deprecated_version(&self) -> Option<&Version> {
122                self.deprecated_version.as_ref()
123            }
124
125            fn stability(&self) -> Option<Stability> {
126                self.stability
127            }
128        }
129    };
130}
131
132macro_rules! impl_callable {
133    ($rust_type:ident) => {
134        impl Callable for $rust_type {
135            fn name(&self) -> &str {
136                &self.name
137            }
138
139            fn c_identifier(&self) -> Option<&str> {
140                self.c_identifier.as_deref()
141            }
142
143            fn shadows(&self) -> Option<&str> {
144                self.shadows.as_deref()
145            }
146
147            fn shadowed_by(&self) -> Option<&str> {
148                self.shadowed_by.as_deref()
149            }
150
151            fn moved_to(&self) -> Option<&str> {
152                self.moved_to.as_deref()
153            }
154
155            fn async_func(&self) -> Option<&str> {
156                self.async_func.as_deref()
157            }
158
159            fn finish_func(&self) -> Option<&str> {
160                self.finish_func.as_deref()
161            }
162
163            fn sync_func(&self) -> Option<&str> {
164                self.sync_func.as_deref()
165            }
166        }
167    };
168}
169
170pub trait FunctionLike: Attributable + Info + Documentable {
171    fn throws(&self) -> bool;
172    fn return_value(&self) -> &ReturnValue;
173    fn parameters(&self) -> &Parameters;
174}
175
176macro_rules! impl_function_like {
177    ($rust_type:ident) => {
178        impl FunctionLike for $rust_type {
179            fn throws(&self) -> bool {
180                self.throws.unwrap_or(false)
181            }
182
183            fn return_value(&self) -> &ReturnValue {
184                &self.return_value
185            }
186
187            fn parameters(&self) -> &Parameters {
188                &self.parameters
189            }
190        }
191    };
192}