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
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use thiserror::Error;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Polarity {
    Normal,
    Inversed,
}

impl FromStr for Polarity {
    type Err = ParsePolarityError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "normal" => Polarity::Normal,
            "inversed" => Polarity::Inversed,
            _ => return Err(ParsePolarityError),
        })
    }
}

#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
#[error("provided string was not `normal` or `inversed`")]
pub struct ParsePolarityError;

impl Display for Polarity {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Polarity::Normal => "normal",
            Polarity::Inversed => "inversed",
        }
        .fmt(f)
    }
}