1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::{
convert::TryFrom,
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
#[derive(
Clone, Copy, Debug, Deserialize_repr, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize_repr,
)]
#[repr(u8)]
pub enum StickerFormatType {
Png = 1,
Apng = 2,
Lottie = 3,
}
impl TryFrom<u8> for StickerFormatType {
type Error = StickerFormatTypeConversionError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
1 => StickerFormatType::Png,
2 => StickerFormatType::Apng,
3 => StickerFormatType::Lottie,
_ => return Err(StickerFormatTypeConversionError { value }),
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StickerFormatTypeConversionError {
value: u8,
}
impl<'a> StickerFormatTypeConversionError {
pub fn value(&self) -> u8 {
self.value
}
}
impl Display for StickerFormatTypeConversionError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_fmt(format_args!(
"Value ({}) doesn't match a sticker type",
self.value,
))
}
}
impl Error for StickerFormatTypeConversionError {}
#[cfg(test)]
mod tests {
use super::StickerFormatType;
use serde_test::Token;
use std::convert::TryFrom;
#[test]
fn test_variants() {
serde_test::assert_tokens(&StickerFormatType::Png, &[Token::U8(1)]);
serde_test::assert_tokens(&StickerFormatType::Apng, &[Token::U8(2)]);
serde_test::assert_tokens(&StickerFormatType::Lottie, &[Token::U8(3)]);
}
#[test]
fn test_conversions() {
assert_eq!(
StickerFormatType::try_from(1).unwrap(),
StickerFormatType::Png
);
assert_eq!(
StickerFormatType::try_from(2).unwrap(),
StickerFormatType::Apng
);
assert_eq!(
StickerFormatType::try_from(3).unwrap(),
StickerFormatType::Lottie
);
}
}