Skip to main content

hornet_bind9/
error.rs

1//! Error types for `hornet-bind9`.
2
3use miette::Diagnostic;
4use thiserror::Error;
5
6/// Top-level error returned by all public parse/write/validate functions.
7#[derive(Debug, Error, Diagnostic)]
8pub enum Error {
9    #[error("Parse error in {file}: {message}")]
10    #[diagnostic(code(hornet_bind9::parse))]
11    Parse {
12        file: String,
13        message: String,
14        #[source_code]
15        src: miette::NamedSource<String>,
16        #[label("here")]
17        span: miette::SourceSpan,
18    },
19
20    #[error("Validation error: {0}")]
21    #[diagnostic(code(hornet_bind9::validate))]
22    Validation(#[from] ValidationError),
23
24    #[error("I/O error: {0}")]
25    #[diagnostic(code(hornet_bind9::io))]
26    Io(#[from] std::io::Error),
27
28    #[error("Write error: {0}")]
29    #[diagnostic(code(hornet_bind9::write))]
30    Write(String),
31}
32
33/// A single validation finding.
34#[derive(Debug, Clone, Error)]
35#[error("{severity}: {message}")]
36pub struct ValidationError {
37    pub severity: Severity,
38    pub message: String,
39    pub location: Option<ErrorLocation>,
40}
41
42/// Diagnostic severity level.
43#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
44pub enum Severity {
45    Info,
46    Warning,
47    Error,
48}
49
50impl std::fmt::Display for Severity {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            Severity::Info => write!(f, "info"),
54            Severity::Warning => write!(f, "warning"),
55            Severity::Error => write!(f, "error"),
56        }
57    }
58}
59
60/// Source location attached to a diagnostic.
61#[derive(Debug, Clone)]
62pub struct ErrorLocation {
63    pub file: String,
64    pub line: usize,
65    pub column: usize,
66}
67
68/// Convenience alias.
69pub type Result<T> = std::result::Result<T, Error>;
70
71#[cfg(test)]
72mod error_tests;