use alloc::vec::Vec;
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct OptFlds(u16);
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct TrgOps(u8);
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct ReasonCode(u8);
macro_rules! packed_bits {
($t:ty, $width:expr, $($name:ident = $bit:expr, $set:ident, $doc:expr;)+) => {
impl $t {
pub const NONE: $t = Self(0);
pub const BITS: usize = $width;
$(
#[doc = $doc]
pub const fn $name(self) -> bool {
self.0 & (1 << $bit) != 0
}
#[doc = concat!("Set [`Self::", stringify!($name), "`].")]
#[must_use]
pub const fn $set(mut self, on: bool) -> Self {
if on { self.0 |= 1 << $bit } else { self.0 &= !(1 << $bit) }
self
}
)+
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub fn from_bit_string(bytes: &[u8]) -> Self {
let mut v = 0u16;
for bit in 0..$width {
let (octet, shift) = (bit / 8, 7 - (bit % 8));
if bytes.get(octet).is_some_and(|b| b >> shift & 1 == 1) {
v |= 1 << bit;
}
}
#[allow(clippy::cast_possible_truncation)]
Self(v as _)
}
pub fn to_bit_string(self) -> (u8, Vec<u8>) {
let octets = usize::div_ceil($width, 8);
let mut out = alloc::vec![0u8; octets];
for bit in 0..$width {
if u16::from(self.0) & (1 << bit) != 0 {
let (octet, shift) = (bit / 8, 7 - (bit % 8));
if let Some(b) = out.get_mut(octet) {
*b |= 1 << shift;
}
}
}
(((octets * 8) - $width) as u8, out)
}
}
impl core::fmt::Debug for $t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(stringify!($t))?;
f.write_str("(")?;
let mut first = true;
$(
if self.$name() {
if !first {
f.write_str(" | ")?;
}
first = false;
f.write_str(stringify!($name))?;
}
)+
if first {
f.write_str("none")?;
}
f.write_str(")")
}
}
};
}
packed_bits!(OptFlds, 10,
sequence_number = 1, with_sequence_number, "`SqNum` is present.";
report_time_stamp = 2, with_report_time_stamp, "`TimeOfEntry` is present.";
reason_for_inclusion = 3, with_reason_for_inclusion, "A `ReasonCode` follows every value.";
data_set_name = 4, with_data_set_name, "`DatSet` is present.";
data_reference = 5, with_data_reference, "A reference precedes every value.";
buffer_overflow = 6, with_buffer_overflow, "`BufOvfl` is present (buffered control blocks).";
entry_id = 7, with_entry_id, "`EntryID` is present (buffered control blocks).";
conf_revision = 8, with_conf_revision, "`ConfRev` is present.";
segmentation = 9, with_segmentation, "`SubSeqNum` and `MoreSegmentsFollow` are present.";
);
packed_bits!(TrgOps, 6,
data_change = 1, with_data_change, "Report when a value changes.";
quality_change = 2, with_quality_change, "Report when a quality changes.";
data_update = 3, with_data_update, "Report on every update, changed or not.";
integrity = 4, with_integrity, "Report periodically, every `IntgPd` milliseconds.";
general_interrogation = 5, with_general_interrogation, "Report when the client asks (`GI`).";
);
packed_bits!(ReasonCode, 6,
data_change = 1, with_data_change, "A value changed.";
quality_change = 2, with_quality_change, "A quality changed.";
data_update = 3, with_data_update, "The value was updated without changing.";
integrity = 4, with_integrity, "The integrity period elapsed.";
general_interrogation = 5, with_general_interrogation, "The client asked (`GI`).";
);
impl TrgOps {
pub const EVENTS: TrgOps = TrgOps(0b0010_0110);
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ControlModel {
StatusOnly,
#[default]
DirectNormal,
SboNormal,
DirectEnhanced,
SboEnhanced,
}
impl ControlModel {
pub const fn from_code(code: i64) -> Option<ControlModel> {
Some(match code {
0 => ControlModel::StatusOnly,
1 => ControlModel::DirectNormal,
2 => ControlModel::SboNormal,
3 => ControlModel::DirectEnhanced,
4 => ControlModel::SboEnhanced,
_ => return None,
})
}
pub const fn to_code(self) -> i64 {
match self {
ControlModel::StatusOnly => 0,
ControlModel::DirectNormal => 1,
ControlModel::SboNormal => 2,
ControlModel::DirectEnhanced => 3,
ControlModel::SboEnhanced => 4,
}
}
pub const fn needs_select(self) -> bool {
matches!(self, ControlModel::SboNormal | ControlModel::SboEnhanced)
}
pub const fn select_carries_value(self) -> bool {
matches!(self, ControlModel::SboEnhanced)
}
pub const fn enhanced_security(self) -> bool {
matches!(self, ControlModel::DirectEnhanced | ControlModel::SboEnhanced)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn report_bit_strings_have_one_width_in_every_edition() {
assert_eq!(OptFlds::BITS, 10);
assert_eq!(TrgOps::BITS, 6);
assert_eq!(ReasonCode::BITS, 6);
}
}