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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Contains utilities to generate courses based on the circles of fifths.
//!
//! Suppose that you have a course that contains guitar riffs that you would like to learn in
//! all keys. The utilities in this module allow you to define a course in which the first lesson
//! teaches you the riffs in the key of C (or A minor if the riffs were in a minor key). From this
//! lesson, there are two dependent lessons one for the key of G and another for the key of F,
//! because these keys contain only one sharp or flat respectively. The process repeats until the
//! circle is traversed in both clockwise and counter-clockwise directions.

use anyhow::Result;

use crate::{
    course_builder::{AssetBuilder, CourseBuilder, LessonBuilder},
    data::{music::notes::*, CourseManifest, LessonManifestBuilder},
};

/// Generates a course builder that contains a lesson per key and which follows the circle of
/// fifths, starting with the key of C (which has not flats nor sharps), and continuing in both
/// directions, allowing each lesson to depend on the lesson that comes before in the circle of
/// fifths. This is useful to create courses that teach the same exercises for each key.
pub struct CircleFifthsCourse {
    /// Base name of the directory on which to store this lesson.
    pub directory_name: String,

    /// The manifest for the course.
    pub course_manifest: CourseManifest,

    /// The asset builders for the course.
    pub course_asset_builders: Vec<AssetBuilder>,

    /// An optional closure that returns a different note from the one found by traversing the
    /// circle of fifths. This is useful, for example, to generate a course based on the minor scale
    /// in the correct order by the number of flats or sharps in the scale (i.e., the lesson based
    /// on A minor appears first because it's the relative minor of C major).
    pub note_alias: Option<fn(Note) -> Result<Note>>,

    /// The template used to generate the lesson manifests.
    pub lesson_manifest_template: LessonManifestBuilder,

    /// A closure which generates the builder for each lesson.
    pub lesson_builder_generator: Box<dyn Fn(Note, Option<Note>) -> Result<LessonBuilder>>,

    /// An optional closure which generates extra lessons which do not follow the circle of fifths
    /// pattern.
    pub extra_lessons_generator: Option<Box<dyn Fn() -> Result<Vec<LessonBuilder>>>>,
}

impl CircleFifthsCourse {
    /// Generates the lesson builder for the lesson based on the given note. An optional note is
    /// also provided for the purpose of creating the dependencies of the lesson.
    fn generate_lesson_builder(
        &self,
        note: Note,
        previous_note: Option<Note>,
    ) -> Result<LessonBuilder> {
        let note_alias = match &self.note_alias {
            None => note,
            Some(closure) => closure(note)?,
        };

        let previous_note_alias = match &self.note_alias {
            None => previous_note,
            Some(closure) => match previous_note {
                None => None,
                Some(previous_note) => Some(closure(previous_note)?),
            },
        };

        (self.lesson_builder_generator)(note_alias, previous_note_alias)
    }

    /// Traverses the circle of fifths in counter-clockwise motion and returns a list of the lesson
    /// builders generated along the way.
    fn generate_counter_clockwise(&self, note: Option<Note>) -> Result<Vec<LessonBuilder>> {
        if note.is_none() {
            return Ok(vec![]);
        }

        let note = note.unwrap();
        let mut lessons = vec![self.generate_lesson_builder(note, note.clockwise())?];
        lessons.extend(
            self.generate_counter_clockwise(note.counter_clockwise())?
                .into_iter(),
        );
        Ok(lessons)
    }

    /// Traverses the circle of fifths in clockwise motion and returns a list of the lesson builders
    /// generated along the way.
    fn generate_clockwise(&self, note: Option<Note>) -> Result<Vec<LessonBuilder>> {
        if note.is_none() {
            return Ok(vec![]);
        }

        let note = note.unwrap();
        let mut lessons = vec![self.generate_lesson_builder(note, note.counter_clockwise())?];
        lessons.extend(self.generate_clockwise(note.clockwise())?.into_iter());
        Ok(lessons)
    }

    /// Generates all the lesson builders for the course by starting at the note C and traversing
    /// the circle of fifths in both directions.
    fn generate_lesson_builders(&self) -> Result<Vec<LessonBuilder>> {
        let mut lessons = vec![self.generate_lesson_builder(Note::C, None)?];
        lessons.extend(
            self.generate_counter_clockwise(Note::C.counter_clockwise())?
                .into_iter(),
        );
        lessons.extend(self.generate_clockwise(Note::C.clockwise())?.into_iter());
        if let Some(generator) = &self.extra_lessons_generator {
            lessons.extend(generator()?.into_iter());
        }
        Ok(lessons)
    }

    /// Generates a course builder which contains lessons based on each key.
    pub fn generate_course_builder(&self) -> Result<CourseBuilder> {
        Ok(CourseBuilder {
            directory_name: self.directory_name.clone(),
            course_manifest: self.course_manifest.clone(),
            asset_builders: self.course_asset_builders.clone(),
            lesson_builders: self.generate_lesson_builders()?,
            lesson_manifest_template: self.lesson_manifest_template.clone(),
        })
    }
}