tpt_protocol_tup/
quality.rs1use core::fmt;
2use core::str::FromStr;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum Quality {
7 Good,
8 Bad,
9 Stale,
10 Uncertain,
11}
12
13impl Quality {
14 pub fn as_str(&self) -> &'static str {
15 match self {
16 Self::Good => "good",
17 Self::Bad => "bad",
18 Self::Stale => "stale",
19 Self::Uncertain => "uncertain",
20 }
21 }
22
23 pub fn from_str_opt(s: &str) -> Option<Self> {
24 Self::from_str_inner(s)
25 }
26
27 fn from_str_inner(s: &str) -> Option<Self> {
28 match s {
29 "good" => Some(Self::Good),
30 "bad" => Some(Self::Bad),
31 "stale" => Some(Self::Stale),
32 "uncertain" => Some(Self::Uncertain),
33 _ => None,
34 }
35 }
36}
37
38impl FromStr for Quality {
39 type Err = ();
40
41 fn from_str(s: &str) -> Result<Self, Self::Err> {
42 Self::from_str_inner(s).ok_or(())
43 }
44}
45
46impl fmt::Display for Quality {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 f.write_str(self.as_str())
49 }
50}