Skip to main content

gir_parser/
class.rs

1use xmlserde_derives::XmlDeserialize;
2
3use crate::{
4    attribute::Attribute,
5    callable::Callable,
6    callback::Callback,
7    constant::Constant,
8    documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
9    field::Field,
10    function::{Function, FunctionInline},
11    method::{Method, MethodInline},
12    prelude::*,
13    property::Property,
14    record::Record,
15    signal::Signal,
16    union::Union,
17    version::Version,
18    virtual_method::VirtualMethod,
19    Stability,
20};
21
22#[derive(Clone, Debug, PartialEq, Eq, Hash, XmlDeserialize)]
23#[xmlserde(root = b"implements")]
24#[xmlserde(deny_unknown_fields)]
25pub struct Implements {
26    #[xmlserde(name = b"name", ty = "attr")]
27    name: String,
28}
29
30impl Implements {
31    pub fn name(&self) -> &str {
32        &self.name
33    }
34}
35
36#[derive(Clone, Debug, PartialEq, Eq, XmlDeserialize)]
37// FIXME: The `Type` / `AnyType` fields are quite huge and some boxing would
38// probably be useful here but `xmlserde` does not seem to support that.
39#[allow(clippy::large_enum_variant)]
40pub enum ClassField {
41    #[xmlserde(name = b"field")]
42    Field(Field),
43    #[xmlserde(name = b"union")]
44    Union(Union),
45    #[xmlserde(name = b"record")]
46    Record(Record),
47    #[xmlserde(name = b"callback")]
48    Callback(Callback),
49}
50
51#[derive(Clone, Debug, XmlDeserialize)]
52#[xmlserde(root = b"class")]
53#[xmlserde(deny_unknown_fields)]
54pub struct Class {
55    #[xmlserde(name = b"name", ty = "attr")]
56    name: String,
57    #[xmlserde(name = b"c:symbol-prefix", ty = "attr")]
58    symbol_prefix: Option<String>,
59    #[xmlserde(name = b"c:type", ty = "attr")]
60    c_type: Option<String>,
61    #[xmlserde(name = b"parent", ty = "attr")]
62    parent: Option<String>,
63    // Common attributes
64    #[xmlserde(name = b"introspectable", ty = "attr")]
65    introspectable: Option<bool>,
66    #[xmlserde(name = b"deprecated", ty = "attr")]
67    deprecated: Option<bool>,
68    #[xmlserde(name = b"version", ty = "attr")]
69    version: Option<Version>,
70    #[xmlserde(name = b"deprecated-version", ty = "attr")]
71    deprecated_version: Option<Version>,
72    #[xmlserde(name = b"stability", ty = "attr")]
73    stability: Option<Stability>,
74    // Documentation
75    #[xmlserde(name = b"doc", ty = "child")]
76    doc: Option<Documentation>,
77    #[xmlserde(name = b"doc-deprecated", ty = "child")]
78    doc_deprecated: Option<DocDeprecated>,
79    #[xmlserde(name = b"doc-stability", ty = "child")]
80    doc_stability: Option<DocStability>,
81    #[xmlserde(name = b"doc-version", ty = "child")]
82    doc_version: Option<DocVersion>,
83    #[xmlserde(name = b"source-position", ty = "child")]
84    source_position: Option<SourcePosition>,
85    // Attributes: 0 or more
86    #[xmlserde(name = b"attribute", ty = "child")]
87    attributes: Vec<Attribute>,
88
89    #[xmlserde(name = b"glib:type-name", ty = "attr")]
90    g_type_name: String,
91    #[xmlserde(name = b"glib:get-type", ty = "attr")]
92    g_get_type: String,
93    #[xmlserde(name = b"glib:type-struct", ty = "attr")]
94    g_type_struct: Option<String>,
95    #[xmlserde(name = b"glib:fundamental", ty = "attr")]
96    g_fundamental: Option<bool>,
97    #[xmlserde(name = b"final", ty = "attr")]
98    r#final: Option<bool>,
99    #[xmlserde(name = b"abstract", ty = "attr")]
100    r#abstract: Option<bool>,
101    #[xmlserde(name = b"glib:ref-func", ty = "attr")]
102    g_ref_func: Option<String>,
103    #[xmlserde(name = b"glib:unref-func", ty = "attr")]
104    g_unref_func: Option<String>,
105    #[xmlserde(name = b"glib:set-value-func", ty = "attr")]
106    g_set_value_func: Option<String>,
107    #[xmlserde(name = b"glib:get-value-func", ty = "attr")]
108    g_get_value_func: Option<String>,
109    #[xmlserde(name = b"implements", ty = "child")]
110    implements: Vec<Implements>,
111
112    #[xmlserde(name = b"function-inline", ty = "child")]
113    inline_functions: Vec<FunctionInline>,
114
115    #[xmlserde(name = b"inline-methods", ty = "child")]
116    inline_methods: Vec<MethodInline>,
117
118    #[xmlserde(name = b"property", ty = "child")]
119    properties: Vec<Property>,
120    #[xmlserde(name = b"glib:signal", ty = "child")]
121    signals: Vec<Signal>,
122    #[xmlserde(name = b"virtual-method", ty = "child")]
123    virtual_methods: Vec<VirtualMethod>,
124    #[xmlserde(name = b"constant", ty = "child")]
125    constants: Vec<Constant>,
126
127    #[xmlserde(ty = "untag")]
128    fields: Vec<ClassField>,
129    #[xmlserde(ty = "untag")]
130    callables: Vec<Callable>,
131}
132
133impl Class {
134    pub fn name(&self) -> &str {
135        &self.name
136    }
137
138    pub fn is_fundamental(&self) -> bool {
139        self.g_fundamental.unwrap_or(false)
140    }
141
142    pub fn is_final(&self) -> bool {
143        self.r#final.unwrap_or(false)
144    }
145
146    pub fn is_abstract(&self) -> bool {
147        self.r#abstract.unwrap_or(false)
148    }
149
150    pub fn symbol_prefix(&self) -> Option<&str> {
151        self.symbol_prefix.as_deref()
152    }
153
154    pub fn c_type(&self) -> Option<&str> {
155        self.c_type.as_deref()
156    }
157
158    pub fn parent(&self) -> Option<&str> {
159        self.parent.as_deref()
160    }
161
162    pub fn g_type_name(&self) -> &str {
163        &self.g_type_name
164    }
165
166    pub fn g_get_type(&self) -> &str {
167        &self.g_get_type
168    }
169
170    pub fn g_type_struct(&self) -> Option<&str> {
171        self.g_type_struct.as_deref()
172    }
173
174    pub fn g_ref_func(&self) -> Option<&str> {
175        self.g_ref_func.as_deref()
176    }
177
178    pub fn g_unref_func(&self) -> Option<&str> {
179        self.g_unref_func.as_deref()
180    }
181
182    pub fn g_set_value_func(&self) -> Option<&str> {
183        self.g_set_value_func.as_deref()
184    }
185
186    pub fn g_get_value_func(&self) -> Option<&str> {
187        self.g_get_value_func.as_deref()
188    }
189
190    pub fn implements(&self) -> &[Implements] {
191        &self.implements
192    }
193
194    pub fn callables(&self) -> &[Callable] {
195        &self.callables
196    }
197
198    pub fn constructors(&self) -> impl Iterator<Item = &Function> {
199        self.callables.iter().filter_map(|c| match c {
200            Callable::Constructor(f) => Some(f),
201            _ => None,
202        })
203    }
204
205    pub fn methods(&self) -> impl Iterator<Item = &Method> {
206        self.callables.iter().filter_map(|c| match c {
207            Callable::Method(m) => Some(m),
208            _ => None,
209        })
210    }
211
212    pub fn inlined_methods(&self) -> &[MethodInline] {
213        &self.inline_methods
214    }
215
216    pub fn functions(&self) -> impl Iterator<Item = &Function> {
217        self.callables.iter().filter_map(|c| match c {
218            Callable::Function(f) => Some(f),
219            _ => None,
220        })
221    }
222
223    pub fn inlined_functions(&self) -> &[FunctionInline] {
224        &self.inline_functions
225    }
226
227    pub fn virtual_methods(&self) -> &[VirtualMethod] {
228        &self.virtual_methods
229    }
230
231    pub fn fields(&self) -> &[ClassField] {
232        &self.fields
233    }
234
235    pub fn properties(&self) -> &[Property] {
236        &self.properties
237    }
238
239    pub fn signals(&self) -> &[Signal] {
240        &self.signals
241    }
242
243    pub fn constants(&self) -> &[Constant] {
244        &self.constants
245    }
246}
247
248impl_documentable!(Class);
249impl_attributable!(Class);
250impl_info!(Class);