ralph/template/loader/types.rs
1//! Purpose: Define shared data types for template loading, listing, and error
2//! reporting.
3//!
4//! Responsibilities:
5//! - Represent template sources and listing metadata.
6//! - Represent loaded templates plus validation/context warnings.
7//! - Define structured template-operation errors.
8//!
9//! Scope:
10//! - Data modeling only; no filesystem access, parsing, or substitution.
11//!
12//! Usage:
13//! - Used by the `load` and `list` companions and re-exported through
14//! `template::loader`.
15//!
16//! Invariants/Assumptions:
17//! - Error messages remain user-facing and actionable.
18//! - `TemplateSource::Custom` always stores the resolved template path.
19
20use std::path::PathBuf;
21
22use crate::contracts::Task;
23use crate::template::variables::TemplateWarning;
24
25/// Source of a loaded template.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum TemplateSource {
28 /// Custom template from `.ralph/templates/`.
29 Custom(PathBuf),
30 /// Built-in embedded template (stores the name, not the content).
31 Builtin(String),
32}
33
34/// Metadata for a template (used for listing).
35#[derive(Debug, Clone)]
36pub struct TemplateInfo {
37 pub name: String,
38 pub source: TemplateSource,
39 pub description: String,
40}
41
42/// Result of loading a template with context.
43#[derive(Debug, Clone)]
44pub struct LoadedTemplate {
45 /// The task with variables substituted.
46 pub task: Task,
47 /// The source of the template.
48 pub source: TemplateSource,
49 /// Warnings collected during validation and context detection.
50 pub warnings: Vec<TemplateWarning>,
51}
52
53/// Error type for template operations.
54#[derive(Debug, thiserror::Error)]
55pub enum TemplateError {
56 #[error("Template not found: {0}")]
57 NotFound(String),
58 #[error("Failed to read template file: {0}")]
59 ReadError(String),
60 #[error("Invalid template JSON: {0}")]
61 InvalidJson(String),
62 #[error("Template validation failed: {0}")]
63 ValidationError(String),
64}