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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Defines types to represent a template's content and metadata.
use std::path::{Path, PathBuf};
use serde::Deserialize;
use crate::result::{Error, Result};
use super::defaults::{CONFIG_TAG_CLOSE, CONFIG_TAG_OPEN};
use super::names::Names;
/// A struct representing a fully configured template.
#[derive(Clone, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Template {
/// The template's id.
///
/// This is the file path relative to the templates directory. It serves to identify a template
/// within the registry when rendering.
///
/// ```plaintext
/// --> /path/to/templates/nested/template.md
/// --> nested/template.md
/// ```
#[serde(skip_deserializing)]
pub id: String,
/// The unparsed contents of the template.
///
/// This gets parsed and validated during template registration.
#[serde(skip_deserializing)]
pub contents: String,
/// The template's group name.
///
/// See [`StructureMode::FlatGrouped`] and [`StructureMode::NestedGrouped`] for more information.
#[serde(deserialize_with = "crate::utils::deserialize_and_sanitize")]
pub group: String,
/// The template's context mode i.e what the template intends to render.
///
/// See [`ContextMode`] for more information.
#[serde(rename = "context")]
pub context_mode: ContextMode,
/// The template's structure mode i.e. how the output should be structured.
///
/// See [`StructureMode`] for more information.
#[serde(rename = "structure")]
pub structure_mode: StructureMode,
/// The template's file extension.
pub extension: String,
/// The template strings for generating output file and directory names.
#[serde(default)]
pub names: Names,
}
impl Template {
/// Creates a new instance of [`Template`].
///
/// # Arguments
///
/// * `path` - The path to the template relative to the templates directory.
/// * `string` - The contents of the template file.
///
/// # Errors
///
/// Will return `Err` if:
/// * The template's opening and closing config tags have syntax errors.
/// * The tempalte's config has syntax errors or is missing required fields.
pub fn new<P>(path: P, string: &str) -> Result<Self>
where
P: AsRef<Path>,
{
let path = path.as_ref();
let (config, contents) = Self::parse(string).ok_or(Error::InvalidTemplateConfig {
path: path.display().to_string(),
})?;
let mut template: Self = serde_yaml_ng::from_str(config)?;
template.id = path.display().to_string();
template.contents = contents;
Ok(template)
}
/// Returns a tuple containing the template's configuration and its contents respectively.
///
/// Returns `None` if the template's config block is formatted incorrectly.
fn parse(string: &str) -> Option<(&str, String)> {
// Find where the opening tag starts...
let mut config_start = string.find(CONFIG_TAG_OPEN)?;
// (Save the pre-config contents.)
let pre_config_contents = &string[0..config_start];
// ...and offset it by the length of the config opening tag.
config_start += CONFIG_TAG_OPEN.len();
// Starting from where we found the opening tag, search for a closing tag. If we don't offset
// the starting point we might find another closing tag located before the opening tag.
let mut config_end = string[config_start..].find(CONFIG_TAG_CLOSE)?;
// Remove the offset we just used.
config_end += config_start;
let config = &string[config_start..config_end];
// The template's post-config contents start after the closiong tag.
let post_config_contents = config_end + CONFIG_TAG_CLOSE.len();
let mut post_config_contents = &string[post_config_contents..];
// Trim a single linebreak if its present.
if post_config_contents.starts_with('\n') {
post_config_contents = &post_config_contents[1..];
}
let contents = format!("{pre_config_contents}{post_config_contents}",);
Some((config, contents))
}
}
impl std::fmt::Debug for Template {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Template")
.field("id", &self.id)
.field("group", &self.group)
.field("context_mode", &self.context_mode)
.field("structure_mode", &self.structure_mode)
.finish_non_exhaustive()
}
}
/// A struct representing an unconfigured partial template.
///
/// Partial templates get their configuration from the normal templates that `include` them.
#[derive(Clone)]
pub struct TemplatePartial {
/// The template's id.
///
/// This is the file path relative to the templates directory. It serves to identify a partial
/// template when called in an `include` tag from within a non-partial template.
///
/// ```plaintext
/// --> /path/to/templates/nested/template.md
/// --> nested/template.md
/// --> {% include "nested/template.md" %}
/// ````
pub id: String,
/// The unparsed contents of the template.
///
/// This gets parsed and validated *only* if its called in an `include` tag in a non-partial
/// template that is being registered/parsed/valiated.
pub contents: String,
}
impl TemplatePartial {
/// Creates a new instance of [`TemplatePartial`].
///
/// # Arguments
///
/// * `path` - The path to the template relative to the templates directory.
/// * `string` - The contents of the template file.
pub fn new<P>(path: P, string: &str) -> Self
where
P: AsRef<Path>,
{
Self {
id: path.as_ref().display().to_string(),
contents: string.to_owned(),
}
}
}
impl std::fmt::Debug for TemplatePartial {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TemplatePartial")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
/// A struct representing a rendered template.
#[derive(Default)]
pub struct Render {
/// The path to where the template will be written to.
///
/// This path should be relative to the final output directory as this path is appended to it to
/// determine the the full output path.
pub path: PathBuf,
/// The final output filename.
pub filename: String,
/// The rendered content.
pub contents: String,
}
impl Render {
/// Creates a new instance of [`Template`].
#[must_use]
pub fn new(path: PathBuf, filename: String, contents: String) -> Self {
Self {
path,
filename,
contents,
}
}
}
impl std::fmt::Debug for Render {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Render")
.field("path", &self.path)
.field("filename", &self.filename)
.finish_non_exhaustive()
}
}
/// An enum representing the ways to structure a template's rendered files.
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum StructureMode {
/// When selected, the template is rendered to the output directory without any structure.
///
/// ```yaml
/// output-mode: flat
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// │
/// ├─ [template-name-01].[extension]
/// ├─ [template-name-01].[extension]
/// └─ ...
/// ```
Flat,
/// When selected, the template is rendered to the output directory and placed inside a
/// directory named after its `group`. This useful if there are multiple related and unrelated
/// templates being rendered to the same directory.
///
/// ```yaml
/// output-mode: flat-grouped
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// │
/// ├─ [template-group-01]
/// │ ├─ [template-name-01].[extension]
/// │ ├─ [template-name-01].[extension]
/// │ └─ ...
/// │
/// ├─ [template-group-02]
/// │ └─ ...
/// └─ ...
/// ```
FlatGrouped,
/// When selected, the template is rendered to the output directory and placed inside a
/// directory named after its `nested-directory-template`. This useful if multiple templates are
/// used to represent a single book i.e. a book template used to render a book's information to
/// a single file and an annotation template used to render each annotation to a separate file.
///
/// ```yaml
/// output-mode: nested
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// │
/// ├─ [author-title-01]
/// │ ├─ [template-name-01].[extension]
/// │ ├─ [template-name-01].[extension]
/// │ └─ ...
/// │
/// ├─ [author-title-02]
/// │ └─ ...
/// └─ ...
/// ```
Nested,
/// When selected, the template is rendered to the output directory and placed inside a
/// directory named after its `group` and another named after its `nested-directory-template`.
/// This useful if multiple templates are used to represent a single book i.e. a book template
/// and an annotation template and there are multiple related and unrelated templates being
/// rendered to the same directory.
///
///
/// ```yaml
/// output-mode: nested-grouped
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// │
/// ├─ [template-group-01]
/// │ │
/// │ ├─ [author-title-01]
/// │ │ ├─ [template-name-01].[extension]
/// │ │ ├─ [template-name-01].[extension]
/// │ │ └─ ...
/// │ │
/// │ ├─ [author-title-02]
/// │ │ ├─ [template-name-02].[extension]
/// │ │ ├─ [template-name-02].[extension]
/// │ │ └─ ...
/// │ └─ ...
/// │
/// ├─ [template-group-02]
/// │ ├─ [author-title-01]
/// │ │ └─ ...
/// │ └─ ...
/// └─ ...
/// ```
NestedGrouped,
}
/// An enum representing what a template intends to render.
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ContextMode {
/// When selected, the template is rendered to a single file containing a [`Book`][book] and all
/// its [`Annotation`][annotation]s.
///
/// ```yaml
/// render-context: book
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// └─ [template-name].[extension]
/// ```
///
/// [book]: crate::models::book::Book
/// [annotation]: crate::models::annotation::Annotation
Book,
/// When selected, the template is rendered to multiple files containing a [`Book`][book] and
/// only one its [`Annotation`][annotation]s.
///
/// ```yaml
/// render-context: annotation
/// ```
///
/// ```plaintext
/// [ouput-directory]
/// ├─ [template-name].[extension]
/// ├─ [template-name].[extension]
/// ├─ [template-name].[extension]
/// └─ ...
/// ```
///
/// [book]: crate::models::book::Book
/// [annotation]: crate::models::annotation::Annotation
Annotation,
}
#[cfg(test)]
mod test {
use super::*;
use crate::defaults::test::TemplatesDirectory;
use crate::utils;
mod invalid_config {
use super::*;
// Tests that a missing config block returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_config() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-config.txt",
);
Template::parse(&template).unwrap();
}
// Tests that a missing closing tag returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_closing_tag() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-closing-tag.txt",
);
Template::parse(&template).unwrap();
}
// Tests that missing `readstor` in the opening tag returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn incomplete_opening_tag_01() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"incomplete-opening-tag-01.txt",
);
Template::parse(&template).unwrap();
}
// Tests that missing the `!` in the opening tag returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn incomplete_opening_tag_02() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"incomplete-opening-tag-02.txt",
);
Template::parse(&template).unwrap();
}
// Tests that no linebreak after `readstor` returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_linebreak_01() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-linebreak-01.txt",
);
Template::parse(&template).unwrap();
}
// Tests that no linebreak after the config body returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_linebreak_02() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-linebreak-02.txt",
);
Template::parse(&template).unwrap();
}
// Tests that no linebreak after the closing tag returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_linebreak_03() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-linebreak-03.txt",
);
Template::parse(&template).unwrap();
}
// Tests that no linebreak before the opening tag returns an error.
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn missing_linebreak_04() {
let template = utils::testing::load_template_str(
TemplatesDirectory::InvalidConfig,
"missing-linebreak-04.txt",
);
Template::parse(&template).unwrap();
}
}
mod valid_config {
use super::*;
// Test the minimum required keys.
#[test]
fn minimum_required_keys() {
let filename = "minimum-required-keys.txt";
let template =
utils::testing::load_template_str(TemplatesDirectory::ValidConfig, filename);
Template::new(filename, &template).unwrap();
}
// Tests that a template with pre- and post-config-content returns no error.
#[test]
fn pre_and_post_config_content() {
let template = utils::testing::load_template_str(
TemplatesDirectory::ValidConfig,
"pre-and-post-config-content.txt",
);
Template::parse(&template).unwrap();
}
// Tests that a template with pre-config-content returns no error.
#[test]
fn pre_config_content() {
let template = utils::testing::load_template_str(
TemplatesDirectory::ValidConfig,
"pre-config-content.txt",
);
Template::parse(&template).unwrap();
}
// Tests that a template with post-config-content returns no error.
#[test]
fn post_config_content() {
let template = utils::testing::load_template_str(
TemplatesDirectory::ValidConfig,
"post-config-content.txt",
);
Template::parse(&template).unwrap();
}
}
}