Skip to content

Commit ef90458

Browse files
committed
impl FromStr for NeighbourState
1 parent 6dd74c9 commit ef90458

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/neighbour/state.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,35 @@ impl std::fmt::Display for NeighbourState {
7676
}
7777
}
7878
}
79+
80+
impl std::str::FromStr for NeighbourState {
81+
type Err = NeighbourStateParseError;
82+
83+
fn from_str(state: &str) -> Result<Self, Self::Err> {
84+
let state = match state {
85+
"incomplete" => Self::Incomplete,
86+
"reachable" => Self::Reachable,
87+
"stale" => Self::Stale,
88+
"delay" => Self::Delay,
89+
"probe" => Self::Probe,
90+
"failed" => Self::Failed,
91+
"noarp" => Self::Noarp,
92+
"permanent" => Self::Permanent,
93+
"none" => Self::None,
94+
_ => return Err(NeighbourStateParseError),
95+
};
96+
97+
Ok(state)
98+
}
99+
}
100+
101+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102+
pub struct NeighbourStateParseError;
103+
104+
impl std::fmt::Display for NeighbourStateParseError {
105+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106+
write!(f, "invalid neighbour state")
107+
}
108+
}
109+
110+
impl std::error::Error for NeighbourStateParseError {}

0 commit comments

Comments
 (0)