metis-docs-core 1.2.0

Core library for Flight Levels documentation management system
Documentation
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
//! Template loading service with fallback chain support.
//!
//! This service loads document templates from:
//! 1. Project-level: `.metis/templates/{type}/content.md`
//! 2. Global-level: `~/.config/metis/templates/{type}/content.md`
//! 3. Embedded defaults (compiled into binary)

use std::path::{Path, PathBuf};
use tera::{Context, Tera};

/// Embedded default templates for each document type
mod defaults {
    pub mod vision {
        pub const CONTENT: &str = include_str!("../../domain/documents/vision/content.md");
        pub const EXIT_CRITERIA: &str =
            include_str!("../../domain/documents/vision/acceptance_criteria.md");
    }

    pub mod strategy {
        pub const CONTENT: &str = include_str!("../../domain/documents/strategy/content.md");
        pub const EXIT_CRITERIA: &str =
            include_str!("../../domain/documents/strategy/acceptance_criteria.md");
    }

    pub mod initiative {
        pub const CONTENT: &str = include_str!("../../domain/documents/initiative/content.md");
        pub const EXIT_CRITERIA: &str =
            include_str!("../../domain/documents/initiative/acceptance_criteria.md");
    }

    pub mod task {
        pub const CONTENT: &str = include_str!("../../domain/documents/task/content.md");
        pub const EXIT_CRITERIA: &str =
            include_str!("../../domain/documents/task/acceptance_criteria.md");
    }

    pub mod adr {
        pub const CONTENT: &str = include_str!("../../domain/documents/adr/content.md");
        pub const EXIT_CRITERIA: &str =
            include_str!("../../domain/documents/adr/acceptance_criteria.md");
    }
}

/// Error type for template loading operations
#[derive(Debug, Clone)]
pub enum TemplateError {
    /// Template file could not be read
    IoError(String),
    /// Template failed to parse as valid Tera template
    ParseError(String),
    /// Template failed validation (render with sample data)
    ValidationError(String),
    /// Unknown document type
    UnknownDocumentType(String),
}

impl std::fmt::Display for TemplateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TemplateError::IoError(msg) => write!(f, "Template IO error: {}", msg),
            TemplateError::ParseError(msg) => write!(f, "Template parse error: {}", msg),
            TemplateError::ValidationError(msg) => write!(f, "Template validation error: {}", msg),
            TemplateError::UnknownDocumentType(t) => write!(f, "Unknown document type: {}", t),
        }
    }
}

impl std::error::Error for TemplateError {}

/// Template types that can be loaded
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateType {
    Content,
    ExitCriteria,
}

impl TemplateType {
    fn filename(&self) -> &'static str {
        match self {
            TemplateType::Content => "content.md",
            TemplateType::ExitCriteria => "exit_criteria.md",
        }
    }
}

/// Service for loading templates with fallback chain support.
///
/// Templates are loaded in this order:
/// 1. Project-level: `{workspace}/.metis/templates/{type}/{template}.md`
/// 2. Global-level: `~/.config/metis/templates/{type}/{template}.md`
/// 3. Embedded defaults
pub struct TemplateLoader {
    /// Path to the project workspace (e.g., `/path/to/project/.metis`)
    project_path: Option<PathBuf>,
    /// Path to global config (e.g., `~/.config/metis`)
    global_path: PathBuf,
}

impl TemplateLoader {
    /// Create a new TemplateLoader with the given project workspace path.
    ///
    /// If `project_path` is None, only global and embedded templates will be used.
    pub fn new(project_path: Option<PathBuf>) -> Self {
        let global_path = dirs::config_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("metis");

        Self {
            project_path,
            global_path,
        }
    }

    /// Create a TemplateLoader for a specific workspace directory.
    pub fn for_workspace<P: AsRef<Path>>(workspace_dir: P) -> Self {
        Self::new(Some(workspace_dir.as_ref().to_path_buf()))
    }

    /// Load a content template for the given document type.
    ///
    /// Returns the template content string, loading from the first available source
    /// in the fallback chain.
    pub fn load_content_template(&self, doc_type: &str) -> Result<String, TemplateError> {
        self.load_template(doc_type, TemplateType::Content)
    }

    /// Load an exit criteria template for the given document type.
    pub fn load_exit_criteria_template(&self, doc_type: &str) -> Result<String, TemplateError> {
        self.load_template(doc_type, TemplateType::ExitCriteria)
    }

    /// Load a template with the fallback chain.
    fn load_template(
        &self,
        doc_type: &str,
        template_type: TemplateType,
    ) -> Result<String, TemplateError> {
        // 1. Try project-level template
        if let Some(ref project_path) = self.project_path {
            let project_template = project_path
                .join("templates")
                .join(doc_type)
                .join(template_type.filename());

            if project_template.exists() {
                let content = std::fs::read_to_string(&project_template)
                    .map_err(|e| TemplateError::IoError(e.to_string()))?;

                // Validate the template before returning
                self.validate_template(&content, doc_type)?;
                return Ok(content);
            }
        }

        // 2. Try global-level template
        let global_template = self
            .global_path
            .join("templates")
            .join(doc_type)
            .join(template_type.filename());

        if global_template.exists() {
            let content = std::fs::read_to_string(&global_template)
                .map_err(|e| TemplateError::IoError(e.to_string()))?;

            // Validate the template before returning
            self.validate_template(&content, doc_type)?;
            return Ok(content);
        }

        // 3. Fall back to embedded defaults
        self.get_embedded_template(doc_type, template_type)
    }

    /// Get the embedded default template for a document type.
    fn get_embedded_template(
        &self,
        doc_type: &str,
        template_type: TemplateType,
    ) -> Result<String, TemplateError> {
        let template = match (doc_type, template_type) {
            ("vision", TemplateType::Content) => defaults::vision::CONTENT,
            ("vision", TemplateType::ExitCriteria) => defaults::vision::EXIT_CRITERIA,
            ("strategy", TemplateType::Content) => defaults::strategy::CONTENT,
            ("strategy", TemplateType::ExitCriteria) => defaults::strategy::EXIT_CRITERIA,
            ("initiative", TemplateType::Content) => defaults::initiative::CONTENT,
            ("initiative", TemplateType::ExitCriteria) => defaults::initiative::EXIT_CRITERIA,
            ("task", TemplateType::Content) => defaults::task::CONTENT,
            ("task", TemplateType::ExitCriteria) => defaults::task::EXIT_CRITERIA,
            ("adr", TemplateType::Content) => defaults::adr::CONTENT,
            ("adr", TemplateType::ExitCriteria) => defaults::adr::EXIT_CRITERIA,
            _ => return Err(TemplateError::UnknownDocumentType(doc_type.to_string())),
        };

        Ok(template.to_string())
    }

    /// Validate a template by rendering it with sample data.
    ///
    /// This catches template syntax errors and missing variable references early.
    pub fn validate_template(&self, template: &str, doc_type: &str) -> Result<(), TemplateError> {
        let mut tera = Tera::default();

        // Try to parse the template
        tera.add_raw_template("test_template", template)
            .map_err(|e| TemplateError::ParseError(e.to_string()))?;

        // Try to render with sample context
        let context = self.sample_context_for_type(doc_type);
        tera.render("test_template", &context)
            .map_err(|e| TemplateError::ValidationError(e.to_string()))?;

        Ok(())
    }

    /// Generate sample context values for validating templates.
    ///
    /// Each document type gets appropriate sample values for all available variables.
    pub fn sample_context_for_type(&self, doc_type: &str) -> Context {
        let mut context = Context::new();

        // Common variables for all document types
        context.insert("title", "Sample Document Title");
        context.insert("slug", "sample-document-title");
        context.insert("short_code", &format!("TEST-{}-0001", doc_type_letter(doc_type)));
        context.insert("created_at", "2025-01-01T00:00:00Z");
        context.insert("updated_at", "2025-01-01T00:00:00Z");
        context.insert("archived", "false");
        context.insert("exit_criteria_met", "false");
        context.insert("parent_id", "");
        context.insert("parent_title", "");
        context.insert("blocked_by", &Vec::<String>::new());
        context.insert("tags", &vec!["#sample", "#phase/draft"]);

        // Type-specific variables
        match doc_type {
            "vision" => {
                // Vision has no additional required variables
            }
            "strategy" => {
                context.insert("risk_level", "Medium");
                context.insert("stakeholders", &Vec::<String>::new());
            }
            "initiative" => {
                context.insert("estimated_complexity", "M");
                context.insert("strategy_id", "NULL");
                context.insert("initiative_id", "sample-initiative");
            }
            "task" => {
                context.insert("strategy_id", "NULL");
                context.insert("initiative_id", "NULL");
                context.insert("parent_title", "Sample Parent Initiative");
            }
            "adr" => {
                context.insert("number", &1);
                context.insert("decision_maker", "");
                context.insert("decision_date", "");
            }
            _ => {}
        }

        context
    }

    /// Check if custom templates exist for a document type.
    pub fn has_custom_template(&self, doc_type: &str, template_type: TemplateType) -> bool {
        // Check project-level
        if let Some(ref project_path) = self.project_path {
            let project_template = project_path
                .join("templates")
                .join(doc_type)
                .join(template_type.filename());
            if project_template.exists() {
                return true;
            }
        }

        // Check global-level
        let global_template = self
            .global_path
            .join("templates")
            .join(doc_type)
            .join(template_type.filename());
        global_template.exists()
    }

    /// Get the source of a template (for debugging/info).
    pub fn template_source(&self, doc_type: &str, template_type: TemplateType) -> TemplateSource {
        // Check project-level
        if let Some(ref project_path) = self.project_path {
            let project_template = project_path
                .join("templates")
                .join(doc_type)
                .join(template_type.filename());
            if project_template.exists() {
                return TemplateSource::Project(project_template);
            }
        }

        // Check global-level
        let global_template = self
            .global_path
            .join("templates")
            .join(doc_type)
            .join(template_type.filename());
        if global_template.exists() {
            return TemplateSource::Global(global_template);
        }

        TemplateSource::Embedded
    }
}

/// Indicates where a template was loaded from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TemplateSource {
    /// Template from project's `.metis/templates/` directory
    Project(PathBuf),
    /// Template from global `~/.config/metis/templates/` directory
    Global(PathBuf),
    /// Embedded default template
    Embedded,
}

impl std::fmt::Display for TemplateSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TemplateSource::Project(path) => write!(f, "project: {}", path.display()),
            TemplateSource::Global(path) => write!(f, "global: {}", path.display()),
            TemplateSource::Embedded => write!(f, "embedded default"),
        }
    }
}

/// Helper to get the type letter for short codes
fn doc_type_letter(doc_type: &str) -> char {
    match doc_type {
        "vision" => 'V',
        "strategy" => 'S',
        "initiative" => 'I',
        "task" => 'T',
        "adr" => 'A',
        _ => 'X',
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_load_embedded_templates() {
        let loader = TemplateLoader::new(None);

        // All document types should have embedded templates
        for doc_type in &["vision", "strategy", "initiative", "task", "adr"] {
            let content = loader.load_content_template(doc_type);
            assert!(content.is_ok(), "Failed to load content for {}", doc_type);
            assert!(!content.unwrap().is_empty());

            let exit_criteria = loader.load_exit_criteria_template(doc_type);
            assert!(
                exit_criteria.is_ok(),
                "Failed to load exit criteria for {}",
                doc_type
            );
        }
    }

    #[test]
    fn test_unknown_document_type() {
        let loader = TemplateLoader::new(None);
        let result = loader.load_content_template("unknown");
        assert!(matches!(result, Err(TemplateError::UnknownDocumentType(_))));
    }

    #[test]
    fn test_project_template_override() {
        let temp_dir = tempdir().unwrap();
        let project_path = temp_dir.path().to_path_buf();

        // Create a custom template
        let template_dir = project_path.join("templates").join("task");
        std::fs::create_dir_all(&template_dir).unwrap();

        let custom_template = "# {{ title }}\n\nCustom task template!";
        std::fs::write(template_dir.join("content.md"), custom_template).unwrap();

        let loader = TemplateLoader::for_workspace(&project_path);
        let content = loader.load_content_template("task").unwrap();

        assert!(content.contains("Custom task template!"));
        assert_eq!(
            loader.template_source("task", TemplateType::Content),
            TemplateSource::Project(template_dir.join("content.md"))
        );
    }

    #[test]
    fn test_template_validation_error() {
        let temp_dir = tempdir().unwrap();
        let project_path = temp_dir.path().to_path_buf();

        // Create an invalid template (unclosed Tera tag)
        let template_dir = project_path.join("templates").join("task");
        std::fs::create_dir_all(&template_dir).unwrap();

        let invalid_template = "# {{ title }\n\nBroken template";
        std::fs::write(template_dir.join("content.md"), invalid_template).unwrap();

        let loader = TemplateLoader::for_workspace(&project_path);
        let result = loader.load_content_template("task");

        assert!(matches!(result, Err(TemplateError::ParseError(_))));
    }

    #[test]
    fn test_template_validation_missing_variable() {
        let temp_dir = tempdir().unwrap();
        let project_path = temp_dir.path().to_path_buf();

        // Create a template with an undefined variable
        let template_dir = project_path.join("templates").join("task");
        std::fs::create_dir_all(&template_dir).unwrap();

        let template_with_missing_var = "# {{ title }}\n\nValue: {{ nonexistent_variable }}";
        std::fs::write(template_dir.join("content.md"), template_with_missing_var).unwrap();

        let loader = TemplateLoader::for_workspace(&project_path);
        let result = loader.load_content_template("task");

        assert!(matches!(result, Err(TemplateError::ValidationError(_))));
    }

    #[test]
    fn test_sample_context_generation() {
        let loader = TemplateLoader::new(None);

        for doc_type in &["vision", "strategy", "initiative", "task", "adr"] {
            let context = loader.sample_context_for_type(doc_type);

            // All types should have common variables
            assert!(context.get("title").is_some());
            assert!(context.get("slug").is_some());
            assert!(context.get("short_code").is_some());
        }

        // Type-specific variables
        let initiative_ctx = loader.sample_context_for_type("initiative");
        assert!(initiative_ctx.get("estimated_complexity").is_some());

        let strategy_ctx = loader.sample_context_for_type("strategy");
        assert!(strategy_ctx.get("risk_level").is_some());
    }

    #[test]
    fn test_has_custom_template() {
        let temp_dir = tempdir().unwrap();
        let project_path = temp_dir.path().to_path_buf();

        let loader = TemplateLoader::for_workspace(&project_path);

        // No custom templates initially
        assert!(!loader.has_custom_template("task", TemplateType::Content));

        // Create a custom template
        let template_dir = project_path.join("templates").join("task");
        std::fs::create_dir_all(&template_dir).unwrap();
        std::fs::write(template_dir.join("content.md"), "# {{ title }}").unwrap();

        assert!(loader.has_custom_template("task", TemplateType::Content));
        assert!(!loader.has_custom_template("task", TemplateType::ExitCriteria));
    }
}