rusmpp_core/values/
congestion_state.rs1use rusmpp_macros::Rusmpp;
2
3#[repr(u8)]
4#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Rusmpp)]
5#[rusmpp(from_into = skip)]
6#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
7#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
8#[cfg_attr(feature = "serde-deserialize-unchecked", derive(::serde::Deserialize))]
9pub enum CongestionState {
10 #[default]
11 Idle,
12 LowLoad(u8),
13 MediumLoad(u8),
14 HighLoad(u8),
15 OptimumLoad(u8),
16 NearingCongestion(u8),
17 Congested,
18 Other(u8),
19}
20
21impl From<u8> for CongestionState {
22 fn from(value: u8) -> Self {
23 match value {
24 0 => Self::Idle,
25 1..=29 => Self::LowLoad(value),
26 30..=49 => Self::MediumLoad(value),
27 50..=79 => Self::HighLoad(value),
28 80..=89 => Self::OptimumLoad(value),
29 90..=99 => Self::NearingCongestion(value),
30 100 => Self::Congested,
31 _ => Self::Other(value),
32 }
33 }
34}
35
36impl From<CongestionState> for u8 {
37 fn from(value: CongestionState) -> Self {
38 match value {
39 CongestionState::Idle => 0,
40 CongestionState::LowLoad(v) => v,
41 CongestionState::MediumLoad(v) => v,
42 CongestionState::HighLoad(v) => v,
43 CongestionState::OptimumLoad(v) => v,
44 CongestionState::NearingCongestion(v) => v,
45 CongestionState::Congested => 100,
46 CongestionState::Other(v) => v,
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn encode_decode() {
57 #[cfg(feature = "alloc")]
58 crate::tests::owned::encode_decode_test_instances::<CongestionState>();
59 crate::tests::borrowed::encode_decode_test_instances::<CongestionState>();
60 }
61}