capybara_util/
lib.rs

1#![allow(clippy::type_complexity)]
2#![allow(clippy::from_over_into)]
3#![allow(clippy::module_inception)]
4#![allow(clippy::upper_case_acronyms)]
5#![doc(test(
6    no_crate_inject,
7    attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
8))]
9
10use std::net::IpAddr;
11
12use once_cell::sync::Lazy;
13
14pub use ifaddrs::IfAddrs;
15pub use rotate::{FileRotate, RotationMode};
16pub use weighted::{WeightedResource, WeightedResourceBuilder};
17
18static IP: Lazy<Option<IpAddr>> = Lazy::new(|| {
19    if let Ok(addrs) = IfAddrs::get() {
20        for next in addrs.iter() {
21            if let Some(addr) = next.addr() {
22                if addr.is_ipv4() && !addr.is_loopback() {
23                    return Some(addr);
24                }
25            }
26        }
27    }
28
29    None
30});
31
32pub fn local_addr() -> Option<IpAddr> {
33    Clone::clone(&IP)
34}
35
36/// cached string
37pub mod cachestr {
38    include!(concat!(env!("OUT_DIR"), "/cachestr.rs"));
39}
40
41mod ifaddrs;
42mod rotate;
43mod weighted;
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    fn init() {
50        pretty_env_logger::try_init_timed().ok();
51    }
52
53    #[test]
54    fn test_local_addr() {
55        init();
56        log::info!("local_addr: {:?}", &*IP);
57        assert!(IP.is_some());
58    }
59}