gir_parser/
union.rs

1use xmlserde_derives::XmlDeserialize;
2
3use crate::{
4    attribute::Attribute,
5    documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
6    field::Field,
7    function::{Function, FunctionInline},
8    method::{Method, MethodInline},
9    prelude::*,
10    record::Record,
11    version::Version,
12    Callback, Stability,
13};
14
15#[derive(Clone, Debug, XmlDeserialize)]
16// FIXME: The `Type` / `AnyType` fields are quite huge and some boxing would
17// probably be useful here but `xmlserde` does not seem to support that.
18#[allow(clippy::large_enum_variant)]
19pub enum UnionField {
20    #[xmlserde(name = b"field")]
21    Field(Field),
22    #[xmlserde(name = b"union")]
23    Union(Union),
24    #[xmlserde(name = b"record")]
25    Record(Record),
26    #[xmlserde(name = b"callback")]
27    Callback(Callback),
28}
29
30#[derive(Clone, Debug, XmlDeserialize)]
31#[xmlserde(root = b"union")]
32#[xmlserde(deny_unknown_fields)]
33pub struct Union {
34    #[xmlserde(name = b"name", ty = "attr")]
35    name: Option<String>,
36    #[xmlserde(name = b"c:type", ty = "attr")]
37    c_type: Option<String>,
38    #[xmlserde(name = b"c:symbol-prefix", ty = "attr")]
39    c_symbol_prefix: Option<String>,
40    #[xmlserde(name = b"glib:type-name", ty = "attr")]
41    g_type_name: Option<String>,
42    #[xmlserde(name = b"glib:get-type", ty = "attr")]
43    g_get_type: Option<String>,
44    #[xmlserde(name = b"copy-function", ty = "attr")]
45    copy_function: Option<String>,
46    #[xmlserde(name = b"free-function", ty = "attr")]
47    free_function: Option<String>,
48    // Common attributes
49    #[xmlserde(name = b"introspectable", ty = "attr")]
50    introspectable: Option<bool>,
51    #[xmlserde(name = b"deprecated", ty = "attr")]
52    deprecated: Option<bool>,
53    #[xmlserde(name = b"version", ty = "attr")]
54    version: Option<Version>,
55    #[xmlserde(name = b"deprecated-version", ty = "attr")]
56    deprecated_version: Option<Version>,
57    #[xmlserde(name = b"stability", ty = "attr")]
58    stability: Option<Stability>,
59    // Documentation
60    #[xmlserde(name = b"doc", ty = "child")]
61    doc: Option<Documentation>,
62    #[xmlserde(name = b"doc-deprecated", ty = "child")]
63    doc_deprecated: Option<DocDeprecated>,
64    #[xmlserde(name = b"doc-stability", ty = "child")]
65    doc_stability: Option<DocStability>,
66    #[xmlserde(name = b"doc-version", ty = "child")]
67    doc_version: Option<DocVersion>,
68    #[xmlserde(name = b"source-position", ty = "child")]
69    source_position: Option<SourcePosition>,
70    // Attributes: 0 or more
71    #[xmlserde(name = b"attribute", ty = "child")]
72    attributes: Vec<Attribute>,
73
74    #[xmlserde(name = b"constructor", ty = "child")]
75    constructors: Vec<Function>,
76    #[xmlserde(name = b"function", ty = "child")]
77    functions: Vec<Function>,
78    #[xmlserde(name = b"function-inline", ty = "child")]
79    inline_functions: Vec<FunctionInline>,
80
81    #[xmlserde(name = b"method", ty = "child")]
82    methods: Vec<Method>,
83    #[xmlserde(name = b"method-inline", ty = "child")]
84    inline_methods: Vec<MethodInline>,
85
86    #[xmlserde(ty = "untag")]
87    fields: Vec<UnionField>,
88}
89
90impl Union {
91    pub fn name(&self) -> Option<&str> {
92        self.name.as_deref()
93    }
94
95    pub fn c_type(&self) -> Option<&str> {
96        self.c_type.as_deref()
97    }
98
99    pub fn c_symbol_prefix(&self) -> Option<&str> {
100        self.c_symbol_prefix.as_deref()
101    }
102
103    pub fn g_type_name(&self) -> Option<&str> {
104        self.g_type_name.as_deref()
105    }
106
107    pub fn g_get_type(&self) -> Option<&str> {
108        self.g_get_type.as_deref()
109    }
110
111    pub fn copy_function(&self) -> Option<&str> {
112        self.copy_function.as_deref()
113    }
114
115    pub fn free_function(&self) -> Option<&str> {
116        self.free_function.as_deref()
117    }
118
119    pub fn fields(&self) -> &[UnionField] {
120        &self.fields
121    }
122
123    pub fn constructors(&self) -> &[Function] {
124        &self.constructors
125    }
126
127    pub fn inlined_methods(&self) -> &[MethodInline] {
128        &self.inline_methods
129    }
130
131    pub fn methods(&self) -> &[Method] {
132        &self.methods
133    }
134
135    pub fn inlined_functions(&self) -> &[FunctionInline] {
136        &self.inline_functions
137    }
138
139    pub fn functions(&self) -> &[Function] {
140        &self.functions
141    }
142}
143
144impl_info!(Union);
145impl_attributable!(Union);
146impl_documentable!(Union);