notation_model/
section.rs

1use fehler::throws;
2use std::fmt::Display;
3use std::sync::{Arc, Weak};
4
5use crate::prelude::{Bar, ParseError, SectionKind, Tab, Track};
6
7#[derive(Debug)]
8pub struct Section {
9    pub tab: Weak<Tab>,
10    pub index: usize,
11    pub kind: SectionKind,
12    pub id: String,
13    pub bars: Vec<Arc<Bar>>,
14}
15impl Display for Section {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        write!(
18            f,
19            "<Section>({} <{}> {} B:{})",
20            self.index,
21            self.kind,
22            self.id,
23            self.bars.len()
24        )
25    }
26}
27impl Section {
28    pub fn new(
29        tab: Weak<Tab>,
30        index: usize,
31        kind: SectionKind,
32        id: String,
33        bars: Vec<Arc<Bar>>,
34    ) -> Self {
35        Self {
36            tab,
37            id,
38            kind,
39            bars,
40            index,
41        }
42    }
43}
44
45impl Section {
46    #[throws(ParseError)]
47    pub fn try_new(
48        tab: Weak<Tab>,
49        index: usize,
50        proto: notation_proto::prelude::Section,
51        tracks: &Vec<Arc<Track>>,
52    ) -> Self {
53        let mut bars = Vec::new();
54        for (bar_index, bar) in proto.bars.into_iter().enumerate() {
55            bars.push(Bar::try_new(bar_index, bar, tracks).map(Arc::new)?);
56        }
57        Self::new(tab, index, proto.kind, proto.id, bars)
58    }
59}