Skip to main content

xidl_parser/hir/
struct_dcl.rs

1use super::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct StructForwardDcl {
6    pub annotations: Vec<Annotation>,
7    pub ident: String,
8}
9
10#[derive(Debug, Serialize, Deserialize, Clone)]
11pub struct StructDcl {
12    pub annotations: Vec<Annotation>,
13    pub ident: String,
14    pub parent: Vec<ScopedName>,
15    pub member: Vec<Member>,
16}
17
18#[derive(Debug, Serialize, Deserialize, Clone)]
19pub struct Member {
20    pub annotations: Vec<Annotation>,
21    pub ty: TypeSpec,
22    pub ident: Vec<Declarator>,
23    pub default: Option<Default>,
24    pub field_id: Option<u32>,
25}
26
27#[derive(Debug, Serialize, Deserialize, Clone)]
28pub struct Default(pub ConstExpr);
29
30impl StructDcl {
31    pub fn serialize_kind(&self, config: &SerializeConfig) -> SerializeKind {
32        if self.member.iter().any(Member::is_optional) {
33            config.resolve(Extensibility::Mutable)
34        } else {
35            config.resolve_for_annotations(&self.annotations)
36        }
37    }
38}
39
40impl Member {
41    pub fn is_optional(&self) -> bool {
42        self.annotations.iter().any(|annotation| match annotation {
43            Annotation::Builtin { name, .. } => name.eq_ignore_ascii_case("optional"),
44            Annotation::ScopedName { name, .. } => name
45                .name
46                .last()
47                .map(|value| value.eq_ignore_ascii_case("optional"))
48                .unwrap_or(false),
49            _ => false,
50        })
51    }
52}
53
54impl From<crate::typed_ast::StructDef> for StructDcl {
55    fn from(value: crate::typed_ast::StructDef) -> Self {
56        let mut members = value
57            .member
58            .into_iter()
59            .map(Into::into)
60            .collect::<Vec<Member>>();
61        for (index, member) in members.iter_mut().enumerate() {
62            if member.field_id.is_none() {
63                member.field_id = Some((index + 1) as u32);
64            }
65        }
66        Self {
67            annotations: vec![],
68            ident: value.ident.0,
69            parent: value.parent.into_iter().map(Into::into).collect(),
70            member: members,
71        }
72    }
73}
74
75impl From<crate::typed_ast::Member> for Member {
76    fn from(value: crate::typed_ast::Member) -> Self {
77        let annotations = expand_annotations(value.annotations);
78        let field_id = annotation_id_value(&annotations);
79        Self {
80            annotations,
81            ty: value.ty.into(),
82            ident: value.ident.0.into_iter().map(Into::into).collect(),
83            default: value.default.map(Into::into),
84            field_id,
85        }
86    }
87}
88
89impl From<crate::typed_ast::StructForwardDcl> for StructForwardDcl {
90    fn from(typed_ast: crate::typed_ast::StructForwardDcl) -> Self {
91        Self {
92            annotations: vec![],
93            ident: typed_ast.ident.0,
94        }
95    }
96}
97
98impl From<crate::typed_ast::Default> for Default {
99    fn from(value: crate::typed_ast::Default) -> Self {
100        Self(value.0.into())
101    }
102}