1use std::fmt::Display;
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum ValidationError {
8 InvalidPasswordFormat(String),
10 InvalidAliasFormat(String),
12 InvalidUrlFormat(String),
14 InvalidMaxClicks(u32),
16 InvalidEmojiSequence(String),
18}
19
20impl Display for ValidationError {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 match self {
23 ValidationError::InvalidPasswordFormat(msg) => {
24 write!(f, "Invalid password format: {}", msg)
25 }
26 ValidationError::InvalidAliasFormat(msg) => write!(f, "Invalid alias format: {}", msg),
27 ValidationError::InvalidUrlFormat(msg) => write!(f, "Invalid URL format: {}", msg),
28 ValidationError::InvalidMaxClicks(value) => {
29 write!(f, "Max-clicks must be a positive integer, got: {}", value)
30 }
31 ValidationError::InvalidEmojiSequence(seq) => {
32 write!(f, "Invalid emoji sequence: {}", seq)
33 }
34 }
35 }
36}
37
38#[derive(Debug, Error)]
40pub enum ApiError {
41 UrlError,
43 AliasError,
45 PasswordError,
47 MaxClicksError,
49 EmojiError,
51 RateLimitExceeded,
53 Other(String),
55}
56
57impl Display for ApiError {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 match self {
60 ApiError::UrlError => write!(f, "Invalid URL format"),
61 ApiError::AliasError => write!(f, "Alias already in use or invalid"),
62 ApiError::PasswordError => write!(f, "Incorrect password provided"),
63 ApiError::MaxClicksError => write!(f, "Invalid max clicks value"),
64 ApiError::EmojiError => write!(f, "Invalid or already used emoji sequence"),
65 ApiError::RateLimitExceeded => write!(f, "Rate limit exceeded for the API"),
66 ApiError::Other(msg) => write!(f, "API error: {}", msg),
67 }
68 }
69}
70
71#[derive(Debug, Error)]
73pub enum UrlShortenerError {
74 Validation(ValidationError),
76 Api(ApiError),
78 Http(reqwest::Error),
80 Json(serde_json::Error),
82 Other(String),
84}
85
86impl Display for UrlShortenerError {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 match self {
89 UrlShortenerError::Validation(err) => write!(f, "Validation error: {}", err),
90 UrlShortenerError::Api(err) => write!(f, "API error: {:?}", err),
91 UrlShortenerError::Http(err) => write!(f, "HTTP error: {}", err),
92 UrlShortenerError::Json(err) => write!(f, "JSON error: {}", err),
93 UrlShortenerError::Other(msg) => write!(f, "Other error: {}", msg),
94 }
95 }
96}