Skip to content

Commit a22f44a

Browse files
committed
Add slides package builder
1 parent b560372 commit a22f44a

File tree

4 files changed

+178
-3
lines changed

4 files changed

+178
-3
lines changed

Diff for: modmod/src/book.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
};
1313

1414
#[derive(Debug, Default)]
15+
#[non_exhaustive]
1516
pub struct RenderBookError;
1617

1718
impl fmt::Display for RenderBookError {

Diff for: modmod/src/exercises.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::{fmt, path::{PathBuf, Path}};
2+
3+
#[non_exhaustive]
4+
#[derive(Debug, Default)]
5+
pub struct RenderExercisesError;
6+
7+
impl fmt::Display for RenderExercisesError {
8+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9+
f.write_str("unable to render exercises")
10+
}
11+
}
12+
13+
impl error_stack::Context for RenderExercisesError {}
14+
15+
pub struct ExerciseCollection {}
16+
17+
impl ExerciseCollection {
18+
pub fn builder() -> ExerciseCollectionBuilder {
19+
todo!()
20+
}
21+
22+
pub fn render(&self, output_dir: impl AsRef<Path>) -> Result<(), RenderExercisesError> {
23+
todo!();
24+
}
25+
}
26+
27+
pub struct UnitExercises {}
28+
29+
pub struct Exercise {
30+
name: String,
31+
path: PathBuf,
32+
description: PathBuf,
33+
includes: Vec<String>,
34+
}
35+
36+
37+
pub struct ExerciseCollectionBuilder {
38+
units: Vec<UnitExercises>,
39+
}
40+

Diff for: modmod/src/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
mod book;
22
mod io;
33
mod load;
4+
mod exercises;
5+
mod slides;
46

57
use self::{
68
book::{Book, BookBuilder, ChapterBuilder, SectionBuilder},
79
load::{Load, TrackDef},
810
};
911
use error_stack::{IntoReport, Report, Result, ResultExt};
1012
use io::{copy, create_dir_all, create_file, get_dir_content, read_to_string, write_all};
13+
use slides::{SlidesPackage, SlidesPackageBuilder};
1114
use std::{
1215
fmt,
13-
fs::{self},
16+
fs,
1417
path::{Path, PathBuf},
1518
};
1619

@@ -50,10 +53,15 @@ impl Track {
5053

5154
// Render the modules in the track
5255
let mut book_builder = Book::builder(&self.name);
56+
let mut slides_builder = SlidesPackage::builder(self.name);
5357
for (module, index) in self.modules.iter().zip(1..) {
54-
module.render(&mut book_builder, index, output_dir)?;
58+
module.render(&mut book_builder, &mut slides_builder, index, output_dir)?;
5559
}
5660

61+
// Build and render the slides package
62+
let slides_package = slides_builder.build();
63+
slides_package.render(output_dir).change_context(LoadTrackError)?;
64+
5765
// Build and render the exercise book
5866
let book = book_builder.build();
5967
book.render(output_dir).change_context(LoadTrackError)?;
@@ -72,16 +80,18 @@ impl Module {
7280
fn render(
7381
&self,
7482
book_builder: &mut BookBuilder,
83+
slides: &mut SlidesPackageBuilder,
7584
index: i32,
7685
output_dir: impl AsRef<Path>,
7786
) -> Result<(), LoadTrackError> {
7887
let module_tag = to_numbered_tag(&self.name, index);
7988
let module_out_dir = output_dir.as_ref().join(Path::new(&module_tag));
8089
create_dir_all(&module_out_dir)?;
90+
8191
let mut chapter = book_builder.chapter(&self.name);
8292

8393
for (unit, index) in self.units.iter().zip(1..) {
84-
unit.render(&mut chapter, index, &module_out_dir)?;
94+
unit.render(&mut chapter, slides, index, &module_out_dir)?;
8595
}
8696
chapter.add();
8797
Ok(())
@@ -99,9 +109,11 @@ impl Unit {
99109
fn render(
100110
&self,
101111
chapter: &mut ChapterBuilder,
112+
slides: &mut SlidesPackageBuilder,
102113
index: i32,
103114
output_dir: impl AsRef<Path>,
104115
) -> Result<(), LoadTrackError> {
116+
let mut deck = slides.deck(&self.name, self.template.clone());
105117
let mut section = chapter.section(&self.name);
106118
let unit_tag = to_numbered_tag(&self.name, index);
107119
let unit_out_dir = output_dir.as_ref().join(unit_tag);

Diff for: modmod/src/slides.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#![allow(dead_code)]
2+
use std::path::{PathBuf, Path};
3+
use std::fmt;
4+
5+
use error_stack::Result;
6+
7+
8+
#[derive(Debug, Default)]
9+
#[non_exhaustive]
10+
pub struct RenderSlidesError;
11+
12+
impl fmt::Display for RenderSlidesError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("unable to render slides")
15+
}
16+
}
17+
18+
impl error_stack::Context for RenderSlidesError {}
19+
20+
pub struct SlidesPackage {
21+
/// Name of the package, corresponds to the name of the track
22+
name: String,
23+
decks: Vec<SlideDeck>,
24+
}
25+
26+
impl SlidesPackage {
27+
pub fn builder(name: &str) -> SlidesPackageBuilder {
28+
SlidesPackageBuilder {
29+
package: SlidesPackage {
30+
name: name.to_string(),
31+
decks: vec![],
32+
},
33+
}
34+
}
35+
36+
pub fn render(&self, out_dir: impl AsRef<Path>) -> Result<(), RenderSlidesError> {
37+
todo!()
38+
}
39+
}
40+
41+
pub struct SlideDeck {
42+
/// Name of the slide deck, corresponds to the name of the unit in the module
43+
name: String,
44+
template: PathBuf,
45+
sections: Vec<Section>,
46+
}
47+
48+
pub struct Section {
49+
content: PathBuf,
50+
objectives: Vec<String>,
51+
summary: Vec<String>,
52+
further_reading: Vec<String>,
53+
}
54+
55+
pub struct SlidesPackageBuilder {
56+
package: SlidesPackage,
57+
}
58+
59+
impl SlidesPackageBuilder {
60+
pub fn deck(&mut self, name: &str, template: PathBuf) -> SlideDeckBuilder<'_> {
61+
SlideDeckBuilder {
62+
package_builder: self,
63+
slide_deck: SlideDeck {
64+
name: name.to_string(),
65+
template,
66+
sections: vec![],
67+
},
68+
}
69+
}
70+
71+
pub fn build(self) -> SlidesPackage {
72+
self.package
73+
}
74+
}
75+
76+
pub struct SlideDeckBuilder<'p> {
77+
package_builder: &'p mut SlidesPackageBuilder,
78+
slide_deck: SlideDeck,
79+
}
80+
81+
impl<'p> SlideDeckBuilder<'p> {
82+
fn section(&mut self, content: PathBuf) -> SectionBuilder<'p, '_> {
83+
SectionBuilder {
84+
deck_builder: self,
85+
section: Section {
86+
content,
87+
objectives: vec![],
88+
summary: vec![],
89+
further_reading: vec![],
90+
},
91+
}
92+
}
93+
94+
fn add(self) -> &'p mut SlidesPackageBuilder {
95+
self.package_builder.package.decks.push(self.slide_deck);
96+
self.package_builder
97+
}
98+
}
99+
100+
pub struct SectionBuilder<'p, 'd> {
101+
deck_builder: &'d mut SlideDeckBuilder<'p>,
102+
section: Section,
103+
}
104+
105+
impl<'p, 'd> SectionBuilder<'p, 'd> {
106+
pub fn objective(&mut self, objective: &str) {
107+
self.section.objectives.push(objective.to_string());
108+
}
109+
110+
pub fn summary(&mut self, summary: String) {
111+
self.section.summary.push(summary);
112+
}
113+
114+
pub fn further_reading(&mut self, further_reading: String) {
115+
self.section.further_reading.push(further_reading);
116+
}
117+
118+
pub fn add(self) -> &'d mut SlideDeckBuilder<'p> {
119+
self.deck_builder.slide_deck.sections.push(self.section);
120+
self.deck_builder
121+
}
122+
}

0 commit comments

Comments
 (0)