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
use crate::song::{
    Song,
    Section,
    Line,
    Chunk,
};
use std::slice::Iter;
use std::slice::IterMut;
use std::iter::{Once, once};

impl Song {
    pub fn iter(&self) -> Iter<Section> {
        self.song.iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<Section> {
        self.song.iter_mut()
    }
}

pub enum SectionIterator<'a> {
    Paragraph(Iter<'a, Line>),
    Line(Once<&'a Line>)
}

pub enum SectionMutIterator<'a> {
    Paragraph(IterMut<'a, Line>),
    Line(Once<&'a mut Line>)
}

impl Section {
    pub fn iter(&self) -> SectionIterator {
        match self {
            Section::Chorus(p) => SectionIterator::Paragraph(p.0.iter()),
            Section::Verse(p) => SectionIterator::Paragraph(p.0.iter()),
            Section::Comment(l) => SectionIterator::Line(once(l))
        }
    }

    pub fn iter_mut(&mut self) -> SectionMutIterator {
        match self {
            Section::Chorus(p) => SectionMutIterator::Paragraph(p.0.iter_mut()),
            Section::Verse(p) => SectionMutIterator::Paragraph(p.0.iter_mut()),
            Section::Comment(l) => SectionMutIterator::Line(once(l))
        }
    }
}

impl <'a> Iterator for SectionIterator<'a> {
    type Item = &'a Line;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            SectionIterator::Paragraph(p) => p.next(),
            SectionIterator::Line(l) => l.next()
        }
    }
}

impl <'a> Iterator for SectionMutIterator<'a> {
    type Item = &'a mut Line;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            SectionMutIterator::Paragraph(p) => p.next(),
            SectionMutIterator::Line(l) => l.next()
        }
    }
}

impl Line {
    pub fn iter(&self) -> Iter<Chunk> {
        self.0.iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<Chunk> {
        self.0.iter_mut()
    }
}