Skip to main content

macos_resolver/
config.rs

1//! Resolver entry configuration.
2
3/// Configuration for a single `/etc/resolver/<domain>` entry.
4///
5/// # Example
6///
7/// ```
8/// use macos_resolver::ResolverConfig;
9///
10/// let config = ResolverConfig::new("myapp.local", "127.0.0.1", 5553)
11///     .with_search_order(10);
12///
13/// assert_eq!(config.domain, "myapp.local");
14/// assert_eq!(config.port, 5553);
15/// assert_eq!(config.search_order, 10);
16/// ```
17#[derive(Debug, Clone)]
18pub struct ResolverConfig {
19    /// Domain suffix (e.g., `"myapp.local"`).
20    /// Becomes the filename under `/etc/resolver/`.
21    pub domain: String,
22
23    /// Nameserver IP address (e.g., `"127.0.0.1"`).
24    pub nameserver: String,
25
26    /// DNS port. Standard DNS uses 53; custom resolvers typically use a
27    /// high port (e.g., 5553) to avoid conflicts.
28    pub port: u16,
29
30    /// Search order — lower values are tried first.
31    pub search_order: u32,
32}
33
34impl ResolverConfig {
35    /// Creates a new resolver config with `search_order = 1`.
36    #[must_use]
37    pub fn new(domain: impl Into<String>, nameserver: impl Into<String>, port: u16) -> Self {
38        Self {
39            domain: domain.into(),
40            nameserver: nameserver.into(),
41            port,
42            search_order: 1,
43        }
44    }
45
46    /// Overrides the search order.
47    #[must_use]
48    pub const fn with_search_order(mut self, order: u32) -> Self {
49        self.search_order = order;
50        self
51    }
52
53    /// Convenience: `arcbox.local` pointing to `127.0.0.1:<port>`.
54    #[must_use]
55    pub fn arcbox_default(port: u16) -> Self {
56        Self::new("arcbox.local", "127.0.0.1", port)
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn new_sets_defaults() {
66        let c = ResolverConfig::new("test.local", "127.0.0.1", 5553);
67        assert_eq!(c.domain, "test.local");
68        assert_eq!(c.nameserver, "127.0.0.1");
69        assert_eq!(c.port, 5553);
70        assert_eq!(c.search_order, 1);
71    }
72
73    #[test]
74    fn with_search_order() {
75        let c = ResolverConfig::new("x.local", "127.0.0.1", 53).with_search_order(10);
76        assert_eq!(c.search_order, 10);
77    }
78
79    #[test]
80    fn arcbox_default() {
81        let c = ResolverConfig::arcbox_default(5553);
82        assert_eq!(c.domain, "arcbox.local");
83        assert_eq!(c.nameserver, "127.0.0.1");
84        assert_eq!(c.port, 5553);
85    }
86}