Skip to main content

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, PartialEq, Eq, Hash, 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    #[xmlserde(name = b"array", ty = "child")]
33    arrays: Vec<Array>,
34}
35
36impl Type {
37    pub fn name(&self) -> Option<&str> {
38        self.name.as_deref()
39    }
40
41    pub fn c_type(&self) -> Option<&str> {
42        self.c_type.as_deref()
43    }
44
45    pub fn is_introspectable(&self) -> bool {
46        self.introspectable.unwrap_or(true)
47    }
48
49    pub fn types(&self) -> &[Type] {
50        &self.types
51    }
52
53    pub fn arrays(&self) -> &[Array] {
54        &self.arrays
55    }
56}
57
58impl_documentable!(Type);
59
60#[derive(Clone, Debug, PartialEq, Eq, XmlDeserialize)]
61pub enum AnyType {
62    #[xmlserde(name = b"type")]
63    Type(Type),
64    #[xmlserde(name = b"array")]
65    Array(Array),
66}
67
68impl From<crate::r#type::Type> for AnyType {
69    fn from(value: crate::r#type::Type) -> Self {
70        Self::Type(value)
71    }
72}
73
74impl AnyType {
75    pub fn is_array(&self) -> bool {
76        matches!(self, Self::Array(_))
77    }
78
79    pub fn as_array(&self) -> &Array {
80        match self {
81            Self::Array(array) => array,
82            _ => unreachable!(),
83        }
84    }
85
86    pub fn is_type(&self) -> bool {
87        matches!(self, Self::Type(_))
88    }
89
90    pub fn as_type(&self) -> &Type {
91        match self {
92            Self::Type(ty) => ty,
93            _ => unreachable!(),
94        }
95    }
96}