Skip to main content

modo/template/
config.rs

1use serde::Deserialize;
2
3/// Configuration for the template engine.
4///
5/// All fields have sensible defaults and can be overridden via YAML config.
6/// Paths are relative to the working directory of the running process.
7///
8/// # Defaults
9///
10/// | Field                | Default         |
11/// |----------------------|-----------------|
12/// | `templates_path`     | `"templates"`   |
13/// | `static_path`        | `"static"`      |
14/// | `static_url_prefix`  | `"/assets"`     |
15#[non_exhaustive]
16#[derive(Debug, Clone, Deserialize)]
17#[serde(default)]
18pub struct TemplateConfig {
19    /// Directory that contains MiniJinja template files.
20    pub templates_path: String,
21    /// Directory that contains static assets (CSS, JS, images, etc.).
22    pub static_path: String,
23    /// URL prefix under which static assets are served (e.g. `"/assets"`).
24    pub static_url_prefix: String,
25}
26
27impl Default for TemplateConfig {
28    fn default() -> Self {
29        Self {
30            templates_path: "templates".into(),
31            static_path: "static".into(),
32            static_url_prefix: "/assets".into(),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn default_config_has_sensible_values() {
43        let config = TemplateConfig::default();
44        assert_eq!(config.templates_path, "templates");
45        assert_eq!(config.static_path, "static");
46        assert_eq!(config.static_url_prefix, "/assets");
47    }
48
49    #[test]
50    fn config_deserializes_from_yaml() {
51        let yaml = r#"
52            templates_path: "views"
53            static_path: "public"
54            static_url_prefix: "/static"
55        "#;
56        let config: TemplateConfig = serde_yaml_ng::from_str(yaml).unwrap();
57        assert_eq!(config.templates_path, "views");
58        assert_eq!(config.static_path, "public");
59        assert_eq!(config.static_url_prefix, "/static");
60    }
61
62    #[test]
63    fn config_uses_defaults_for_missing_fields() {
64        let yaml = r#"
65            templates_path: "views"
66        "#;
67        let config: TemplateConfig = serde_yaml_ng::from_str(yaml).unwrap();
68        assert_eq!(config.templates_path, "views");
69        assert_eq!(config.static_path, "static");
70        assert_eq!(config.static_url_prefix, "/assets");
71    }
72}