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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum Finger {
    Thumb,
    Index,
    Middle,
    Ring,
    Pinky,
}
impl Display for Finger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", match self {
            Finger::Thumb => "T",
            Finger::Index => "I",
            Finger::Middle => "M",
            Finger::Ring => "R",
            Finger::Pinky => "Y",
        })
    }
}

macro_rules! impl_hand_shape {
    ($type:ident, $strings:literal) => {
        #[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
        pub struct $type {
            pub barre: Option<u8>,
            #[serde(with = "serde_arrays")]
            pub frets: [Option<u8>; $strings],
            #[serde(with = "serde_arrays")]
            pub fingers: [Option<Finger>; $strings],
        }
        impl Display for $type {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "[")?;
                for index in 0..self.frets.len() {
                    let fret = self.frets[self.frets.len() - 1 - index];
                    match fret {
                        Some(fret) => write!(f, "{}", fret)?,
                        None => write!(f, "x")?,
                    }
                }
                write!(f, "]")?;
                Ok(())
            }
        }
        impl Default for $type {
            fn default() -> Self {
                let frets = [Some(0); $strings];
                let fingers = [None; $strings];
                Self::new(frets, fingers)
            }
        }
        impl $type {
            pub fn new_barre(
                barre: u8,
                frets: [Option<u8>; $strings],
                fingers: [Option<Finger>; $strings],
            ) -> Self {
                if barre == 0 {
                    Self {
                        barre: None,
                        frets,
                        fingers,
                    }
                } else {
                    Self {
                        barre: Some(barre),
                        frets,
                        fingers,
                    }
                }
            }
            pub fn new(frets: [Option<u8>; $strings], fingers: [Option<Finger>; $strings]) -> Self {
                Self {
                    barre: None,
                    frets,
                    fingers,
                }
            }
            pub fn barre(&self) -> u8 {
                self.barre.unwrap_or(0)
            }
            pub fn string_fret(&self, string: u8) -> Option<u8> {
                if string == 0 || string as usize > self.frets.len() {
                    None
                } else {
                    self.frets[string as usize - 1]
                }
            }
            pub fn string_fret_with_barre(&self, string: u8) -> Option<u8> {
                self.string_fret(string).map(|x| x + self.barre())
            }
            pub fn max_fret(&self) -> u8 {
                let mut max = 0;
                for index in 0..self.frets.len() {
                    if let Some(fret) = self.frets[index] {
                        if fret > max {
                            max = fret
                        }
                    }
                }
                max
            }
            pub fn max_fret_with_barre(&self) -> u8 {
                self.max_fret() + self.barre()
            }
        }

        impl From<([Option<u8>; $strings], [Option<Finger>; $strings])> for $type {
            fn from(v: ([Option<u8>; $strings], [Option<Finger>; $strings])) -> Self {
                Self::new(v.0, v.1)
            }
        }

        impl From<[Option<u8>; $strings]> for $type {
            fn from(v: [Option<u8>; $strings]) -> Self {
                Self::new(v, [None; $strings])
            }
        }

        impl From<(u8, [Option<u8>; $strings], [Option<Finger>; $strings])> for $type {
            fn from(v: (u8, [Option<u8>; $strings], [Option<Finger>; $strings])) -> Self {
                Self::new_barre(v.0, v.1, v.2)
            }
        }

        impl From<(u8, [Option<u8>; $strings])> for $type {
            fn from(v: (u8, [Option<u8>; $strings])) -> Self {
                Self::new_barre(v.0, v.1, [None; $strings])
            }
        }
    };
}

impl_hand_shape!(HandShape6, 6);
impl_hand_shape!(HandShape4, 4);