Skip to main content

systemprompt_identifiers/
error.rs

1//! Error types raised by identifier validation and database value conversion.
2//!
3//! The crate exposes two error enums:
4//!
5//! - `IdValidationError` — produced when a `try_new` constructor on a typed
6//!   identifier rejects its input (empty string, malformed shape, etc.).
7//! - `DbValueError` — produced when `FromDbValue` cannot convert a `DbValue`
8//!   variant into the requested target type (NULL where a value is required,
9//!   type mismatch, parse failure, numeric overflow).
10//!
11//! Both implement `std::error::Error` so callers can compose them into
12//! larger `thiserror`-derived enums via `#[from]`.
13//!
14//! Copyright (c) systemprompt.io — Business Source License 1.1.
15//! See <https://systemprompt.io> for licensing details.
16
17use thiserror::Error;
18
19#[derive(Debug, Clone, Error)]
20pub enum IdValidationError {
21    #[error("{id_type} cannot be empty")]
22    Empty { id_type: &'static str },
23    #[error("{id_type}: {message}")]
24    Invalid {
25        id_type: &'static str,
26        message: String,
27    },
28}
29
30impl IdValidationError {
31    #[must_use]
32    pub const fn empty(id_type: &'static str) -> Self {
33        Self::Empty { id_type }
34    }
35
36    #[must_use]
37    pub fn invalid(id_type: &'static str, message: impl Into<String>) -> Self {
38        Self::Invalid {
39            id_type,
40            message: message.into(),
41        }
42    }
43}
44
45#[derive(Debug, Clone, Error)]
46pub enum DbValueError {
47    #[error("cannot convert NULL to {target}")]
48    Null { target: &'static str },
49    #[error("cannot convert {from} to {target}")]
50    Incompatible {
51        from: &'static str,
52        target: &'static str,
53    },
54    #[error("cannot parse {value:?} as {target}")]
55    Parse { value: String, target: &'static str },
56    #[error("value out of range for {target}")]
57    OutOfRange { target: &'static str },
58}
59
60impl DbValueError {
61    #[must_use]
62    pub const fn null_for(target: &'static str) -> Self {
63        Self::Null { target }
64    }
65
66    #[must_use]
67    pub const fn incompatible(from: &'static str, target: &'static str) -> Self {
68        Self::Incompatible { from, target }
69    }
70
71    #[must_use]
72    pub fn parse(value: impl Into<String>, target: &'static str) -> Self {
73        Self::Parse {
74            value: value.into(),
75            target,
76        }
77    }
78
79    #[must_use]
80    pub const fn out_of_range(target: &'static str) -> Self {
81        Self::OutOfRange { target }
82    }
83}