Skip to main content

switchback_traits/
error.rs

1//! Errors surfaced at the switchback-traits seam.
2
3use std::path::PathBuf;
4
5use thiserror::Error;
6
7/// Error type for seam operations (codec, render, load, link extraction).
8#[derive(Debug, Error)]
9pub enum SwitchbackError {
10    /// Binary codec serialize/deserialize failed.
11    #[error("codec error: {0}")]
12    Codec(String),
13
14    /// Renderer failed to produce output.
15    #[error("render error: {0}")]
16    Render(String),
17
18    /// Contract or manual loading failed.
19    #[error("load error: {0}")]
20    Load(String),
21
22    /// Link extraction or formatting failed.
23    #[error("link error: {0}")]
24    Link(String),
25
26    /// Companion discovery failed.
27    #[error("companion error: {0}")]
28    Companion(String),
29
30    /// Local filesystem I/O.
31    #[error("io error at {path}: {source}")]
32    Io {
33        /// Path involved in the failed I/O operation.
34        path: PathBuf,
35        /// Underlying operating-system I/O error.
36        #[source]
37        source: std::io::Error,
38    },
39
40    /// Catch-all for family-specific failures without pulling in `anyhow`.
41    #[error("{0}")]
42    Other(String),
43}
44
45impl SwitchbackError {
46    /// Wraps a codec failure message.
47    pub fn codec(message: impl Into<String>) -> Self {
48        Self::Codec(message.into())
49    }
50
51    /// Wraps a renderer failure message.
52    pub fn render(message: impl Into<String>) -> Self {
53        Self::Render(message.into())
54    }
55
56    /// Wraps a contract or manual load failure message.
57    pub fn load(message: impl Into<String>) -> Self {
58        Self::Load(message.into())
59    }
60
61    /// Wraps a link extraction or formatting failure message.
62    pub fn link(message: impl Into<String>) -> Self {
63        Self::Link(message.into())
64    }
65
66    /// Wraps a companion discovery failure message.
67    pub fn companion(message: impl Into<String>) -> Self {
68        Self::Companion(message.into())
69    }
70
71    /// Wraps an uncategorized failure message.
72    pub fn other(message: impl Into<String>) -> Self {
73        Self::Other(message.into())
74    }
75}
76
77/// Result alias for seam operations that surface [`SwitchbackError`].
78pub type Result<T> = std::result::Result<T, SwitchbackError>;