aldrin_core/introspection/
layout.rs1use super::{BuiltInType, Enum, LexicalId, Service, Struct};
2use crate::error::{DeserializeError, SerializeError};
3use crate::value_deserializer::{Deserialize, Deserializer};
4use crate::value_serializer::{Serialize, Serializer};
5use num_enum::{IntoPrimitive, TryFromPrimitive};
6use uuid::Uuid;
7
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum Layout {
10 BuiltIn(BuiltInType),
11 Struct(Struct),
12 Enum(Enum),
13 Service(Service),
14}
15
16impl Layout {
17 pub fn namespace(&self) -> Uuid {
18 match self {
19 Self::BuiltIn(_) => BuiltInType::NAMESPACE,
20 Self::Struct(_) => Struct::NAMESPACE,
21 Self::Enum(_) => Enum::NAMESPACE,
22 Self::Service(_) => Service::NAMESPACE,
23 }
24 }
25
26 pub fn lexical_id(&self) -> LexicalId {
27 match self {
28 Self::BuiltIn(ty) => ty.lexical_id(),
29 Self::Struct(ty) => ty.lexical_id(),
30 Self::Enum(ty) => ty.lexical_id(),
31 Self::Service(ty) => ty.lexical_id(),
32 }
33 }
34
35 pub fn as_built_in(&self) -> Option<BuiltInType> {
36 match self {
37 Self::BuiltIn(ty) => Some(*ty),
38 _ => None,
39 }
40 }
41
42 pub fn as_struct(&self) -> Option<&Struct> {
43 match self {
44 Self::Struct(ty) => Some(ty),
45 _ => None,
46 }
47 }
48
49 pub fn as_enum(&self) -> Option<&Enum> {
50 match self {
51 Self::Enum(ty) => Some(ty),
52 _ => None,
53 }
54 }
55
56 pub fn as_service(&self) -> Option<&Service> {
57 match self {
58 Self::Service(ty) => Some(ty),
59 _ => None,
60 }
61 }
62}
63
64impl From<BuiltInType> for Layout {
65 fn from(ty: BuiltInType) -> Self {
66 Self::BuiltIn(ty)
67 }
68}
69
70impl From<Struct> for Layout {
71 fn from(ty: Struct) -> Self {
72 Self::Struct(ty)
73 }
74}
75
76impl From<Enum> for Layout {
77 fn from(ty: Enum) -> Self {
78 Self::Enum(ty)
79 }
80}
81
82impl From<Service> for Layout {
83 fn from(ty: Service) -> Self {
84 Self::Service(ty)
85 }
86}
87
88#[derive(IntoPrimitive, TryFromPrimitive)]
89#[repr(u32)]
90enum LayoutVariant {
91 BuiltIn = 0,
92 Struct = 1,
93 Enum = 2,
94 Service = 3,
95}
96
97impl Serialize for Layout {
98 fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
99 match self {
100 Self::BuiltIn(ty) => serializer.serialize_enum(LayoutVariant::BuiltIn, ty),
101 Self::Struct(ty) => serializer.serialize_enum(LayoutVariant::Struct, ty),
102 Self::Enum(ty) => serializer.serialize_enum(LayoutVariant::Enum, ty),
103 Self::Service(ty) => serializer.serialize_enum(LayoutVariant::Service, ty),
104 }
105 }
106}
107
108impl Deserialize for Layout {
109 fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
110 let deserializer = deserializer.deserialize_enum()?;
111
112 match deserializer.try_variant()? {
113 LayoutVariant::BuiltIn => deserializer.deserialize().map(Self::BuiltIn),
114 LayoutVariant::Struct => deserializer.deserialize().map(Self::Struct),
115 LayoutVariant::Enum => deserializer.deserialize().map(Self::Enum),
116 LayoutVariant::Service => deserializer.deserialize().map(Self::Service),
117 }
118 }
119}