machine_check_common/
node_id.rs1use std::fmt::{Debug, Display};
2use std::num::NonZeroU64;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub struct StateId(pub NonZeroU64);
14
15impl Debug for StateId {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 write!(f, "{}", self.0)
18 }
19}
20
21impl Display for StateId {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 <StateId as Debug>::fmt(self, f)
24 }
25}
26
27#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
33pub struct NodeId(Option<NonZeroU64>);
34
35impl NodeId {
36 pub const ROOT: NodeId = NodeId(None);
38}
39
40impl From<StateId> for NodeId {
41 fn from(state_id: StateId) -> Self {
42 NodeId(Some(state_id.0))
43 }
44}
45
46impl TryFrom<NodeId> for StateId {
47 type Error = ();
48
49 fn try_from(value: NodeId) -> Result<Self, ()> {
50 match value.0 {
51 Some(id) => Ok(StateId(id)),
52 None => Err(()),
53 }
54 }
55}
56
57impl Debug for NodeId {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 match self.0 {
60 Some(id) => write!(f, "{}", id),
61 None => write!(f, "0"),
62 }
63 }
64}
65
66impl Display for NodeId {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 <NodeId as Debug>::fmt(self, f)
69 }
70}