deimos_shared/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![allow(clippy::needless_range_loop)]
4
5pub mod peripherals;
6pub mod states;
7pub use states::OperatingMetrics;
8
9/// The UDP port on which the hardware peripherals expect to receive packets from the control machine
10pub const PERIPHERAL_RX_PORT: u16 = 12367;
11
12/// The UDP port on which the control machine expects to receive packets from hardware peripherals
13pub const CONTROLLER_RX_PORT: u16 = 12368;
14
15/// Derive To/From with an added "Unknown" variant catch-all for converting
16/// from numerical values that do not match a valid variant in order to
17/// avoid either panicking or cumbersome error handling.
18///
19/// Yoinked shamelessly (with some modification) from smoltcp.
20#[macro_export]
21macro_rules! enum_with_unknown {
22    (
23        $( #[$enum_attr:meta] )*
24        pub enum $name:ident($ty:ty) {
25            $(
26              $( #[$variant_attr:meta] )*
27              $variant:ident = $value:expr
28            ),+ $(,)?
29        }
30    ) => {
31        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
32        $( #[$enum_attr] )*
33        pub enum $name {
34            $(
35              $( #[$variant_attr] )*
36              $variant
37            ),*,
38            /// Catch-all for values that do not match a variant
39            Unknown($ty)
40        }
41
42        impl ::core::convert::From<$ty> for $name {
43            fn from(value: $ty) -> Self {
44                match value {
45                    $( $value => $name::$variant ),*,
46                    other => $name::Unknown(other)
47                }
48            }
49        }
50
51        impl ::core::convert::From<$name> for $ty {
52            fn from(value: $name) -> Self {
53                match value {
54                    $( $name::$variant => $value ),*,
55                    $name::Unknown(other) => other
56                }
57            }
58        }
59    }
60}