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
#[cfg(feature = "host-name")]
#[allow(dead_code)]
pub(crate) fn get_host_name() -> &'static str {
use once_cell::sync::OnceCell;
static HOST_NAME: OnceCell<String> = OnceCell::new();
HOST_NAME.get_or_init(|| {
hostname::get()
.ok()
.and_then(|hostname| hostname.into_string().ok())
.unwrap_or_else(|| "unknown".to_string())
})
}
#[cfg(feature = "host-ip")]
#[allow(dead_code)]
pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] {
use once_cell::sync::OnceCell;
use systemstat::{data::IpAddr, platform::common::Platform, System};
static ALL_ADDRS: OnceCell<Vec<std::net::IpAddr>> = OnceCell::new();
ALL_ADDRS.get_or_init(|| {
System::new()
.networks()
.ok()
.map(|networks| {
networks
.values()
.map(|network| {
network
.addrs
.iter()
.filter_map(|network_addr| match network_addr.addr {
IpAddr::V4(addr) => {
if addr.is_loopback() {
None
} else {
Some(std::net::IpAddr::V4(addr))
}
}
IpAddr::V6(addr) => {
if addr.is_loopback() {
None
} else {
Some(std::net::IpAddr::V6(addr))
}
}
_ => None,
})
})
.flatten()
.collect()
})
.unwrap_or_default()
})
}
pub fn canonicalize_namespace(namespace: &str) -> String {
if namespace.ends_with(".properties")
|| namespace.ends_with(".xml")
|| namespace.ends_with(".json")
|| namespace.ends_with(".yaml")
|| namespace.ends_with(".yml")
|| namespace.ends_with(".txt")
{
namespace.to_string()
} else {
format!("{}.properties", namespace)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_canonicalize_namespace() {
assert_eq!(canonicalize_namespace("foo.properties"), "foo.properties");
assert_eq!(canonicalize_namespace("foo.xml"), "foo.xml");
assert_eq!(canonicalize_namespace("foo.yaml"), "foo.yaml");
assert_eq!(canonicalize_namespace("foo.yml"), "foo.yml");
assert_eq!(canonicalize_namespace("foo.json"), "foo.json");
assert_eq!(canonicalize_namespace("foo.txt"), "foo.txt");
assert_eq!(canonicalize_namespace("foo"), "foo.properties");
}
}