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
use crate::id::{IntegrationId, UserId};
use serde::{Deserialize, Serialize};
mod premium_subscriber {
use serde::{
de::{Deserializer, Error as DeError, Visitor},
ser::Serializer,
};
use std::fmt::{Formatter, Result as FmtResult};
struct PremiumSubscriberVisitor;
impl<'de> Visitor<'de> for PremiumSubscriberVisitor {
type Value = bool;
fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("null")
}
fn visit_none<E: DeError>(self) -> Result<Self::Value, E> {
Ok(true)
}
fn visit_unit<E: DeError>(self) -> Result<Self::Value, E> {
Ok(true)
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S: Serializer>(_: &bool, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_none()
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {
deserializer.deserialize_option(PremiumSubscriberVisitor)
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(value: &bool) -> bool {
!value
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct RoleTags {
#[serde(skip_serializing_if = "Option::is_none")]
pub bot_id: Option<UserId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub integration_id: Option<IntegrationId>,
#[serde(default, skip_serializing_if = "is_false", with = "premium_subscriber")]
pub premium_subscriber: bool,
}
#[cfg(test)]
mod tests {
use super::RoleTags;
use crate::id::{IntegrationId, UserId};
use serde_test::Token;
#[test]
fn test_role_tags_all() {
let tags = RoleTags {
bot_id: Some(UserId(1)),
integration_id: Some(IntegrationId(2)),
premium_subscriber: true,
};
serde_test::assert_tokens(
&tags,
&[
Token::Struct {
name: "RoleTags",
len: 3,
},
Token::Str("bot_id"),
Token::Some,
Token::NewtypeStruct { name: "UserId" },
Token::Str("1"),
Token::Str("integration_id"),
Token::Some,
Token::NewtypeStruct {
name: "IntegrationId",
},
Token::Str("2"),
Token::Str("premium_subscriber"),
Token::None,
Token::StructEnd,
],
);
}
#[test]
fn test_role_tags_none() {
let tags = RoleTags {
bot_id: None,
integration_id: None,
premium_subscriber: false,
};
serde_test::assert_tokens(
&tags,
&[
Token::Struct {
name: "RoleTags",
len: 0,
},
Token::StructEnd,
],
);
}
}