use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum IdValidationError {
#[error("{id_type} cannot be empty")]
Empty { id_type: &'static str },
#[error("{id_type}: {message}")]
Invalid {
id_type: &'static str,
message: String,
},
}
impl IdValidationError {
#[must_use]
pub const fn empty(id_type: &'static str) -> Self {
Self::Empty { id_type }
}
#[must_use]
pub fn invalid(id_type: &'static str, message: impl Into<String>) -> Self {
Self::Invalid {
id_type,
message: message.into(),
}
}
}
#[derive(Debug, Clone, Error)]
pub enum DbValueError {
#[error("cannot convert NULL to {target}")]
Null { target: &'static str },
#[error("cannot convert {from} to {target}")]
Incompatible {
from: &'static str,
target: &'static str,
},
#[error("cannot parse {value:?} as {target}")]
Parse { value: String, target: &'static str },
#[error("value out of range for {target}")]
OutOfRange { target: &'static str },
}
impl DbValueError {
#[must_use]
pub const fn null_for(target: &'static str) -> Self {
Self::Null { target }
}
#[must_use]
pub const fn incompatible(from: &'static str, target: &'static str) -> Self {
Self::Incompatible { from, target }
}
#[must_use]
pub fn parse(value: impl Into<String>, target: &'static str) -> Self {
Self::Parse {
value: value.into(),
target,
}
}
#[must_use]
pub const fn out_of_range(target: &'static str) -> Self {
Self::OutOfRange { target }
}
}