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
14use thiserror::Error;
15
16#[derive(Debug, Clone, Error)]
17pub enum IdValidationError {
18    #[error("{id_type} cannot be empty")]
19    Empty { id_type: &'static str },
20    #[error("{id_type}: {message}")]
21    Invalid {
22        id_type: &'static str,
23        message: String,
24    },
25}
26
27impl IdValidationError {
28    #[must_use]
29    pub const fn empty(id_type: &'static str) -> Self {
30        Self::Empty { id_type }
31    }
32
33    #[must_use]
34    pub fn invalid(id_type: &'static str, message: impl Into<String>) -> Self {
35        Self::Invalid {
36            id_type,
37            message: message.into(),
38        }
39    }
40}
41
42#[derive(Debug, Clone, Error)]
43pub enum DbValueError {
44    #[error("cannot convert NULL to {target}")]
45    Null { target: &'static str },
46    #[error("cannot convert {from} to {target}")]
47    Incompatible {
48        from: &'static str,
49        target: &'static str,
50    },
51    #[error("cannot parse {value:?} as {target}")]
52    Parse { value: String, target: &'static str },
53    #[error("value out of range for {target}")]
54    OutOfRange { target: &'static str },
55}
56
57impl DbValueError {
58    #[must_use]
59    pub const fn null_for(target: &'static str) -> Self {
60        Self::Null { target }
61    }
62
63    #[must_use]
64    pub const fn incompatible(from: &'static str, target: &'static str) -> Self {
65        Self::Incompatible { from, target }
66    }
67
68    #[must_use]
69    pub fn parse(value: impl Into<String>, target: &'static str) -> Self {
70        Self::Parse {
71            value: value.into(),
72            target,
73        }
74    }
75
76    #[must_use]
77    pub const fn out_of_range(target: &'static str) -> Self {
78        Self::OutOfRange { target }
79    }
80}