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
//! Contains a macro that's used internally to generate the same code for both `Fingering` and `Layer`.

#[macro_export]
macro_rules! impl_keyboard {
    ($type:ty, $ret:ty, $alias:ident) => {
        impl $type {
            pub fn rows(&self) -> impl Iterator<Item = &Vec<$ret>> {
                self.0.iter()
            }
            pub fn keys(&self) -> impl Iterator<Item = &$ret> {
                self.rows().flatten()
            }
            pub fn shape(&self) -> Shape {
                self.rows().map(|r| r.len()).collect::<Vec<_>>().into()
            }
        }

        impl From<Vec<Vec<$ret>>> for $type {
            fn from(f: Vec<Vec<$ret>>) -> Self {
                Self(f)
            }
        }

        serde_conv!(
            $alias,
            Vec<$ret>,
            |row: &Vec<$ret>| {
                if row.len() == 0 {
                    String::new()
                } else {
                    row.into_iter()
                        .take(row.len() - 1)
                        .map(|e| format!("{e} "))
                        .chain([row.last().unwrap().to_string()])
                        .collect::<String>()
                }
            },
            |line: String| {
                line.split_whitespace()
                    .map(|s| s.parse::<$ret>().map_err(Into::into))
                    .collect::<Result<Vec<_>, crate::dofinitions::DofinitionError>>()
            }
        );
    };
}