use std::fmt::Display; use bitcode::{Decode, Encode}; #[derive(Debug, Clone, Encode, Decode)] pub enum Problem { Length { min: u64, max: u64, actual: u64 }, Empty, Characters, MultiLine, NotJpeg, Dimensions { expected: u32 }, } pub fn size_text(size: u64) -> String { match size { size if size >= 1 << 20 => format!("{:.1} MB", size as f64 / (1 << 20) as f64), size if size >= 1 << 10 => format!("{:.0} KB", size as f64 / (1 << 10) as f64), size => format!("{size} B"), } } impl Problem { pub fn describe(&self, field: &Field) -> String { match (self, field.is_size()) { (Problem::Length { min, actual, .. }, true) if actual < min => { format!( "too small: {} (min {})", size_text(*actual), size_text(*min) ) } (Problem::Length { max, actual, .. }, true) => { format!( "too large: {} (max {})", size_text(*actual), size_text(*max) ) } _ => self.to_string(), } } } impl Display for Problem { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Problem::Length { min, actual, .. } if actual < min => { write!(f, "too short ({actual} < {min})") } Problem::Length { max, actual, .. } => write!(f, "too long ({actual} > {max})"), Problem::Empty => write!(f, "empty"), Problem::Characters => write!(f, "contains forbidden character"), Problem::MultiLine => write!(f, "contains line break"), Problem::NotJpeg => write!(f, "not JPEG"), Problem::Dimensions { expected } => write!(f, "must be square, at most {expected} px"), } } } #[derive(Debug, Clone, Copy, Encode, Decode)] pub enum Field { User, Password, Community, Message, Status, Avatar, Attachment, AttachmentName, Reason, Forward, AttachmentLimit, Device, BackupName, Quota, Announcement, Poll, Reaction, } impl Field { fn is_size(&self) -> bool { matches!( self, Field::Attachment | Field::Avatar | Field::AttachmentLimit | Field::Quota ) } } impl Display for Field { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let label = match self { Field::User => "username:", Field::Password => "password:", Field::Community => "community name:", Field::Message => "message:", Field::Status => "status:", Field::Avatar => "avatar:", Field::Attachment => "file", Field::AttachmentName => "file name:", Field::Reason => "reason:", Field::Forward => "forward:", Field::AttachmentLimit => "max attachment size:", Field::Device => "device:", Field::BackupName => "backup name:", Field::Quota => "quota:", Field::Announcement => "announcement:", Field::Poll => "poll:", Field::Reaction => "reaction:", }; write!(f, "{label}") } } #[derive(Debug, Clone, Encode, Decode, thiserror::Error)] pub enum Error { #[error("unauthenticated")] Unauthenticated, #[error("forbidden")] Forbidden, #[error("not found")] NotFound, #[error("already taken")] Taken, #[error("wrong credentials")] WrongCredentials, #[error("already a member")] AlreadyMember, #[error("already invited")] AlreadyInvited, #[error("suspended")] Suspended { until_ms: Option }, #[error("banned")] Banned, #[error("registration closed")] RegistrationClosed, #[error("maintenance")] Maintenance, #[error("storage quota exceeded")] QuotaExceeded, #[error("limit reached")] LimitReached, #[error("too many requests, retry in {retry_after_seconds}s")] RateLimited { retry_after_seconds: u64 }, #[error("{field} {}", .problem.describe(.field))] Invalid { field: Field, problem: Problem }, #[error("server error")] Server, }