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
#[macro_export]
macro_rules! generate_id_type {
($struct_name:ident) => {
#[derive(Hash, Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-support", derive(serde::Serialize))]
pub struct $struct_name(u16);
impl $struct_name {
pub fn new(val: u16) -> Self {
$struct_name(val)
}
pub fn val(&self) -> u16 {
self.0
}
}
impl std::fmt::Display for $struct_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for $struct_name {
type Err = IdError<$struct_name>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val = s.parse::<u16>().map_err(|_e| IdError::CannotParse(s.to_owned()))?;
Ok(Self::new(val))
}
}
impl std::default::Default for $struct_name {
fn default() -> Self {
Self::new(0)
}
}
};
}
#[cfg(test)]
mod tests {
use crate::IdError;
use stepflow_test_util::test_id;
generate_id_type!(TestId);
#[test]
fn new_id() {
let test_id = TestId::new(10);
assert_eq!(test_id.val(), 10);
assert_ne!(test_id.val(), TestId::new(15).val());
}
#[test]
fn test_testid() {
let test_id1: TestId = test_id!(TestId);
let test_id2: TestId = test_id!(TestId);
assert_ne!(test_id1.val(), test_id2.val());
}
#[test]
fn from_str() {
let test_id = "48".parse::<TestId>().unwrap();
assert_eq!(test_id, TestId::new(48));
}
}