1use core::{error::Error, fmt::Debug, fmt::Display};
2
3#[non_exhaustive]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub(crate) enum Err {
7 TooShort(usize),
8 TooLong(usize),
9 NotAscii,
10}
11
12impl Display for Err {
13 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14 match self {
15 Self::TooShort(min) => {
16 write!(f, "minimum length allowed is {}", min)
17 }
18 Self::TooLong(max) => {
19 write!(f, "maximum length allowed is {}", max)
20 }
21 Self::NotAscii => {
22 write!(f, "only ASCII characters are allowed")
23 }
24 }
25 }
26}
27
28impl Error for Err {}
29
30impl<VE> From<Err> for GStringError<VE> {
31 fn from(value: Err) -> Self {
32 match value {
33 Err::TooShort(min) => Self::TooShort(min),
34 Err::TooLong(max) => Self::TooLong(max),
35 Err::NotAscii => Self::NotAscii,
36 }
37 }
38}
39
40#[cfg(test)]
41mod err_test {
42 use super::Err;
43
44 #[test]
45 fn test_err() {
46 let fmt_err = |err: Err| -> String { err.to_string() };
47
48 assert_eq!(fmt_err(Err::TooShort(0)), "minimum length allowed is 0");
49 assert_eq!(fmt_err(Err::TooLong(100)), "maximum length allowed is 100");
50 assert_eq!(fmt_err(Err::NotAscii), "only ASCII characters are allowed");
51 }
52}
53
54#[non_exhaustive]
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum GStringError<VE> {
58 TooShort(usize),
60 TooLong(usize),
62 NotAscii,
64 Validation(VE),
66 Mutation(&'static str),
68}
69
70impl<VE: Display + Debug> Display for GStringError<VE> {
71 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
72 match self {
73 Self::TooShort(min) => {
74 write!(f, "minimum length allowed is {}", min)
75 }
76 Self::TooLong(max) => {
77 write!(f, "maximum length allowed is {}", max)
78 }
79 Self::NotAscii => {
80 write!(f, "only ASCII characters are allowed")
81 }
82 Self::Validation(err) => write!(f, "validation error: {}", err),
83 Self::Mutation(err) => write!(f, "mutation error: {}", err),
84 }
85 }
86}
87
88impl<VE: Display + Debug> Error for GStringError<VE> {}