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        config.resolve_for_annotations(&self.annotations)
33    }
34}
35
36impl From<crate::typed_ast::StructDef> for StructDcl {
37    fn from(value: crate::typed_ast::StructDef) -> Self {
38        let mut members = value
39            .member
40            .into_iter()
41            .map(Into::into)
42            .collect::<Vec<Member>>();
43        for (index, member) in members.iter_mut().enumerate() {
44            if member.field_id.is_none() {
45                member.field_id = Some((index + 1) as u32);
46            }
47        }
48        Self {
49            annotations: vec![],
50            ident: value.ident.0,
51            parent: value.parent.into_iter().map(Into::into).collect(),
52            member: members,
53        }
54    }
55}
56
57impl From<crate::typed_ast::Member> for Member {
58    fn from(value: crate::typed_ast::Member) -> Self {
59        let annotations = expand_annotations(value.annotations);
60        let field_id = annotation_id_value(&annotations);
61        Self {
62            annotations,
63            ty: value.ty.into(),
64            ident: value.ident.0.into_iter().map(Into::into).collect(),
65            default: value.default.map(Into::into),
66            field_id,
67        }
68    }
69}
70
71impl From<crate::typed_ast::StructForwardDcl> for StructForwardDcl {
72    fn from(typed_ast: crate::typed_ast::StructForwardDcl) -> Self {
73        Self {
74            annotations: vec![],
75            ident: typed_ast.ident.0,
76        }
77    }
78}
79
80impl From<crate::typed_ast::Default> for Default {
81    fn from(value: crate::typed_ast::Default) -> Self {
82        Self(value.0.into())
83    }
84}