Skip to main content

ready_set_sdk/
error.rs

1//! Error and result types used throughout the SDK.
2
3use std::fmt;
4
5/// SDK-wide [`Result`] alias.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors produced by SDK helpers.
9///
10/// `Error` is `#[non_exhaustive]`: callers must use a wildcard arm in `match`
11/// statements. Adding a new variant in a future minor release is non-breaking.
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum Error {
15    /// An I/O failure surfaced by `std::io`.
16    #[error("io: {0}")]
17    Io(#[from] std::io::Error),
18
19    /// A TOML document failed to parse or did not match the expected schema.
20    #[error("toml: {0}")]
21    TomlParse(String),
22
23    /// A JSON document failed to parse or did not match the expected schema.
24    #[error("json: {0}")]
25    JsonParse(String),
26
27    /// A required external dependency was not found.
28    #[error("missing dependency: {name}{}", .hint.as_deref().map(|h| format!(" ({h})")).unwrap_or_default())]
29    MissingDependency {
30        /// Human-readable name of the missing dependency.
31        name: String,
32        /// Optional installation hint shown to the user.
33        hint: Option<String>,
34    },
35
36    /// A plugin or peer violated the documented dispatcher↔plugin contract.
37    #[error("contract violation: {0}")]
38    ContractViolation(String),
39
40    /// A catch-all variant for errors that do not fit the categories above.
41    #[error("{0}")]
42    Other(String),
43}
44
45impl Error {
46    /// Construct an `Other` error from anything that implements [`fmt::Display`].
47    pub fn other(msg: impl fmt::Display) -> Self {
48        Self::Other(msg.to_string())
49    }
50
51    /// Construct a `ContractViolation` error from anything that implements
52    /// [`fmt::Display`].
53    pub fn contract(msg: impl fmt::Display) -> Self {
54        Self::ContractViolation(msg.to_string())
55    }
56}
57
58impl From<toml::de::Error> for Error {
59    fn from(value: toml::de::Error) -> Self {
60        Self::TomlParse(value.to_string())
61    }
62}
63
64impl From<serde_json::Error> for Error {
65    fn from(value: serde_json::Error) -> Self {
66        Self::JsonParse(value.to_string())
67    }
68}