battery/types/
technology.rs

1use std::fmt;
2use std::str;
3
4use crate::Error;
5
6/// Possible battery technologies.
7#[derive(Debug, Eq, PartialEq, Copy, Clone)]
8pub enum Technology {
9    Unknown,
10    LithiumIon,
11    LeadAcid,
12    LithiumPolymer,
13    NickelMetalHydride,
14    NickelCadmium,
15    NickelZinc,
16    LithiumIronPhosphate,
17    RechargeableAlkalineManganese,
18
19    // Awaiting for https://github.com/rust-lang/rust/issues/44109
20    #[doc(hidden)]
21    __Nonexhaustive,
22}
23
24impl str::FromStr for Technology {
25    type Err = Error;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        let tech = match s {
29            _ if s.eq_ignore_ascii_case("li-i") => Technology::LithiumIon,
30            _ if s.eq_ignore_ascii_case("li-ion") => Technology::LithiumIon,
31            _ if s.eq_ignore_ascii_case("lion") => Technology::LithiumIon,
32            _ if s.eq_ignore_ascii_case("pb") => Technology::LeadAcid,
33            _ if s.eq_ignore_ascii_case("pbac") => Technology::LeadAcid,
34            _ if s.eq_ignore_ascii_case("lip") => Technology::LithiumPolymer,
35            _ if s.eq_ignore_ascii_case("lipo") => Technology::LithiumPolymer,
36            _ if s.eq_ignore_ascii_case("li-poly") => Technology::LithiumPolymer,
37            _ if s.eq_ignore_ascii_case("nimh") => Technology::NickelMetalHydride,
38            _ if s.eq_ignore_ascii_case("nicd") => Technology::NickelCadmium,
39            _ if s.eq_ignore_ascii_case("nizn") => Technology::NickelZinc,
40            _ if s.eq_ignore_ascii_case("life") => Technology::LithiumIronPhosphate,
41            _ if s.eq_ignore_ascii_case("ram") => Technology::RechargeableAlkalineManganese,
42            // TODO: warn!
43            _ => Technology::Unknown,
44        };
45
46        Ok(tech)
47    }
48}
49
50impl fmt::Display for Technology {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        let display = match self {
53            Technology::Unknown => "unknown",
54            Technology::LithiumIon => "lithium-ion",
55            Technology::LeadAcid => "lead-acid",
56            Technology::LithiumPolymer => "lithium-polymer",
57            Technology::NickelMetalHydride => "nickel-metal-hydride",
58            Technology::NickelCadmium => "nickel-cadmium",
59            Technology::NickelZinc => "nickel-zinc",
60            Technology::LithiumIronPhosphate => "lithium-iron-phosphate",
61            Technology::RechargeableAlkalineManganese => "rechargeable-alkaline-manganese",
62            _ => "unknown",
63        };
64
65        write!(f, "{}", display)
66    }
67}
68
69impl Default for Technology {
70    fn default() -> Self {
71        Technology::Unknown
72    }
73}