1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 {
pub const fn broadcast() -> Self {
Slave(0)
}
pub const fn min_device() -> Self {
Slave(1)
}
pub const fn max_device() -> Self {
Slave(247)
}
pub const fn tcp_device() -> Self {
Slave(255)
}
pub fn is_broadcast(self) -> bool {
self == Self::broadcast()
}
pub fn is_single_device(self) -> bool {
self >= Self::min_device() && self <= Self::max_device()
}
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 u8::from_str_radix(s, 10) {
Ok(slave_id) => Ok(slave_id),
Err(err) => {
if s.starts_with("0x") {
u8::from_str_radix(&s[2..], 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"));
}
}