codes_iso_10383/
status.rs

1use codes_common::error::{invalid_length, unknown_value};
2use std::fmt;
3use std::str::FromStr;
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8// ------------------------------------------------------------------------------------------------
9// Public Types
10// ------------------------------------------------------------------------------------------------
11
12///
13///
14///
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
17pub enum Status {
18    /// Currently active
19    Active,
20    /// Expired, or deactivated
21    Expired,
22    /// Updated since last publication
23    Updated,
24}
25
26// ------------------------------------------------------------------------------------------------
27// Implementations
28// ------------------------------------------------------------------------------------------------
29
30impl fmt::Display for Status {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}", self.as_ref())
33    }
34}
35
36impl FromStr for Status {
37    type Err = super::MarketIdCodeError;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        if s.len() != 4 {
41            Err(invalid_length("Status", s.len()))
42        } else {
43            match s {
44                "ACTIVE" => Ok(Self::Active),
45                "EXPIRED" => Ok(Self::Expired),
46                "UPDATED" => Ok(Self::Updated),
47                _ => Err(unknown_value("Status", s)),
48            }
49        }
50    }
51}
52
53impl AsRef<str> for Status {
54    fn as_ref(&self) -> &str {
55        match self {
56            Self::Active => "ACTIVE",
57            Self::Expired => "EXPIRED",
58            Self::Updated => "UPDATED",
59        }
60    }
61}
62
63impl From<Status> for String {
64    fn from(v: Status) -> Self {
65        v.as_ref().to_string()
66    }
67}