Repo of no-std crates for my personal embedded projects
1#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Copy, Clone)]
2#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3#[repr(u64)]
4pub enum MessageKind {
5 {{#each this}}
6 {{ name }} = {{ id }},
7 {{/each}}
8}
9
10#[derive(Debug, Clone)]
11#[cfg_attr(feature = "defmt", derive(defmt::Format))]
12pub enum Message {
13 {{#each this}}
14 {{ name }}({{ name }}),
15 {{/each}}
16}
17
18impl Message {
19 pub fn decode(message_kind: MessageKind, buffer: &mut &[u8]) -> Result<Self, prost::DecodeError> {
20 match message_kind {
21 {{#each this}}
22 MessageKind::{{ name }} => Ok(Self::{{ name }}({{name}}::decode(buffer)?)),
23 {{/each}}
24 }
25 }
26
27 pub fn encoded_len(&self) -> usize {
28 match self {
29 {{#each this}}
30 Self::{{ name }}(message) => message.encoded_len(),
31 {{/each}}
32 }
33 }
34
35 pub fn encode(&self, buf: &mut impl prost::bytes::BufMut) -> Result<(), prost::EncodeError> {
36 match self {
37 {{#each this}}
38 Self::{{ name }}(message) => message.encode(buf),
39 {{/each}}
40 }
41 }
42}
43
44impl From<&Message> for MessageKind {
45 fn from(value: &Message) -> Self {
46 match value {
47 {{#each this}}
48 Message::{{ name }}(message) => Self::from(message),
49 {{/each}}
50 }
51 }
52}
53
54{{#each this}}
55impl From<&{{ name }}> for MessageKind {
56 fn from(_value: &{{name}}) -> Self {
57 Self::{{name}}
58 }
59}
60
61impl From<{{ name }}> for Message {
62 fn from(value: {{ name }}) -> Self {
63 Message::{{ name }}(value)
64 }
65}
66{{/each}}