notation_proto/
section.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3
4use crate::prelude::Bar;
5
6// https://www.masterclass.com/articles/songwriting-101-learn-common-song-structures
7#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
8pub enum SectionKind {
9    Ready,
10    Intro,
11    Verse,
12    Chorus,
13    Bridge,
14    Outro,
15    PreChorus,
16    Solo,
17    Custom(String),
18}
19impl Display for SectionKind {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        write!(f, "{:?}", self)
22    }
23}
24impl SectionKind {
25    pub fn from_ident(ident: &str) -> Self {
26        match ident {
27            "Ready" => Self::Ready,
28            "Intro" => Self::Intro,
29            "Verse" => Self::Verse,
30            "Chorus" => Self::Chorus,
31            "Bridge" => Self::Bridge,
32            "Outro" => Self::Outro,
33            "PreChorus" => Self::PreChorus,
34            "Solo" => Self::Solo,
35            _ => Self::Custom(ident.to_string()),
36        }
37    }
38}
39
40#[derive(Clone, Serialize, Deserialize, Debug)]
41pub struct Section {
42    pub id: String,
43    pub kind: SectionKind,
44    pub bars: Vec<Bar>,
45}
46impl Display for Section {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(
49            f,
50            "<Section>({} <{}> B:{})",
51            self.id,
52            self.kind,
53            self.bars.len()
54        )
55    }
56}
57impl Section {
58    pub const READY_ID: &'static str = "READY";
59    pub fn new(id: String, kind: SectionKind, bars: Vec<Bar>) -> Self {
60        Self { id, kind, bars }
61    }
62    pub fn new_ready() -> Self {
63        let mut bars = Vec::new();
64        bars.push(Bar { layers: Vec::new() });
65        Self::new(Self::READY_ID.to_string(), SectionKind::Ready, bars)
66    }
67}
68
69#[derive(Clone, Serialize, Deserialize, Debug)]
70pub struct Form {
71    pub sections: Vec<String>,
72}
73impl Display for Form {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        write!(f, "<Form>(S:{})", self.sections.len())
76    }
77}
78impl From<Vec<String>> for Form {
79    fn from(v: Vec<String>) -> Self {
80        Self { sections: v }
81    }
82}
83impl From<Vec<&str>> for Form {
84    fn from(v: Vec<&str>) -> Self {
85        Self {
86            sections: v.iter().map(|x| x.to_string()).collect(),
87        }
88    }
89}