avalanche_types/
node.rs

1//! Defines the node type.
2use serde::{Deserialize, Serialize};
3
4/// Defines the node type.
5/// MUST BE either "anchor" or "non-anchor"
6#[derive(
7    Deserialize,
8    Serialize,
9    std::clone::Clone,
10    std::cmp::Eq,
11    std::cmp::Ord,
12    std::cmp::PartialEq,
13    std::cmp::PartialOrd,
14    std::fmt::Debug,
15    std::hash::Hash,
16)]
17pub enum Kind {
18    #[serde(rename = "anchor")]
19    Anchor,
20    #[serde(rename = "non-anchor")]
21    NonAnchor,
22    Unknown(String),
23}
24
25impl std::convert::From<&str> for Kind {
26    fn from(s: &str) -> Self {
27        match s {
28            "anchor" => Kind::Anchor,
29            "non-anchor" => Kind::NonAnchor,
30            "non_anchor" => Kind::NonAnchor,
31
32            other => Kind::Unknown(other.to_owned()),
33        }
34    }
35}
36
37impl std::str::FromStr for Kind {
38    type Err = std::convert::Infallible;
39
40    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
41        Ok(Kind::from(s))
42    }
43}
44
45impl Kind {
46    /// Returns the `&str` value of the enum member.
47    pub fn as_str(&self) -> &str {
48        match self {
49            Kind::Anchor => "anchor",
50            Kind::NonAnchor => "non-anchor",
51
52            Kind::Unknown(s) => s.as_ref(),
53        }
54    }
55
56    /// Returns all the `&str` values of the enum members.
57    pub fn values() -> &'static [&'static str] {
58        &[
59            "anchor",     //
60            "non-anchor", //
61        ]
62    }
63}
64
65impl AsRef<str> for Kind {
66    fn as_ref(&self) -> &str {
67        self.as_str()
68    }
69}