1#[cfg(feature = "host-name")]
4#[allow(dead_code)]
5pub(crate) fn get_host_name() -> &'static str {
6 use once_cell::sync::OnceCell;
7
8 static HOST_NAME: OnceCell<String> = OnceCell::new();
9 HOST_NAME.get_or_init(|| {
10 hostname::get()
11 .ok()
12 .and_then(|hostname| hostname.into_string().ok())
13 .unwrap_or_else(|| "unknown".to_string())
14 })
15}
16
17#[cfg(feature = "host-ip")]
18#[allow(dead_code)]
19pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] {
20 use once_cell::sync::OnceCell;
21 use systemstat::{data::IpAddr, platform::common::Platform, System};
22
23 static ALL_ADDRS: OnceCell<Vec<std::net::IpAddr>> = OnceCell::new();
24 ALL_ADDRS.get_or_init(|| {
25 System::new()
26 .networks()
27 .ok()
28 .map(|networks| {
29 networks
30 .values()
31 .flat_map(|network| {
32 network
33 .addrs
34 .iter()
35 .filter_map(|network_addr| match network_addr.addr {
36 IpAddr::V4(addr) => {
37 if addr.is_loopback() {
38 None
39 } else {
40 Some(std::net::IpAddr::V4(addr))
41 }
42 }
43 IpAddr::V6(addr) => {
44 if addr.is_loopback() {
45 None
46 } else {
47 Some(std::net::IpAddr::V6(addr))
48 }
49 }
50 _ => None,
51 })
52 })
53 .collect()
54 })
55 .unwrap_or_default()
56 })
57}
58
59pub fn canonicalize_namespace(namespace: &str) -> String {
70 if namespace.ends_with(".properties")
71 || namespace.ends_with(".xml")
72 || namespace.ends_with(".json")
73 || namespace.ends_with(".yaml")
74 || namespace.ends_with(".yml")
75 || namespace.ends_with(".txt")
76 {
77 namespace.to_string()
78 } else {
79 format!("{}.properties", namespace)
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_canonicalize_namespace() {
89 assert_eq!(canonicalize_namespace("foo.properties"), "foo.properties");
90 assert_eq!(canonicalize_namespace("foo.xml"), "foo.xml");
91 assert_eq!(canonicalize_namespace("foo.yaml"), "foo.yaml");
92 assert_eq!(canonicalize_namespace("foo.yml"), "foo.yml");
93 assert_eq!(canonicalize_namespace("foo.json"), "foo.json");
94 assert_eq!(canonicalize_namespace("foo.txt"), "foo.txt");
95 assert_eq!(canonicalize_namespace("foo"), "foo.properties");
96 }
97}