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

73 lines
2.1 KiB
Rust
Raw Normal View History

pub mod auth;
pub mod communities;
pub mod communities_permissions;
pub mod moderation;
2025-03-23 16:37:43 -04:00
pub mod permissions;
2025-03-24 22:42:33 -04:00
pub mod reactions;
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),
2025-03-31 11:45:34 -04:00
UsernameInUse,
TitleInUse,
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!"),
2025-03-31 11:45:34 -04:00
Self::UsernameInUse => "Username in use".to_string(),
Self::TitleInUse => "Title in use".to_string(),
_ => format!("An unknown error as occurred: ({:?})", self),
}
}
}
2025-04-10 18:16:52 -04:00
impl<T> From<Error> for ApiReturn<T>
where
T: Default + Serialize,
{
2025-04-10 18:16:52 -04:00
fn from(val: Error) -> Self {
ApiReturn {
ok: false,
2025-04-10 18:16:52 -04:00
message: val.to_string(),
payload: T::default(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;