use std::{fmt, num::ParseIntError, str::FromStr};
pub type SlaveId = u8;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Slave(pub SlaveId);
impl Slave {
#[must_use]
pub const fn broadcast() -> Self {
Slave(0)
}
#[must_use]
pub const fn min_device() -> Self {
Slave(1)
}
#[must_use]
pub const fn max_device() -> Self {
Slave(247)
}
#[must_use]
pub const fn tcp_device() -> Self {
Slave(255)
}
#[must_use]
pub fn is_broadcast(self) -> bool {
self == Self::broadcast()
}
#[must_use]
pub fn is_single_device(self) -> bool {
self >= Self::min_device() && self <= Self::max_device()
}
#[must_use]
pub fn is_reserved(self) -> bool {
self > Self::max_device()
}
}
impl From<SlaveId> for Slave {
fn from(from: SlaveId) -> Self {
Slave(from)
}
}
impl From<Slave> for SlaveId {
fn from(from: Slave) -> Self {
from.0
}
}
impl FromStr for Slave {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let slave_id = match s.parse::<u8>() {
Ok(slave_id) => Ok(slave_id),
Err(err) => {
if let Some(stripped) = s.strip_prefix("0x") {
u8::from_str_radix(stripped, 16)
} else {
Err(err)
}
}
}?;
Ok(Slave(slave_id))
}
}
impl fmt::Display for Slave {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (0x{:0>2X})", self.0, self.0)
}
}
pub trait SlaveContext {
fn set_slave(&mut self, slave: Slave);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_dec() {
assert_eq!(Slave(0), Slave::from_str("0").unwrap());
assert_eq!(Slave(123), Slave::from_str("123").unwrap());
assert_eq!(Slave(255), Slave::from_str("255").unwrap());
assert!(Slave::from_str("-1").is_err());
assert!(Slave::from_str("256").is_err());
}
#[test]
fn parse_hex() {
assert_eq!(Slave(0), Slave::from_str("0x00").unwrap());
assert_eq!(Slave(123), Slave::from_str("0x7b").unwrap());
assert_eq!(Slave(123), Slave::from_str("0x7B").unwrap());
assert_eq!(Slave(255), Slave::from_str("0xff").unwrap());
assert_eq!(Slave(255), Slave::from_str("0xFF").unwrap());
assert!(Slave::from_str("0X00").is_err());
assert!(Slave::from_str("0x100").is_err());
assert!(Slave::from_str("0xfff").is_err());
assert!(Slave::from_str("0xFFF").is_err());
}
#[test]
fn format() {
assert!(format!("{}", Slave(123)).contains("123"));
assert!(format!("{}", Slave(0x7B)).contains("0x7B"));
assert!(!format!("{}", Slave(0x7B)).contains("0x7b"));
}
}