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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use super::{file, PhotoSize};
use is_macro::Is;
use serde::de::{Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor};
use std::fmt::{self, Formatter};
pub mod mask_position;
pub mod set;
pub use {mask_position::MaskPosition, set::Set};
#[derive(Debug, PartialEq, Clone, Copy, Is)]
#[non_exhaustive]
pub enum Kind {
Plain,
Animated,
Mask(MaskPosition),
}
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub struct Sticker {
pub file_id: file::Id,
pub file_unique_id: String,
pub width: u32,
pub height: u32,
pub thumb: Option<PhotoSize>,
pub emoji: Option<String>,
pub set_name: Option<String>,
pub file_size: Option<u32>,
pub kind: Kind,
}
const FILE_ID: &str = "file_id";
const FILE_UNIQUE_ID: &str = "file_unique_id";
const WIDTH: &str = "width";
const HEIGHT: &str = "height";
const IS_ANIMATED: &str = "is_animated";
const THUMB: &str = "thumb";
const EMOJI: &str = "emoji";
const SET_NAME: &str = "set_name";
const MASK_POSITION: &str = "mask_position";
const FILE_SIZE: &str = "file_size";
struct StickerVisitor;
impl<'v> Visitor<'v> for StickerVisitor {
type Value = Sticker;
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "struct Sticker")
}
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: MapAccess<'v>,
{
let mut file_id = None;
let mut file_unique_id = None;
let mut width = None;
let mut height = None;
let mut is_animated = None;
let mut thumb = None;
let mut emoji = None;
let mut set_name = None;
let mut mask_position = None;
let mut file_size = None;
while let Some(key) = map.next_key()? {
match key {
FILE_ID => file_id = Some(map.next_value()?),
FILE_UNIQUE_ID => file_unique_id = Some(map.next_value()?),
WIDTH => width = Some(map.next_value()?),
HEIGHT => height = Some(map.next_value()?),
IS_ANIMATED => is_animated = Some(map.next_value()?),
THUMB => thumb = Some(map.next_value()?),
EMOJI => emoji = Some(map.next_value()?),
SET_NAME => set_name = Some(map.next_value()?),
MASK_POSITION => mask_position = Some(map.next_value()?),
FILE_SIZE => file_size = Some(map.next_value()?),
_ => {
let _ = map.next_value::<IgnoredAny>()?;
}
}
}
#[allow(clippy::option_if_let_else)]
let kind = if let Some(mask_position) = mask_position {
Kind::Mask(mask_position)
} else if is_animated == Some(true) {
Kind::Animated
} else {
Kind::Plain
};
Ok(Sticker {
file_id: file_id
.ok_or_else(|| serde::de::Error::missing_field(FILE_ID))?,
file_unique_id: file_unique_id.ok_or_else(|| {
serde::de::Error::missing_field(FILE_UNIQUE_ID)
})?,
width: width
.ok_or_else(|| serde::de::Error::missing_field(WIDTH))?,
height: height
.ok_or_else(|| serde::de::Error::missing_field(HEIGHT))?,
thumb,
emoji,
set_name,
file_size,
kind,
})
}
}
impl<'de> Deserialize<'de> for Sticker {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
d.deserialize_struct(
"Sticker",
&[
FILE_ID,
FILE_UNIQUE_ID,
WIDTH,
HEIGHT,
IS_ANIMATED,
THUMB,
EMOJI,
SET_NAME,
MASK_POSITION,
FILE_SIZE,
],
StickerVisitor,
)
}
}