use std::fmt;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Error)]
pub enum SqliteError {
#[error("{0}")]
ConstraintViolation(ConstraintViolation),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Error)]
#[error("{kind}: {message}")]
pub struct ConstraintViolation {
pub kind: ConstraintViolationKind,
pub message: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConstraintViolationKind {
Unique,
PrimaryKey,
NotNull,
ForeignKey,
Check,
Other,
}
impl fmt::Display for ConstraintViolationKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unique => f.write_str("unique"),
Self::PrimaryKey => f.write_str("primary-key"),
Self::NotNull => f.write_str("not-null"),
Self::ForeignKey => f.write_str("foreign-key"),
Self::Check => f.write_str("check"),
Self::Other => f.write_str("other"),
}
}
}
#[derive(Clone, Debug, Error)]
#[error("command rejected: {0}")]
pub struct CommandExecuteError(pub String);
#[derive(Clone, Debug, Error)]
#[error("{code}: {message}")]
pub struct CommandError {
pub code: ErrorCode,
pub message: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
pub enum ErrorCode {
#[error("rejected")]
Rejected,
#[error("invalid_input")]
InvalidInput,
#[error("internal")]
Internal,
}
impl CommandError {
pub fn reject(message: impl Into<String>) -> Self {
Self {
code: ErrorCode::Rejected,
message: message.into(),
}
}
pub fn invalid_input(message: impl Into<String>) -> Self {
Self {
code: ErrorCode::InvalidInput,
message: message.into(),
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self {
code: ErrorCode::Internal,
message: message.into(),
}
}
}
#[derive(Clone, Debug, Error)]
#[error("(de)serialization error: {message}")]
pub struct SerializationError {
pub message: String,
}
impl SerializationError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl From<serde_json::Error> for SerializationError {
fn from(err: serde_json::Error) -> Self {
Self::new(err.to_string())
}
}