1use crate::names_match;
4
5#[derive(Debug)]
7pub struct Cluster {
8 pub id: u32,
10 pub name: &'static str,
12 pub revision: u16,
14 pub attributes: &'static [Attribute],
16 pub commands: &'static [Command],
18 pub events: &'static [Event],
20 pub responses: &'static [(u32, &'static StructDef)],
22}
23
24impl Cluster {
25 pub fn attribute(&self, id: u32) -> Option<&'static Attribute> {
27 self.attributes
28 .binary_search_by_key(&id, |a| a.id)
29 .ok()
30 .map(|i| &self.attributes[i])
31 }
32
33 pub fn attribute_by_name(&self, name: &str) -> Option<&'static Attribute> {
35 self.attributes.iter().find(|a| names_match(a.name, name))
36 }
37
38 pub fn command(&self, id: u32) -> Option<&'static Command> {
40 self.commands
41 .binary_search_by_key(&id, |c| c.id)
42 .ok()
43 .map(|i| &self.commands[i])
44 }
45
46 pub fn command_by_name(&self, name: &str) -> Option<&'static Command> {
48 self.commands.iter().find(|c| names_match(c.name, name))
49 }
50
51 pub fn event(&self, id: u32) -> Option<&'static Event> {
53 self.events
54 .binary_search_by_key(&id, |e| e.id)
55 .ok()
56 .map(|i| &self.events[i])
57 }
58
59 pub fn event_by_name(&self, name: &str) -> Option<&'static Event> {
61 self.events.iter().find(|e| names_match(e.name, name))
62 }
63
64 pub fn response(&self, id: u32) -> Option<&'static StructDef> {
66 self.responses
67 .iter()
68 .find(|(rid, _)| *rid == id)
69 .map(|(_, s)| *s)
70 }
71}
72
73#[derive(Debug)]
75pub struct Attribute {
76 pub id: u32,
78 pub name: &'static str,
80 pub ty: Type,
82 pub list: bool,
84 pub nullable: bool,
86 pub optional: bool,
88 pub writable: bool,
90 pub timed_write: bool,
92}
93
94impl Attribute {
95 pub const fn shape(&self) -> Shape {
97 Shape {
98 ty: Some(self.ty),
99 list: self.list,
100 }
101 }
102}
103
104#[derive(Debug)]
106pub struct Command {
107 pub id: u32,
109 pub name: &'static str,
111 pub request: Option<&'static StructDef>,
113 pub response: Option<&'static StructDef>,
116 pub timed: bool,
118}
119
120#[derive(Debug)]
122pub struct Event {
123 pub id: u32,
125 pub name: &'static str,
127 pub payload: &'static StructDef,
129}
130
131impl Event {
132 pub const fn shape(&self) -> Shape {
134 Shape {
135 ty: Some(Type::Struct(self.payload)),
136 list: false,
137 }
138 }
139}
140
141#[derive(Debug)]
143pub struct Field {
144 pub tag: u8,
146 pub name: &'static str,
148 pub ty: Type,
150 pub list: bool,
152 pub nullable: bool,
154 pub optional: bool,
156}
157
158impl Field {
159 pub const fn shape(&self) -> Shape {
161 Shape {
162 ty: Some(self.ty),
163 list: self.list,
164 }
165 }
166}
167
168#[derive(Debug)]
170pub struct StructDef {
171 pub name: &'static str,
173 pub fields: &'static [Field],
175 pub fabric_scoped: bool,
177}
178
179impl StructDef {
180 pub fn field(&self, tag: u8) -> Option<&'static Field> {
182 self.fields.iter().find(|f| f.tag == tag)
183 }
184
185 pub fn field_by_name(&self, name: &str) -> Option<&'static Field> {
187 self.fields.iter().find(|f| names_match(f.name, name))
188 }
189
190 pub fn shape(&'static self) -> Shape {
192 Shape {
193 ty: Some(Type::Struct(self)),
194 list: false,
195 }
196 }
197}
198
199#[derive(Debug)]
201pub struct EnumDef {
202 pub name: &'static str,
204 pub values: &'static [(u64, &'static str)],
206}
207
208impl EnumDef {
209 pub fn name_of(&self, value: u64) -> Option<&'static str> {
211 self.values
212 .iter()
213 .find(|(v, _)| *v == value)
214 .map(|(_, n)| *n)
215 }
216
217 pub fn value_of(&self, name: &str) -> Option<u64> {
220 self.values
221 .iter()
222 .find(|(_, n)| {
223 names_match(n, name) || n.strip_prefix('k').is_some_and(|n| names_match(n, name))
224 })
225 .map(|(v, _)| *v)
226 }
227}
228
229#[derive(Debug)]
231pub struct BitmapDef {
232 pub name: &'static str,
234 pub bits: &'static [(u64, &'static str)],
236}
237
238#[derive(Debug, Clone, Copy)]
240pub enum Type {
241 Bool,
243 Uint,
245 Int,
247 Float,
249 Double,
251 String,
253 Octets,
255 Enum(&'static EnumDef),
257 Bitmap(&'static BitmapDef),
259 Struct(&'static StructDef),
261 Unknown(&'static str),
263}
264
265#[derive(Debug, Clone, Copy)]
268pub struct Shape {
269 pub ty: Option<Type>,
271 pub list: bool,
273}
274
275impl Shape {
276 pub const ANY: Shape = Shape {
278 ty: None,
279 list: false,
280 };
281
282 pub const fn of(ty: Type) -> Shape {
284 Shape {
285 ty: Some(ty),
286 list: false,
287 }
288 }
289}