forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

error.rs170 lines, 4.7 KiBHistoryRawDownload
1use std::fmt::Display;
2
3use bitcode::{Decode, Encode};
4
5#[derive(Debug, Clone, Encode, Decode)]
6pub enum Problem {
7 Length { min: u64, max: u64, actual: u64 },
8 Empty,
9 Characters,
10 MultiLine,
11 NotJpeg,
12 Dimensions { expected: u32 },
13}
14
15pub fn size_text(size: u64) -> String {
16 match size {
17 size if size >= 1 << 20 => format!("{:.1} MB", size as f64 / (1 << 20) as f64),
18 size if size >= 1 << 10 => format!("{:.0} KB", size as f64 / (1 << 10) as f64),
19 size => format!("{size} B"),
20 }
21}
22
23impl Problem {
24 pub fn describe(&self, field: &Field) -> String {
25 match (self, field.is_size()) {
26 (Problem::Length { min, actual, .. }, true) if actual < min => {
27 format!(
28 "too small: {} (min {})",
29 size_text(*actual),
30 size_text(*min)
31 )
32 }
33 (Problem::Length { max, actual, .. }, true) => {
34 format!(
35 "too large: {} (max {})",
36 size_text(*actual),
37 size_text(*max)
38 )
39 }
40 _ => self.to_string(),
41 }
42 }
43}
44
45impl Display for Problem {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 Problem::Length { min, actual, .. } if actual < min => {
49 write!(f, "too short ({actual} < {min})")
50 }
51 Problem::Length { max, actual, .. } => write!(f, "too long ({actual} > {max})"),
52 Problem::Empty => write!(f, "empty"),
53 Problem::Characters => write!(f, "contains forbidden character"),
54 Problem::MultiLine => write!(f, "contains line break"),
55 Problem::NotJpeg => write!(f, "not JPEG"),
56 Problem::Dimensions { expected } => write!(f, "must be square, at most {expected} px"),
57 }
58 }
59}
60
61#[derive(Debug, Clone, Copy, Encode, Decode)]
62pub enum Field {
63 User,
64 Password,
65 Community,
66 Message,
67 Status,
68 Avatar,
69 Attachment,
70 AttachmentName,
71 Reason,
72 Forward,
73 AttachmentLimit,
74 Device,
75 BackupName,
76 Quota,
77 Announcement,
78 Poll,
79 Reaction,
80}
81
82impl Field {
83 fn is_size(&self) -> bool {
84 matches!(
85 self,
86 Field::Attachment | Field::Avatar | Field::AttachmentLimit | Field::Quota
87 )
88 }
89}
90
91impl Display for Field {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 let label = match self {
94 Field::User => "username:",
95 Field::Password => "password:",
96 Field::Community => "community name:",
97 Field::Message => "message:",
98 Field::Status => "status:",
99 Field::Avatar => "avatar:",
100 Field::Attachment => "file",
101 Field::AttachmentName => "file name:",
102 Field::Reason => "reason:",
103 Field::Forward => "forward:",
104 Field::AttachmentLimit => "max attachment size:",
105 Field::Device => "device:",
106 Field::BackupName => "backup name:",
107 Field::Quota => "quota:",
108 Field::Announcement => "announcement:",
109 Field::Poll => "poll:",
110 Field::Reaction => "reaction:",
111 };
112 write!(f, "{label}")
113 }
114}
115
116#[derive(Debug, Clone, Encode, Decode, thiserror::Error)]
117pub enum Error {
118 #[error("unauthenticated")]
119 Unauthenticated,
120 #[error("forbidden")]
121 Forbidden,
122 #[error("not found")]
123 NotFound,
124 #[error("already taken")]
125 Taken,
126 #[error("wrong credentials")]
127 WrongCredentials,
128 #[error("already a member")]
129 AlreadyMember,
130 #[error("already invited")]
131 AlreadyInvited,
132 #[error("suspended")]
133 Suspended { until_ms: Option<u64> },
134 #[error("banned")]
135 Banned,
136 #[error("registration closed")]
137 RegistrationClosed,
138 #[error("maintenance")]
139 Maintenance,
140 #[error("storage quota exceeded")]
141 QuotaExceeded,
142 #[error("limit reached")]
143 LimitReached,
144 #[error("too many requests, retry in {retry_after_seconds}s")]
145 RateLimited { retry_after_seconds: u64 },
146 #[error("{field} {}", .problem.describe(.field))]
147 Invalid { field: Field, problem: Problem },
148 #[error("{0}")]
149 Release(ReleaseProblem),
150 #[error("server error")]
151 Server,
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, thiserror::Error)]
155pub enum ReleaseProblem {
156 #[error("not a yumush client")]
157 NotClient,
158 #[error("too large")]
159 TooLarge,
160 #[error("already in release {0}")]
161 Released(u64),
162 #[error("no files")]
163 Empty,
164 #[error("two files for one target")]
165 SameTarget,
166 #[error("file not staged")]
167 NotStaged,
168 #[error("server binary does not start")]
169 ServerBroken,
170}