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
use std::{error::Error, fmt, str::FromStr};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Whether the output represents a right handed or left handed neck style
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Handedness {
    #[default]
    Right,
    Left,
}

/// An error occurred parsing the neck's Handedness from a str
#[derive(Debug)]
pub struct ParseHandednessError;

impl fmt::Display for ParseHandednessError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Parse Handedness Error")
    }
}

impl Error for ParseHandednessError {}

impl FromStr for Handedness {
    type Err = ParseHandednessError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "right" | "Right" => Ok(Self::Right),
            "left" | "Left" => Ok(Self::Left),
            _ => Err(ParseHandednessError),
        }
    }
}

impl fmt::Display for Handedness {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Right => "right",
                Self::Left => "left",
            }
        )
    }
}