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

90 lines
2.6 KiB
Rust
Raw Normal View History

pub mod auth;
pub mod communities;
pub mod communities_permissions;
pub mod moderation;
2025-04-27 23:11:37 -04:00
pub mod oauth;
2025-03-23 16:37:43 -04:00
pub mod permissions;
2025-03-24 22:42:33 -04:00
pub mod reactions;
2025-04-12 22:25:54 -04:00
pub mod requests;
2025-05-05 19:38:01 -04:00
pub mod uploads;
#[cfg(feature = "redis")]
pub mod channels;
2025-04-27 23:11:37 -04:00
#[cfg(feature = "redis")]
pub mod socket;
2025-04-19 18:59:55 -04:00
use std::fmt::Display;
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,
2025-04-12 22:25:54 -04:00
QuestionsDisabled,
RequiresSupporter,
Unknown,
}
2025-04-19 18:59:55 -04:00
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&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(),
2025-04-12 22:25:54 -04:00
Self::QuestionsDisabled => "You are not allowed to ask questions there".to_string(),
Self::RequiresSupporter => {
"Only site supporters can upload GIF files as their avatar/banner".to_string()
}
_ => format!("An unknown error as occurred: ({:?})", self),
2025-04-19 18:59:55 -04:00
})
}
}
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>;