gir_parser/
type.rs

1use xmlserde_derives::XmlDeserialize;
2
3use crate::{
4    array::Array,
5    documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
6    prelude::*,
7};
8
9#[derive(Clone, Debug, XmlDeserialize)]
10#[xmlserde(root = b"type")]
11#[xmlserde(deny_unknown_fields)]
12pub struct Type {
13    #[xmlserde(name = b"name", ty = "attr")]
14    name: Option<String>,
15    #[xmlserde(name = b"c:type", ty = "attr")]
16    c_type: Option<String>,
17    #[xmlserde(name = b"introspectable", ty = "attr")]
18    introspectable: Option<bool>,
19    // Documentation
20    #[xmlserde(name = b"doc", ty = "child")]
21    doc: Option<Documentation>,
22    #[xmlserde(name = b"doc-deprecated", ty = "child")]
23    doc_deprecated: Option<DocDeprecated>,
24    #[xmlserde(name = b"doc-stability", ty = "child")]
25    doc_stability: Option<DocStability>,
26    #[xmlserde(name = b"doc-version", ty = "child")]
27    doc_version: Option<DocVersion>,
28    #[xmlserde(name = b"source-position", ty = "child")]
29    source_position: Option<SourcePosition>,
30    #[xmlserde(name = b"type", ty = "child")]
31    types: Vec<Type>,
32}
33
34impl Type {
35    pub fn name(&self) -> Option<&str> {
36        self.name.as_deref()
37    }
38
39    pub fn c_type(&self) -> Option<&str> {
40        self.c_type.as_deref()
41    }
42
43    pub fn is_introspectable(&self) -> bool {
44        self.introspectable.unwrap_or(true)
45    }
46
47    pub fn types(&self) -> &[Type] {
48        &self.types
49    }
50}
51
52impl_documentable!(Type);
53
54#[derive(Clone, Debug, XmlDeserialize)]
55pub enum AnyType {
56    #[xmlserde(name = b"type")]
57    Type(Type),
58    #[xmlserde(name = b"array")]
59    Array(Array),
60}
61
62impl From<crate::r#type::Type> for AnyType {
63    fn from(value: crate::r#type::Type) -> Self {
64        Self::Type(value)
65    }
66}
67
68impl AnyType {
69    pub fn is_array(&self) -> bool {
70        matches!(self, Self::Array(_))
71    }
72
73    pub fn as_array(&self) -> &Array {
74        match self {
75            Self::Array(array) => array,
76            _ => unreachable!(),
77        }
78    }
79
80    pub fn is_type(&self) -> bool {
81        matches!(self, Self::Type(_))
82    }
83
84    pub fn as_type(&self) -> &Type {
85        match self {
86            Self::Type(ty) => ty,
87            _ => unreachable!(),
88        }
89    }
90}