gir_parser/
field.rs
1use xmlserde_derives::XmlDeserialize;
2
3use crate::{
4 array::Array,
5 attribute::Attribute,
6 callback::Callback,
7 documentation::{DocDeprecated, DocStability, DocVersion, Documentation, SourcePosition},
8 prelude::*,
9 r#type::Type,
10 version::Version,
11 Stability,
12};
13
14#[derive(Clone, Debug, XmlDeserialize)]
15#[allow(clippy::large_enum_variant)]
16pub enum FieldType {
17 #[xmlserde(name = b"type")]
18 Type(Type),
19 #[xmlserde(name = b"callback")]
20 Callback(Callback),
21 #[xmlserde(name = b"array")]
22 Array(Array),
23}
24
25impl FieldType {
26 pub fn is_type(&self) -> bool {
27 matches!(self, Self::Type(_))
28 }
29
30 pub fn as_type(&self) -> &Type {
31 if let Self::Type(t) = &self {
32 t
33 } else {
34 unreachable!()
35 }
36 }
37
38 pub fn is_callback(&self) -> bool {
39 matches!(self, Self::Callback(_))
40 }
41
42 pub fn as_callback(&self) -> &Callback {
43 if let Self::Callback(c) = &self {
44 c
45 } else {
46 unreachable!()
47 }
48 }
49
50 pub fn is_array(&self) -> bool {
51 matches!(self, Self::Array(_))
52 }
53
54 pub fn as_array(&self) -> &Array {
55 if let Self::Array(a) = &self {
56 a
57 } else {
58 unreachable!()
59 }
60 }
61}
62
63#[derive(Clone, Debug, XmlDeserialize)]
64#[xmlserde(root = b"field")]
65#[xmlserde(deny_unknown_fields)]
66pub struct Field {
67 #[xmlserde(name = b"name", ty = "attr")]
68 name: String,
69 #[xmlserde(name = b"readable", ty = "attr")]
70 readable: Option<bool>,
71 #[xmlserde(name = b"writable", ty = "attr")]
72 writable: Option<bool>,
73 #[xmlserde(name = b"nullable", ty = "attr")]
76 _nullable: Option<bool>,
77 #[xmlserde(name = b"private", ty = "attr")]
78 private: Option<bool>,
79 #[xmlserde(name = b"bits", ty = "attr")]
80 bits: Option<u8>,
81 #[xmlserde(name = b"introspectable", ty = "attr")]
83 introspectable: Option<bool>,
84 #[xmlserde(name = b"deprecated", ty = "attr")]
85 deprecated: Option<bool>,
86 #[xmlserde(name = b"version", ty = "attr")]
87 version: Option<Version>,
88 #[xmlserde(name = b"deprecated-version", ty = "attr")]
89 deprecated_version: Option<Version>,
90 #[xmlserde(name = b"stability", ty = "attr")]
91 stability: Option<Stability>,
92 #[xmlserde(name = b"doc", ty = "child")]
94 doc: Option<Documentation>,
95 #[xmlserde(name = b"doc-deprecated", ty = "child")]
96 doc_deprecated: Option<DocDeprecated>,
97 #[xmlserde(name = b"doc-stability", ty = "child")]
98 doc_stability: Option<DocStability>,
99 #[xmlserde(name = b"doc-version", ty = "child")]
100 doc_version: Option<DocVersion>,
101 #[xmlserde(name = b"source-position", ty = "child")]
102 source_position: Option<SourcePosition>,
103 #[xmlserde(name = b"attribute", ty = "child")]
105 attributes: Vec<Attribute>,
106 #[xmlserde(ty = "untag")]
107 inner: FieldType,
108}
109
110impl Field {
111 pub fn name(&self) -> &str {
112 &self.name
113 }
114
115 pub fn is_readable(&self) -> bool {
116 self.readable.unwrap_or(false)
117 }
118
119 pub fn is_writable(&self) -> bool {
120 self.writable.unwrap_or(false)
121 }
122
123 pub fn is_private(&self) -> bool {
124 self.private.unwrap_or(false)
125 }
126
127 pub fn bits(&self) -> Option<u8> {
128 self.bits
129 }
130
131 pub fn ty(&self) -> &FieldType {
132 &self.inner
133 }
134}
135
136impl_info!(Field);
137impl_documentable!(Field);
138impl_attributable!(Field);