xidl_parser/hir/
struct_dcl.rs1use 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 Member {
31 pub fn is_optional(&self) -> bool {
32 self.annotations.iter().any(|annotation| match annotation {
33 Annotation::Optional { .. } => true,
34 Annotation::ScopedName { name, .. } => name
35 .name
36 .last()
37 .map(|value| value.eq_ignore_ascii_case("optional"))
38 .unwrap_or(false),
39 _ => false,
40 })
41 }
42}
43
44impl From<crate::typed_ast::StructDef> for StructDcl {
45 fn from(value: crate::typed_ast::StructDef) -> Self {
46 let mut members = value
47 .member
48 .into_iter()
49 .map(Into::into)
50 .collect::<Vec<Member>>();
51 for (index, member) in members.iter_mut().enumerate() {
52 if member.field_id.is_none() {
53 member.field_id = Some((index + 1) as u32);
54 }
55 }
56 Self {
57 annotations: vec![],
58 ident: value.ident.0,
59 parent: value.parent.into_iter().map(Into::into).collect(),
60 member: members,
61 }
62 }
63}
64
65impl From<crate::typed_ast::Member> for Member {
66 fn from(value: crate::typed_ast::Member) -> Self {
67 let annotations = expand_annotations(value.annotations);
68 let field_id = annotation_id_value(&annotations);
69 Self {
70 annotations,
71 ty: value.ty.into(),
72 ident: value.ident.0.into_iter().map(Into::into).collect(),
73 default: value.default.map(Into::into),
74 field_id,
75 }
76 }
77}
78
79impl From<crate::typed_ast::StructForwardDcl> for StructForwardDcl {
80 fn from(typed_ast: crate::typed_ast::StructForwardDcl) -> Self {
81 Self {
82 annotations: vec![],
83 ident: typed_ast.ident.0,
84 }
85 }
86}
87
88impl From<crate::typed_ast::Default> for Default {
89 fn from(value: crate::typed_ast::Default) -> Self {
90 Self(value.0.into())
91 }
92}