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
use serde::{Deserialize, Serialize};
use std::fmt::Display;

use crate::prelude::Bar;

// https://www.masterclass.com/articles/songwriting-101-learn-common-song-structures
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum SectionKind {
    Ready,
    Intro,
    Verse,
    Chorus,
    Bridge,
    Outro,
    PreChorus,
    Solo,
    Custom(String),
}
impl Display for SectionKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}
impl SectionKind {
    pub fn from_ident(ident: &str) -> Self {
        match ident {
            "Ready" => Self::Ready,
            "Intro" => Self::Intro,
            "Verse" => Self::Verse,
            "Chorus" => Self::Chorus,
            "Bridge" => Self::Bridge,
            "Outro" => Self::Outro,
            "PreChorus" => Self::PreChorus,
            "Solo" => Self::Solo,
            _ => Self::Custom(ident.to_string()),
        }
    }
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Section {
    pub id: String,
    pub kind: SectionKind,
    pub bars: Vec<Bar>,
}
impl Display for Section {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "<Section>({} <{}> B:{})",
            self.id,
            self.kind,
            self.bars.len()
        )
    }
}
impl Section {
    pub const READY_ID: &'static str = "READY";
    pub fn new(id: String, kind: SectionKind, bars: Vec<Bar>) -> Self {
        Self { id, kind, bars }
    }
    pub fn new_ready() -> Self {
        let mut bars = Vec::new();
        bars.push(Bar { layers: Vec::new() });
        Self::new(Self::READY_ID.to_string(), SectionKind::Ready, bars)
    }
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Form {
    pub sections: Vec<String>,
}
impl Display for Form {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<Form>(S:{})", self.sections.len())
    }
}
impl From<Vec<String>> for Form {
    fn from(v: Vec<String>) -> Self {
        Self { sections: v }
    }
}
impl From<Vec<&str>> for Form {
    fn from(v: Vec<&str>) -> Self {
        Self {
            sections: v.iter().map(|x| x.to_string()).collect(),
        }
    }
}