tetratto/crates/core/src/model/mod.rs

67 lines
1.9 KiB
Rust
Raw Normal View History

pub mod auth;
pub mod journal;
pub mod journal_permissions;
2025-03-23 16:37:43 -04:00
pub mod permissions;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ApiReturn<T>
where
T: Serialize,
{
pub ok: bool,
pub message: String,
pub payload: T,
}
#[derive(Debug)]
pub enum Error {
MiscError(String),
DatabaseConnection(String),
UserNotFound,
GeneralNotFound(String),
RegistrationDisabled,
DatabaseError(String),
IncorrectPassword,
2025-03-23 16:37:43 -04:00
NotAllowed,
AlreadyAuthenticated,
DataTooLong(String),
DataTooShort(String),
Unknown,
}
impl ToString for Error {
fn to_string(&self) -> String {
match self {
Self::MiscError(msg) => msg.to_owned(),
2025-03-23 16:37:43 -04:00
Self::DatabaseConnection(msg) => msg.to_owned(),
Self::DatabaseError(msg) => format!("Database error: {msg}"),
Self::UserNotFound => "Unable to find user with given parameters".to_string(),
Self::GeneralNotFound(name) => format!("Unable to find requested {name}"),
2025-03-23 16:37:43 -04:00
Self::RegistrationDisabled => "Registration is disabled".to_string(),
Self::IncorrectPassword => "The given password is invalid".to_string(),
Self::NotAllowed => "You are not allowed to do this".to_string(),
Self::AlreadyAuthenticated => "Already authenticated".to_string(),
Self::DataTooLong(name) => format!("Given {name} is too long!"),
Self::DataTooShort(name) => format!("Given {name} is too short!"),
_ => format!("An unknown error as occurred: ({:?})", self),
}
}
}
impl<T> Into<ApiReturn<T>> for Error
where
T: Default + Serialize,
{
fn into(self) -> ApiReturn<T> {
ApiReturn {
ok: false,
message: self.to_string(),
payload: T::default(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;