aldrin_core/introspection/ir/
service.rs

1use super::{EventFallbackIr, EventIr, FunctionFallbackIr, FunctionIr, LexicalId};
2use crate::tags::{self, PrimaryTag, Tag};
3use crate::{Serialize, SerializeError, Serializer, ServiceUuid};
4use num_enum::{IntoPrimitive, TryFromPrimitive};
5use std::collections::BTreeMap;
6use uuid::{uuid, Uuid};
7
8#[derive(Debug, Clone)]
9pub struct ServiceIr {
10    pub(crate) schema: String,
11    pub(crate) name: String,
12    pub(crate) doc: Option<String>,
13    pub(crate) uuid: ServiceUuid,
14    pub(crate) version: u32,
15    pub(crate) functions: BTreeMap<u32, FunctionIr>,
16    pub(crate) events: BTreeMap<u32, EventIr>,
17    pub(crate) function_fallback: Option<FunctionFallbackIr>,
18    pub(crate) event_fallback: Option<EventFallbackIr>,
19}
20
21impl ServiceIr {
22    pub const NAMESPACE: Uuid = uuid!("de06b048-55f7-43b9-8d34-555795c2f4c6");
23
24    pub fn builder(
25        schema: impl Into<String>,
26        name: impl Into<String>,
27        uuid: ServiceUuid,
28        version: u32,
29    ) -> ServiceIrBuilder {
30        ServiceIrBuilder::new(schema, name, uuid, version)
31    }
32
33    pub fn lexical_id(&self) -> LexicalId {
34        LexicalId::service(&self.schema, &self.name)
35    }
36
37    pub fn schema(&self) -> &str {
38        &self.schema
39    }
40
41    pub fn name(&self) -> &str {
42        &self.name
43    }
44
45    pub fn doc(&self) -> Option<&str> {
46        self.doc.as_deref()
47    }
48
49    pub fn uuid(&self) -> ServiceUuid {
50        self.uuid
51    }
52
53    pub fn version(&self) -> u32 {
54        self.version
55    }
56
57    pub fn functions(&self) -> &BTreeMap<u32, FunctionIr> {
58        &self.functions
59    }
60
61    pub fn events(&self) -> &BTreeMap<u32, EventIr> {
62        &self.events
63    }
64
65    pub fn function_fallback(&self) -> Option<&FunctionFallbackIr> {
66        self.function_fallback.as_ref()
67    }
68
69    pub fn event_fallback(&self) -> Option<&EventFallbackIr> {
70        self.event_fallback.as_ref()
71    }
72}
73
74#[derive(IntoPrimitive, TryFromPrimitive)]
75#[repr(u32)]
76enum ServiceField {
77    Schema = 0,
78    Name = 1,
79    Uuid = 2,
80    Version = 3,
81    Functions = 4,
82    Events = 5,
83    FunctionFallback = 6,
84    EventFallback = 7,
85}
86
87impl Tag for ServiceIr {}
88
89impl PrimaryTag for ServiceIr {
90    type Tag = Self;
91}
92
93impl Serialize<ServiceIr> for &ServiceIr {
94    fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
95        let mut serializer = serializer.serialize_struct2()?;
96
97        serializer.serialize::<tags::String>(ServiceField::Schema, &self.schema)?;
98        serializer.serialize::<tags::String>(ServiceField::Name, &self.name)?;
99        serializer.serialize::<ServiceUuid>(ServiceField::Uuid, &self.uuid)?;
100        serializer.serialize::<tags::U32>(ServiceField::Version, &self.version)?;
101
102        serializer.serialize::<tags::Map<tags::U32, FunctionIr>>(
103            ServiceField::Functions,
104            &self.functions,
105        )?;
106
107        serializer
108            .serialize::<tags::Map<tags::U32, EventIr>>(ServiceField::Events, &self.events)?;
109
110        serializer.serialize_if_some::<tags::Option<FunctionFallbackIr>>(
111            ServiceField::FunctionFallback,
112            &self.function_fallback,
113        )?;
114
115        serializer.serialize_if_some::<tags::Option<EventFallbackIr>>(
116            ServiceField::EventFallback,
117            &self.event_fallback,
118        )?;
119
120        serializer.finish()
121    }
122}
123
124#[derive(Debug, Clone)]
125pub struct ServiceIrBuilder {
126    schema: String,
127    name: String,
128    doc: Option<String>,
129    uuid: ServiceUuid,
130    version: u32,
131    functions: BTreeMap<u32, FunctionIr>,
132    events: BTreeMap<u32, EventIr>,
133    function_fallback: Option<FunctionFallbackIr>,
134    event_fallback: Option<EventFallbackIr>,
135}
136
137impl ServiceIrBuilder {
138    pub fn new(
139        schema: impl Into<String>,
140        name: impl Into<String>,
141        uuid: ServiceUuid,
142        version: u32,
143    ) -> Self {
144        Self {
145            schema: schema.into(),
146            name: name.into(),
147            doc: None,
148            uuid,
149            version,
150            functions: BTreeMap::new(),
151            events: BTreeMap::new(),
152            function_fallback: None,
153            event_fallback: None,
154        }
155    }
156
157    pub fn doc(mut self, doc: impl Into<String>) -> Self {
158        self.doc = Some(doc.into());
159        self
160    }
161
162    pub fn function(mut self, function: FunctionIr) -> Self {
163        self.functions.insert(function.id(), function);
164        self
165    }
166
167    pub fn event(mut self, event: EventIr) -> Self {
168        self.events.insert(event.id(), event);
169        self
170    }
171
172    pub fn function_fallback(mut self, fallback: FunctionFallbackIr) -> Self {
173        self.function_fallback = Some(fallback);
174        self
175    }
176
177    pub fn event_fallback(mut self, fallback: EventFallbackIr) -> Self {
178        self.event_fallback = Some(fallback);
179        self
180    }
181
182    pub fn finish(self) -> ServiceIr {
183        ServiceIr {
184            schema: self.schema,
185            name: self.name,
186            doc: self.doc,
187            uuid: self.uuid,
188            version: self.version,
189            functions: self.functions,
190            events: self.events,
191            function_fallback: self.function_fallback,
192            event_fallback: self.event_fallback,
193        }
194    }
195}