gir_parser/
class.rs

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