-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathservice_role.rs
More file actions
95 lines (84 loc) · 2.92 KB
/
Copy pathservice_role.rs
File metadata and controls
95 lines (84 loc) · 2.92 KB
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
use std::fmt;
use serde::{Deserialize, Serialize};
// issue: #2036
/// A tracker application service role.
///
/// This role identifies the application behavior implemented by a listener.
/// It does not identify its transport or socket binding: HTTP and HTTPS both
/// use [`Self::HttpTracker`] and differ through their service binding.
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum ServiceRole {
/// A `BitTorrent` HTTP or HTTPS tracker service.
HttpTracker,
/// A `BitTorrent` UDP tracker service.
UdpTracker,
/// The tracker management REST API service.
#[serde(rename = "tracker_rest_api")]
RestApi,
/// The tracker health-check API service.
HealthCheckApi,
}
impl ServiceRole {
/// Returns the stable tracker-owned identifier for this role.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::HttpTracker => "http_tracker",
Self::UdpTracker => "udp_tracker",
Self::RestApi => "tracker_rest_api",
Self::HealthCheckApi => "health_check_api",
}
}
}
impl fmt::Display for ServiceRole {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use crate::ServiceRole;
#[test]
fn it_should_return_the_canonical_identifier_for_each_role() {
// Arrange
let roles = [
(ServiceRole::HttpTracker, "http_tracker"),
(ServiceRole::UdpTracker, "udp_tracker"),
(ServiceRole::RestApi, "tracker_rest_api"),
(ServiceRole::HealthCheckApi, "health_check_api"),
];
// Act and Assert
for (service_role, identifier) in roles {
assert_eq!(service_role.as_str(), identifier);
}
}
#[test]
fn it_should_display_the_canonical_identifier_for_each_role() {
// Arrange
let roles = [
(ServiceRole::HttpTracker, "http_tracker"),
(ServiceRole::UdpTracker, "udp_tracker"),
(ServiceRole::RestApi, "tracker_rest_api"),
(ServiceRole::HealthCheckApi, "health_check_api"),
];
// Act and Assert
for (service_role, identifier) in roles {
assert_eq!(service_role.to_string(), identifier);
}
}
#[test]
fn it_should_serialize_each_role_to_its_canonical_identifier() {
// Arrange
let roles = [
(ServiceRole::HttpTracker, r#""http_tracker""#),
(ServiceRole::UdpTracker, r#""udp_tracker""#),
(ServiceRole::RestApi, r#""tracker_rest_api""#),
(ServiceRole::HealthCheckApi, r#""health_check_api""#),
];
// Act and Assert
for (service_role, identifier) in roles {
assert_eq!(serde_json::to_string(&service_role).unwrap(), identifier);
}
}
}