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
pub struct TextSlide {
text: String
}
impl TextSlide {
fn new(text: &str) -> TextSlide {
TextSlide { text: String::from(text) }
}
}
pub struct ImageSlide {
image_path: String
}
impl ImageSlide {
fn new(text: & str) -> ImageSlide {
let path = &text[1..];
ImageSlide { image_path: String::from(path) }
}
}
pub enum Slide {
TextSlide(TextSlide),
ImageSlide(ImageSlide),
EmptySlide
}
pub fn generate_slides(full_text: String) -> Vec<Slide> {
let text_minus_comments: String = full_text
.lines()
.filter(|&line| line.len() == 0 || line.chars().nth(0).unwrap() != '#')
.map(|line | format!("{}\n", line))
.collect();
let slides_text: Vec<&str> = text_minus_comments
.split("\n\n")
.map(|s| s.trim())
.collect();
let mut slides: Vec<Slide> = Vec::new();
for s in slides_text {
match s.chars().nth(0).unwrap() {
'@' => slides.push(Slide::ImageSlide(ImageSlide::new(s))),
'/' => slides.push(Slide::EmptySlide),
_ => slides.push(Slide::TextSlide(TextSlide::new(s)))
}
};
slides
}
#[cfg(test)]
mod tests {
use crate::generate_slides;
#[test]
fn buncha_slides() {
let presentation = String::from("
* test
* test
* test
This is another slide.
Another one!
@image.png
");
let slides = generate_slides(presentation);
assert_eq!(slides.len(), 4)
}
#[test]
fn comments_work() {
let presentation = String::from("
* test
* test
* test
# This is a comment.
Another one!
@image.png
");
let slides = generate_slides(presentation);
assert_eq!(slides.len(), 3)
}
}