gir_parser/
parameter.rs

1use xmlserde::xml_serde_enum;
2use xmlserde_derives::XmlDeserialize;
3
4use crate::{
5    array::Array,
6    attribute::Attribute,
7    documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
8    prelude::*,
9    r#type::Type,
10    FunctionScope, TransferOwnership,
11};
12
13xml_serde_enum! {
14    #[derive(Debug, Copy, Clone)]
15    Direction {
16        In => "in",
17        Out => "out",
18        InOut => "inout",
19    }
20}
21
22#[derive(Clone, Debug, XmlDeserialize)]
23pub enum ParameterType {
24    #[xmlserde(name = b"type")]
25    Type(Type),
26    #[xmlserde(name = b"array")]
27    Array(Array),
28    #[xmlserde(name = b"varargs")]
29    VarArgs,
30}
31
32impl From<crate::r#type::Type> for ParameterType {
33    fn from(value: crate::r#type::Type) -> Self {
34        Self::Type(value)
35    }
36}
37
38impl From<crate::r#type::AnyType> for ParameterType {
39    fn from(value: crate::r#type::AnyType) -> Self {
40        match value {
41            crate::r#type::AnyType::Array(arr) => Self::Array(arr),
42            crate::r#type::AnyType::Type(ty) => Self::Type(ty),
43        }
44    }
45}
46
47#[derive(Clone, Debug, Default, XmlDeserialize)]
48#[xmlserde(root = b"parameters")]
49#[xmlserde(deny_unknown_fields)]
50pub struct Parameters {
51    #[xmlserde(name = b"instance-parameter", ty = "child")]
52    instance_parameter: Option<InstanceParameter>,
53    #[xmlserde(name = b"parameter", ty = "child")]
54    parameter: Vec<Parameter>,
55}
56
57impl Parameters {
58    pub fn is_empty(&self) -> bool {
59        self.instance_parameter.is_none() && self.parameter.is_empty()
60    }
61
62    pub fn instance(&self) -> Option<&InstanceParameter> {
63        self.instance_parameter.as_ref()
64    }
65
66    pub fn inner(&self) -> &[Parameter] {
67        &self.parameter
68    }
69}
70
71impl IntoIterator for Parameters {
72    type Item = Parameter;
73    type IntoIter = std::vec::IntoIter<Self::Item>;
74
75    fn into_iter(self) -> Self::IntoIter {
76        self.parameter.into_iter()
77    }
78}
79
80#[derive(Clone, Debug, XmlDeserialize)]
81#[xmlserde(root = b"parameter")]
82#[xmlserde(deny_unknown_fields)]
83pub struct Parameter {
84    #[xmlserde(name = b"name", ty = "attr")]
85    name: String,
86    #[xmlserde(name = b"transfer-ownership", ty = "attr")]
87    transfer: Option<TransferOwnership>,
88    #[xmlserde(name = b"nullable", ty = "attr")]
89    nullable: Option<bool>,
90    #[xmlserde(name = b"allow-none", ty = "attr")]
91    allow_none: Option<bool>,
92    #[xmlserde(name = b"introspectable", ty = "attr")]
93    introspectable: Option<bool>,
94    #[xmlserde(name = b"scope", ty = "attr")]
95    scope: Option<FunctionScope>,
96    #[xmlserde(name = b"closure", ty = "attr")]
97    closure: Option<usize>,
98    #[xmlserde(name = b"destroy", ty = "attr")]
99    destroy: Option<usize>,
100    #[xmlserde(name = b"direction", ty = "attr")]
101    direction: Option<Direction>,
102    #[xmlserde(name = b"caller-allocates", ty = "attr")]
103    caller_allocates: Option<bool>,
104    #[xmlserde(name = b"optional", ty = "attr")]
105    optional: Option<bool>,
106    #[xmlserde(name = b"skip", ty = "attr")]
107    skip: Option<bool>,
108    // Documentation
109    #[xmlserde(name = b"doc", ty = "child")]
110    doc: Option<Documentation>,
111    #[xmlserde(name = b"doc-deprecated", ty = "child")]
112    doc_deprecated: Option<DocDeprecated>,
113    #[xmlserde(name = b"doc-stability", ty = "child")]
114    doc_stability: Option<DocStability>,
115    #[xmlserde(name = b"doc-version", ty = "child")]
116    doc_version: Option<DocVersion>,
117    #[xmlserde(name = b"source-position", ty = "child")]
118    source_position: Option<SourcePosition>,
119    // Attributes: 0 or more
120    #[xmlserde(name = b"attribute", ty = "child")]
121    attributes: Vec<Attribute>,
122    #[xmlserde(name = b"type", ty = "child")]
123    type_: Option<ParameterType>,
124}
125
126impl Parameter {
127    pub fn name(&self) -> &str {
128        &self.name
129    }
130
131    pub fn is_nullable(&self) -> Option<bool> {
132        self.nullable
133    }
134
135    pub fn is_allow_none(&self) -> Option<bool> {
136        self.allow_none
137    }
138
139    pub fn is_introspectable(&self) -> bool {
140        self.introspectable.unwrap_or(true)
141    }
142
143    pub fn scope(&self) -> Option<FunctionScope> {
144        self.scope
145    }
146
147    pub fn closure(&self) -> Option<usize> {
148        self.closure
149    }
150
151    pub fn destroy(&self) -> Option<usize> {
152        self.destroy
153    }
154
155    pub fn direction(&self) -> Option<Direction> {
156        self.direction
157    }
158
159    pub fn is_caller_allocates(&self) -> Option<bool> {
160        self.caller_allocates
161    }
162
163    pub fn is_optional(&self) -> Option<bool> {
164        self.optional
165    }
166
167    pub fn is_skip(&self) -> Option<bool> {
168        self.skip
169    }
170
171    pub fn transfer_ownership(&self) -> Option<TransferOwnership> {
172        self.transfer
173    }
174
175    pub fn ty(&self) -> Option<&ParameterType> {
176        self.type_.as_ref()
177    }
178}
179
180impl_attributable!(Parameter);
181impl_documentable!(Parameter);
182
183#[derive(Clone, Debug, XmlDeserialize)]
184#[xmlserde(root = b"instance-parameter")]
185#[xmlserde(deny_unknown_fields)]
186pub struct InstanceParameter {
187    #[xmlserde(name = b"name", ty = "attr")]
188    name: String,
189    #[xmlserde(name = b"transfer-ownership", ty = "attr")]
190    transfer: Option<TransferOwnership>,
191    #[xmlserde(name = b"nullable", ty = "attr")]
192    nullable: Option<bool>,
193    #[xmlserde(name = b"allow-none", ty = "attr")]
194    allow_none: Option<bool>,
195    #[xmlserde(name = b"direction", ty = "attr")]
196    direction: Option<Direction>,
197    #[xmlserde(name = b"caller-allocates", ty = "attr")]
198    caller_allocates: Option<bool>,
199    // Documentation
200    #[xmlserde(name = b"doc", ty = "child")]
201    doc: Option<Documentation>,
202    #[xmlserde(name = b"doc-deprecated", ty = "child")]
203    doc_deprecated: Option<DocDeprecated>,
204    #[xmlserde(name = b"doc-stability", ty = "child")]
205    doc_stability: Option<DocStability>,
206    #[xmlserde(name = b"doc-version", ty = "child")]
207    doc_version: Option<DocVersion>,
208    #[xmlserde(name = b"source-position", ty = "child")]
209    source_position: Option<SourcePosition>,
210    #[xmlserde(name = b"type", ty = "child")]
211    type_: Option<Type>,
212}
213
214impl InstanceParameter {
215    pub fn name(&self) -> &str {
216        &self.name
217    }
218
219    pub fn is_nullable(&self) -> Option<bool> {
220        self.nullable
221    }
222
223    pub fn is_allow_none(&self) -> Option<bool> {
224        self.allow_none
225    }
226
227    pub fn direction(&self) -> Option<Direction> {
228        self.direction
229    }
230
231    pub fn is_caller_allocates(&self) -> Option<bool> {
232        self.caller_allocates
233    }
234
235    pub fn transfer_ownership(&self) -> Option<TransferOwnership> {
236        self.transfer
237    }
238
239    pub fn ty(&self) -> Option<&Type> {
240        self.type_.as_ref()
241    }
242}
243
244impl_documentable!(InstanceParameter);