1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt;
use std::str;

use crate::Error;

/// Possible battery technologies.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Technology {
    Unknown,
    LithiumIon,
    LeadAcid,
    LithiumPolymer,
    NickelMetalHydride,
    NickelCadmium,
    NickelZinc,
    LithiumIronPhosphate,
    RechargeableAlkalineManganese,

    // Awaiting for https://github.com/rust-lang/rust/issues/44109
    #[doc(hidden)]
    __Nonexhaustive,
}

impl str::FromStr for Technology {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let tech = match s {
            _ if s.eq_ignore_ascii_case("li-i") => Technology::LithiumIon,
            _ if s.eq_ignore_ascii_case("li-ion") => Technology::LithiumIon,
            _ if s.eq_ignore_ascii_case("lion") => Technology::LithiumIon,
            _ if s.eq_ignore_ascii_case("pb") => Technology::LeadAcid,
            _ if s.eq_ignore_ascii_case("pbac") => Technology::LeadAcid,
            _ if s.eq_ignore_ascii_case("lip") => Technology::LithiumPolymer,
            _ if s.eq_ignore_ascii_case("lipo") => Technology::LithiumPolymer,
            _ if s.eq_ignore_ascii_case("li-poly") => Technology::LithiumPolymer,
            _ if s.eq_ignore_ascii_case("nimh") => Technology::NickelMetalHydride,
            _ if s.eq_ignore_ascii_case("nicd") => Technology::NickelCadmium,
            _ if s.eq_ignore_ascii_case("nizn") => Technology::NickelZinc,
            _ if s.eq_ignore_ascii_case("life") => Technology::LithiumIronPhosphate,
            _ if s.eq_ignore_ascii_case("ram") => Technology::RechargeableAlkalineManganese,
            // TODO: warn!
            _ => Technology::Unknown,
        };

        Ok(tech)
    }
}

impl fmt::Display for Technology {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let display = match self {
            Technology::Unknown => "unknown",
            Technology::LithiumIon => "lithium-ion",
            Technology::LeadAcid => "lead-acid",
            Technology::LithiumPolymer => "lithium-polymer",
            Technology::NickelMetalHydride => "nickel-metal-hydride",
            Technology::NickelCadmium => "nickel-cadmium",
            Technology::NickelZinc => "nickel-zinc",
            Technology::LithiumIronPhosphate => "lithium-iron-phosphate",
            Technology::RechargeableAlkalineManganese => "rechargeable-alkaline-manganese",
            _ => "unknown",
        };

        write!(f, "{}", display)
    }
}

impl Default for Technology {
    fn default() -> Self {
        Technology::Unknown
    }
}