rustysan-sword 0.1.1

Blade-inspired HTML templates for Rust
Documentation
use std::path::PathBuf;

/// A Sword lookup, syntax, or rendering failure.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// The view name is empty or contains a path separator or traversal.
    #[error("invalid view name: {0}")]
    InvalidName(String),
    /// No template root contains the view.
    #[error("view not found: {0}")]
    NotFound(String),
    /// A template file could not be read.
    #[error("failed reading {path}: {source}")]
    Io {
        /// The file that could not be read.
        path: PathBuf,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },
    /// The template source is not valid Sword.
    #[error("{template}:{line}:{column}: {message}")]
    Syntax {
        /// The logical template name.
        template: String,
        /// The one-based line in the original source.
        line: usize,
        /// The one-based column in the original source.
        column: usize,
        /// What is wrong.
        message: String,
    },
    /// Rendering failed at a known line of the original source.
    #[error("{template}:{line}: {message}")]
    Render {
        /// The logical template name.
        template: String,
        /// The one-based line in the original source.
        line: usize,
        /// What went wrong; it may include the file, column and call sites.
        message: String,
    },
    /// A backend error whose original line is unavailable.
    #[error("{template}: {message}")]
    RenderUnlocated {
        /// The logical template name.
        template: String,
        /// What went wrong.
        message: String,
    },
    /// The render succeeded but never reached an `@fragment` with the requested name.
    #[error("{template}: fragment `{fragment}` was not rendered")]
    FragmentNotFound {
        /// The logical template name.
        template: String,
        /// The requested fragment.
        fragment: String,
    },
    /// Templates include or extend each other in a cycle.
    #[error("template dependency cycle: {0}")]
    Cycle(String),
    /// A chain of includes, layouts and components is more than 64 templates deep.
    #[error("template dependency depth exceeds 64 at {0}")]
    DependencyDepth(String),
    /// View data holds a number that cannot be represented.
    #[error("view data contains a number outside the supported range: {0}")]
    Number(String),
    /// View data could not be serialized.
    #[error("could not serialize view data: {0}")]
    Data(#[from] serde_json::Error),
}

impl Error {
    /// A short, data-free label for logs and metrics. It never contains template text,
    /// paths, or request data.
    pub(crate) fn kind(&self) -> &'static str {
        match self {
            Error::InvalidName(_) => "invalid_name",
            Error::NotFound(_) => "not_found",
            Error::Io { .. } => "io",
            Error::Syntax { .. } => "syntax",
            Error::Render { .. } | Error::RenderUnlocated { .. } => "render",
            Error::FragmentNotFound { .. } => "fragment_not_found",
            Error::Cycle(_) => "cycle",
            Error::DependencyDepth(_) => "dependency_depth",
            Error::Number(_) | Error::Data(_) => "data",
        }
    }
}