aldrin_core/introspection/ir/
layout.rs1use super::{BuiltInTypeIr, EnumIr, LexicalId, NewtypeIr, ServiceIr, StructIr};
2use crate::tags::{PrimaryTag, Tag};
3use crate::{Serialize, SerializeError, Serializer};
4use num_enum::{IntoPrimitive, TryFromPrimitive};
5use uuid::Uuid;
6
7#[derive(Debug, Clone)]
8pub enum LayoutIr {
9 BuiltIn(BuiltInTypeIr),
10 Struct(StructIr),
11 Enum(EnumIr),
12 Service(ServiceIr),
13 Newtype(NewtypeIr),
14}
15
16impl LayoutIr {
17 pub fn namespace(&self) -> Uuid {
18 match self {
19 Self::BuiltIn(_) => BuiltInTypeIr::NAMESPACE,
20 Self::Struct(_) => StructIr::NAMESPACE,
21 Self::Enum(_) => EnumIr::NAMESPACE,
22 Self::Service(_) => ServiceIr::NAMESPACE,
23 Self::Newtype(_) => NewtypeIr::NAMESPACE,
24 }
25 }
26
27 pub fn lexical_id(&self) -> LexicalId {
28 match self {
29 Self::BuiltIn(ty) => ty.lexical_id(),
30 Self::Struct(ty) => ty.lexical_id(),
31 Self::Enum(ty) => ty.lexical_id(),
32 Self::Service(ty) => ty.lexical_id(),
33 Self::Newtype(ty) => ty.lexical_id(),
34 }
35 }
36
37 pub fn as_built_in(&self) -> Option<BuiltInTypeIr> {
38 match self {
39 Self::BuiltIn(ty) => Some(*ty),
40 _ => None,
41 }
42 }
43
44 pub fn as_struct(&self) -> Option<&StructIr> {
45 match self {
46 Self::Struct(ty) => Some(ty),
47 _ => None,
48 }
49 }
50
51 pub fn as_enum(&self) -> Option<&EnumIr> {
52 match self {
53 Self::Enum(ty) => Some(ty),
54 _ => None,
55 }
56 }
57
58 pub fn as_service(&self) -> Option<&ServiceIr> {
59 match self {
60 Self::Service(ty) => Some(ty),
61 _ => None,
62 }
63 }
64
65 pub fn as_newtype(&self) -> Option<&NewtypeIr> {
66 match self {
67 Self::Newtype(ty) => Some(ty),
68 _ => None,
69 }
70 }
71}
72
73impl From<BuiltInTypeIr> for LayoutIr {
74 fn from(ty: BuiltInTypeIr) -> Self {
75 Self::BuiltIn(ty)
76 }
77}
78
79impl From<StructIr> for LayoutIr {
80 fn from(ty: StructIr) -> Self {
81 Self::Struct(ty)
82 }
83}
84
85impl From<EnumIr> for LayoutIr {
86 fn from(ty: EnumIr) -> Self {
87 Self::Enum(ty)
88 }
89}
90
91impl From<ServiceIr> for LayoutIr {
92 fn from(ty: ServiceIr) -> Self {
93 Self::Service(ty)
94 }
95}
96
97impl From<NewtypeIr> for LayoutIr {
98 fn from(ty: NewtypeIr) -> Self {
99 Self::Newtype(ty)
100 }
101}
102
103#[derive(IntoPrimitive, TryFromPrimitive)]
104#[repr(u32)]
105enum LayoutVariant {
106 BuiltIn = 0,
107 Struct = 1,
108 Enum = 2,
109 Service = 3,
110 Newtype = 4,
111}
112
113impl Tag for LayoutIr {}
114
115impl PrimaryTag for LayoutIr {
116 type Tag = Self;
117}
118
119impl Serialize<LayoutIr> for &LayoutIr {
120 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
121 match self {
122 LayoutIr::BuiltIn(ty) => {
123 serializer.serialize_enum::<BuiltInTypeIr>(LayoutVariant::BuiltIn, ty)
124 }
125
126 LayoutIr::Struct(ty) => {
127 serializer.serialize_enum::<StructIr>(LayoutVariant::Struct, ty)
128 }
129
130 LayoutIr::Enum(ty) => serializer.serialize_enum::<EnumIr>(LayoutVariant::Enum, ty),
131
132 LayoutIr::Service(ty) => {
133 serializer.serialize_enum::<ServiceIr>(LayoutVariant::Service, ty)
134 }
135
136 LayoutIr::Newtype(ty) => {
137 serializer.serialize_enum::<NewtypeIr>(LayoutVariant::Newtype, ty)
138 }
139 }
140 }
141}