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
|
//! Define the `Anonymity` type to indicate a level of anonymity.
/// The level of anonymity that an onion service should try to run with.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum Anonymity {
/// Try to keep the location of the onion service private.
///
/// Can be represented in a serde-based configuration as `true` or
/// `"anonymous"` (case insensitive).
#[default]
Anonymous,
/// Do not try to keep the location of the onion service private.
///
/// (This is implemented using our "single onion service" design.)
///
/// Can be represented in a serde-based configuration as`"non_anonymous"`
/// (case insensitive).
DangerouslyNonAnonymous,
}
/// A string used to represent `Anonymity::Anonymous` in serde.
const ANON_STRING: &str = "anonymous";
/// A string used to represent `Anonymity::DangerouslyNonAnonymous` in serde.
const DANGER_STRING: &str = "not_anonymous";
impl serde::Serialize for Anonymity {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Anonymity::Anonymous => serializer.serialize_bool(true),
Anonymity::DangerouslyNonAnonymous => serializer.serialize_str(DANGER_STRING),
}
}
}
impl<'de> serde::Deserialize<'de> for Anonymity {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
/// Visitor struct to deserialize an Anonymity object.
struct Vis;
impl<'de> serde::de::Visitor<'de> for Vis {
type Value = Anonymity;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
formatter,
r#"`true`, `{:?}`, or `{:?}`"#,
ANON_STRING, DANGER_STRING
)
}
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v {
Ok(Anonymity::Anonymous)
} else {
Err(E::invalid_value(serde::de::Unexpected::Bool(v), &self))
}
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if s.eq_ignore_ascii_case(ANON_STRING) {
Ok(Anonymity::Anonymous)
} else if s.eq_ignore_ascii_case(DANGER_STRING) {
Ok(Anonymity::DangerouslyNonAnonymous)
} else {
Err(E::invalid_value(serde::de::Unexpected::Str(s), &self))
}
}
}
deserializer.deserialize_any(Vis)
}
}
|