nmrs 2.3.0

A Rust library for NetworkManager over D-Bus
Documentation
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Core connection builder for NetworkManager settings.
//!
//! This module provides a flexible builder API for constructing NetworkManager
//! connection settings dictionaries. The `ConnectionBuilder` handles common
//! sections like connection metadata, IPv4/IPv6 configuration, and allows
//! type-specific builders to add their own sections.
//!
//! # Design Philosophy
//!
//! The builder follows a "base + specialization" pattern:
//! - `ConnectionBuilder` handles common sections (connection, ipv4, ipv6)
//! - Type-specific builders (WifiConnectionBuilder, VpnBuilder, etc.) add
//!   connection-type-specific sections and provide ergonomic APIs
//!
//! # Example
//!
//! ```rust
//! use nmrs::builders::ConnectionBuilder;
//! use std::net::Ipv4Addr;
//!
//! let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
//!     .autoconnect(true)
//!     .ipv4_auto()
//!     .ipv6_auto()
//!     .build();
//! ```

use std::collections::HashMap;
use std::net::{Ipv4Addr, Ipv6Addr};
use uuid::Uuid;
use zvariant::Value;

use crate::api::models::ConnectionOptions;

/// IP address configuration with CIDR prefix.
#[derive(Debug, Clone)]
pub struct IpConfig {
    pub address: String,
    pub prefix: u32,
}

impl IpConfig {
    /// Creates a new IP configuration.
    #[must_use]
    pub fn new(address: impl Into<String>, prefix: u32) -> Self {
        Self {
            address: address.into(),
            prefix,
        }
    }
}

/// Route configuration for static routing.
#[derive(Debug, Clone)]
pub struct Route {
    pub dest: String,
    pub prefix: u32,
    pub next_hop: Option<String>,
    pub metric: Option<u32>,
}

impl Route {
    /// Creates a new route configuration.
    #[must_use]
    pub fn new(dest: impl Into<String>, prefix: u32) -> Self {
        Self {
            dest: dest.into(),
            prefix,
            next_hop: None,
            metric: None,
        }
    }

    /// Sets the next hop gateway for this route.
    #[must_use]
    pub fn next_hop(mut self, gateway: impl Into<String>) -> Self {
        self.next_hop = Some(gateway.into());
        self
    }

    /// Sets the metric (priority) for this route.
    #[must_use]
    pub fn metric(mut self, metric: u32) -> Self {
        self.metric = Some(metric);
        self
    }
}

/// Core connection settings builder.
///
/// This builder constructs the base NetworkManager connection settings dictionary
/// that all connection types share. Type-specific builders wrap this to add
/// their own sections.
///
/// # Sections Managed
///
/// - `connection`: Metadata (type, id, uuid, autoconnect settings)
/// - `ipv4`: IPv4 configuration (auto/manual/disabled/etc)
/// - `ipv6`: IPv6 configuration (auto/manual/ignore/etc)
///
/// # Usage Pattern
///
/// This builder is typically wrapped by type-specific builders like
/// `WifiConnectionBuilder` or `EthernetConnectionBuilder`. However, it can
/// be used directly for advanced use cases:
///
/// ```rust
/// use nmrs::builders::ConnectionBuilder;
///
/// let settings = ConnectionBuilder::new("802-11-wireless", "MyNetwork")
///     .autoconnect(true)
///     .autoconnect_priority(10)
///     .ipv4_auto()
///     .ipv6_auto()
///     .build();
/// ```
pub struct ConnectionBuilder {
    settings: HashMap<&'static str, HashMap<&'static str, Value<'static>>>,
}

impl ConnectionBuilder {
    /// Creates a new connection builder with the specified type and ID.
    ///
    /// # Arguments
    ///
    /// * `connection_type` - NetworkManager connection type (e.g., "802-11-wireless",
    ///   "802-3-ethernet", "wireguard", "bridge", "bond", "vlan")
    /// * `id` - Human-readable connection identifier
    ///
    /// # Example
    ///
    /// ```rust
    /// use nmrs::builders::ConnectionBuilder;
    ///
    /// let builder = ConnectionBuilder::new("802-11-wireless", "HomeNetwork");
    /// ```
    #[must_use]
    pub fn new(connection_type: &str, id: impl Into<String>) -> Self {
        let mut settings = HashMap::new();
        let mut connection = HashMap::new();

        connection.insert("type", Value::from(connection_type.to_string()));
        connection.insert("id", Value::from(id.into()));
        connection.insert("uuid", Value::from(Uuid::new_v4().to_string()));

        settings.insert("connection", connection);

        Self { settings }
    }

    /// Sets a specific UUID for the connection.
    ///
    /// By default, a random UUID is generated. Use this to specify a deterministic
    /// UUID for testing or when recreating existing connections.
    #[must_use]
    pub fn uuid(mut self, uuid: Uuid) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("uuid", Value::from(uuid.to_string()));
        }
        self
    }

    /// Sets the network interface name for this connection.
    ///
    /// This restricts the connection to a specific interface (e.g., "wlan0", "eth0").
    #[must_use]
    pub fn interface_name(mut self, name: impl Into<String>) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("interface-name", Value::from(name.into()));
        }
        self
    }

    /// Enables or disables automatic connection on boot/availability.
    #[must_use]
    pub fn autoconnect(mut self, enabled: bool) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("autoconnect", Value::from(enabled));
        }
        self
    }

    /// Sets the autoconnect priority (higher values are preferred).
    ///
    /// When multiple connections are available, NetworkManager connects to the
    /// one with the highest priority. Default is 0.
    #[must_use]
    pub fn autoconnect_priority(mut self, priority: i32) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("autoconnect-priority", Value::from(priority));
        }
        self
    }

    /// Sets the number of autoconnect retry attempts.
    ///
    /// After this many failed attempts, the connection won't auto-retry.
    /// Default is -1 (unlimited retries).
    #[must_use]
    pub fn autoconnect_retries(mut self, retries: i32) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("autoconnect-retries", Value::from(retries));
        }
        self
    }

    /// Applies multiple connection options at once.
    ///
    /// This is a convenience method to apply all fields from `ConnectionOptions`.
    #[must_use]
    pub fn options(mut self, opts: &ConnectionOptions) -> Self {
        if let Some(conn) = self.settings.get_mut("connection") {
            conn.insert("autoconnect", Value::from(opts.autoconnect));

            if let Some(priority) = opts.autoconnect_priority {
                conn.insert("autoconnect-priority", Value::from(priority));
            }

            if let Some(retries) = opts.autoconnect_retries {
                conn.insert("autoconnect-retries", Value::from(retries));
            }
        }
        self
    }

    /// Configures IPv4 to use automatic configuration (DHCP).
    #[must_use]
    pub fn ipv4_auto(mut self) -> Self {
        let mut ipv4 = HashMap::new();
        ipv4.insert("method", Value::from("auto"));
        self.settings.insert("ipv4", ipv4);
        self
    }

    /// Configures IPv4 with manual (static) addresses.
    ///
    /// # Example
    ///
    /// ```rust
    /// use nmrs::builders::{ConnectionBuilder, IpConfig};
    ///
    /// let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
    ///     .ipv4_manual(vec![
    ///         IpConfig::new("192.168.1.100", 24),
    ///     ])
    ///     .build();
    /// ```
    #[must_use]
    pub fn ipv4_manual(mut self, addresses: Vec<IpConfig>) -> Self {
        let mut ipv4 = HashMap::new();
        ipv4.insert("method", Value::from("manual"));

        // Convert to address-data format (array of dictionaries)
        let address_data: Vec<HashMap<String, Value<'static>>> = addresses
            .into_iter()
            .map(|config| {
                let mut addr_dict = HashMap::new();
                addr_dict.insert("address".to_string(), Value::from(config.address));
                addr_dict.insert("prefix".to_string(), Value::from(config.prefix));
                addr_dict
            })
            .collect();

        ipv4.insert("address-data", Value::from(address_data));
        self.settings.insert("ipv4", ipv4);
        self
    }

    /// Disables IPv4 for this connection.
    #[must_use]
    pub fn ipv4_disabled(mut self) -> Self {
        let mut ipv4 = HashMap::new();
        ipv4.insert("method", Value::from("disabled"));
        self.settings.insert("ipv4", ipv4);
        self
    }

    /// Configures IPv4 to use link-local addressing (169.254.x.x).
    #[must_use]
    pub fn ipv4_link_local(mut self) -> Self {
        let mut ipv4 = HashMap::new();
        ipv4.insert("method", Value::from("link-local"));
        self.settings.insert("ipv4", ipv4);
        self
    }

    /// Configures IPv4 for internet connection sharing.
    ///
    /// The connection will provide DHCP and NAT for other devices.
    #[must_use]
    pub fn ipv4_shared(mut self) -> Self {
        let mut ipv4 = HashMap::new();
        ipv4.insert("method", Value::from("shared"));
        self.settings.insert("ipv4", ipv4);
        self
    }

    /// Sets IPv4 DNS servers.
    ///
    /// DNS servers are specified as integers (network byte order).
    #[must_use]
    pub fn ipv4_dns(mut self, servers: Vec<Ipv4Addr>) -> Self {
        let dns_u32: Vec<u32> = servers.into_iter().map(u32::from).collect();

        if let Some(ipv4) = self.settings.get_mut("ipv4") {
            ipv4.insert("dns", Value::from(dns_u32));
        }
        self
    }

    /// Sets the IPv4 gateway.
    #[must_use]
    pub fn ipv4_gateway(mut self, gateway: Ipv4Addr) -> Self {
        if let Some(ipv4) = self.settings.get_mut("ipv4") {
            ipv4.insert("gateway", Value::from(gateway.to_string()));
        }
        self
    }

    /// Adds IPv4 static routes.
    #[must_use]
    pub fn ipv4_routes(mut self, routes: Vec<Route>) -> Self {
        let route_data: Vec<HashMap<String, Value<'static>>> = routes
            .into_iter()
            .map(|route| {
                let mut route_dict = HashMap::new();
                route_dict.insert("dest".to_string(), Value::from(route.dest));
                route_dict.insert("prefix".to_string(), Value::from(route.prefix));

                if let Some(next_hop) = route.next_hop {
                    route_dict.insert("next-hop".to_string(), Value::from(next_hop));
                }

                if let Some(metric) = route.metric {
                    route_dict.insert("metric".to_string(), Value::from(metric));
                }

                route_dict
            })
            .collect();

        if let Some(ipv4) = self.settings.get_mut("ipv4") {
            ipv4.insert("route-data", Value::from(route_data));
        }
        self
    }

    /// Configures IPv6 to use automatic configuration (SLAAC/DHCPv6).
    #[must_use]
    pub fn ipv6_auto(mut self) -> Self {
        let mut ipv6 = HashMap::new();
        ipv6.insert("method", Value::from("auto"));
        self.settings.insert("ipv6", ipv6);
        self
    }

    /// Configures IPv6 with manual (static) addresses.
    #[must_use]
    pub fn ipv6_manual(mut self, addresses: Vec<IpConfig>) -> Self {
        let mut ipv6 = HashMap::new();
        ipv6.insert("method", Value::from("manual"));

        let address_data: Vec<HashMap<String, Value<'static>>> = addresses
            .into_iter()
            .map(|config| {
                let mut addr_dict = HashMap::new();
                addr_dict.insert("address".to_string(), Value::from(config.address));
                addr_dict.insert("prefix".to_string(), Value::from(config.prefix));
                addr_dict
            })
            .collect();

        ipv6.insert("address-data", Value::from(address_data));
        self.settings.insert("ipv6", ipv6);
        self
    }

    /// Disables IPv6 for this connection.
    #[must_use]
    pub fn ipv6_ignore(mut self) -> Self {
        let mut ipv6 = HashMap::new();
        ipv6.insert("method", Value::from("ignore"));
        self.settings.insert("ipv6", ipv6);
        self
    }

    /// Configures IPv6 to use link-local addressing only.
    #[must_use]
    pub fn ipv6_link_local(mut self) -> Self {
        let mut ipv6 = HashMap::new();
        ipv6.insert("method", Value::from("link-local"));
        self.settings.insert("ipv6", ipv6);
        self
    }

    /// Sets IPv6 DNS servers.
    #[must_use]
    pub fn ipv6_dns(mut self, servers: Vec<Ipv6Addr>) -> Self {
        let dns_strings: Vec<String> = servers.into_iter().map(|s| s.to_string()).collect();

        if let Some(ipv6) = self.settings.get_mut("ipv6") {
            ipv6.insert("dns", Value::from(dns_strings));
        }
        self
    }

    /// Sets the IPv6 gateway.
    #[must_use]
    pub fn ipv6_gateway(mut self, gateway: Ipv6Addr) -> Self {
        if let Some(ipv6) = self.settings.get_mut("ipv6") {
            ipv6.insert("gateway", Value::from(gateway.to_string()));
        }
        self
    }

    /// Adds IPv6 static routes.
    #[must_use]
    pub fn ipv6_routes(mut self, routes: Vec<Route>) -> Self {
        let route_data: Vec<HashMap<String, Value<'static>>> = routes
            .into_iter()
            .map(|route| {
                let mut route_dict = HashMap::new();
                route_dict.insert("dest".to_string(), Value::from(route.dest));
                route_dict.insert("prefix".to_string(), Value::from(route.prefix));

                if let Some(next_hop) = route.next_hop {
                    route_dict.insert("next-hop".to_string(), Value::from(next_hop));
                }

                if let Some(metric) = route.metric {
                    route_dict.insert("metric".to_string(), Value::from(metric));
                }

                route_dict
            })
            .collect();

        if let Some(ipv6) = self.settings.get_mut("ipv6") {
            ipv6.insert("route-data", Value::from(route_data));
        }
        self
    }

    /// Adds or replaces a complete settings section.
    ///
    /// This is useful for type-specific settings that don't have dedicated
    /// builder methods. For example, adding "802-11-wireless" or "wireguard"
    /// sections.
    ///
    /// # Example
    ///
    /// ```rust
    /// use nmrs::builders::ConnectionBuilder;
    /// use std::collections::HashMap;
    /// use zvariant::Value;
    ///
    /// let mut bridge_section = HashMap::new();
    /// bridge_section.insert("stp", Value::from(true));
    ///
    /// let settings = ConnectionBuilder::new("bridge", "br0")
    ///     .with_section("bridge", bridge_section)
    ///     .build();
    /// ```
    #[must_use]
    pub fn with_section(
        mut self,
        name: &'static str,
        section: HashMap<&'static str, Value<'static>>,
    ) -> Self {
        self.settings.insert(name, section);
        self
    }

    /// Updates an existing section using a closure.
    ///
    /// This allows modifying a section after it's been created, which is useful
    /// when a builder method creates a base section and you need to add extra fields.
    ///
    /// # Example
    ///
    /// ```rust
    /// use nmrs::builders::ConnectionBuilder;
    /// use zvariant::Value;
    ///
    /// let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
    ///     .ipv4_auto()
    ///     .update_section("ipv4", |ipv4| {
    ///         ipv4.insert("may-fail", Value::from(false));
    ///     })
    ///     .build();
    /// ```
    #[must_use]
    pub fn update_section<F>(mut self, name: &'static str, f: F) -> Self
    where
        F: FnOnce(&mut HashMap<&'static str, Value<'static>>),
    {
        if let Some(section) = self.settings.get_mut(name) {
            f(section);
        }
        self
    }

    /// Builds and returns the final settings dictionary.
    ///
    /// This consumes the builder and returns the complete settings structure
    /// ready to be passed to NetworkManager's D-Bus API.
    #[must_use]
    pub fn build(self) -> HashMap<&'static str, HashMap<&'static str, Value<'static>>> {
        self.settings
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn creates_basic_connection() {
        let settings = ConnectionBuilder::new("802-11-wireless", "TestNetwork").build();

        assert!(settings.contains_key("connection"));
        let conn = settings.get("connection").unwrap();
        assert_eq!(conn.get("type"), Some(&Value::from("802-11-wireless")));
        assert_eq!(conn.get("id"), Some(&Value::from("TestNetwork")));
        assert!(conn.contains_key("uuid"));
    }

    #[test]
    fn sets_custom_uuid() {
        let test_uuid = Uuid::new_v4();
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .uuid(test_uuid)
            .build();

        let conn = settings.get("connection").unwrap();
        assert_eq!(conn.get("uuid"), Some(&Value::from(test_uuid.to_string())));
    }

    #[test]
    fn sets_interface_name() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "MyConnection")
            .interface_name("eth0")
            .build();

        let conn = settings.get("connection").unwrap();
        assert_eq!(conn.get("interface-name"), Some(&Value::from("eth0")));
    }

    #[test]
    fn configures_autoconnect() {
        let settings = ConnectionBuilder::new("802-11-wireless", "test")
            .autoconnect(false)
            .autoconnect_priority(10)
            .autoconnect_retries(3)
            .build();

        let conn = settings.get("connection").unwrap();
        assert_eq!(conn.get("autoconnect"), Some(&Value::from(false)));
        assert_eq!(conn.get("autoconnect-priority"), Some(&Value::from(10i32)));
        assert_eq!(conn.get("autoconnect-retries"), Some(&Value::from(3i32)));
    }

    #[test]
    fn applies_connection_options() {
        let opts = ConnectionOptions {
            autoconnect: true,
            autoconnect_priority: Some(5),
            autoconnect_retries: Some(2),
        };

        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .options(&opts)
            .build();

        let conn = settings.get("connection").unwrap();
        assert_eq!(conn.get("autoconnect"), Some(&Value::from(true)));
        assert_eq!(conn.get("autoconnect-priority"), Some(&Value::from(5i32)));
        assert_eq!(conn.get("autoconnect-retries"), Some(&Value::from(2i32)));
    }

    #[test]
    fn configures_ipv4_auto() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_auto()
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert_eq!(ipv4.get("method"), Some(&Value::from("auto")));
    }

    #[test]
    fn configures_ipv4_manual() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_manual(vec![IpConfig::new("192.168.1.100", 24)])
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert_eq!(ipv4.get("method"), Some(&Value::from("manual")));
        assert!(ipv4.contains_key("address-data"));
    }

    #[test]
    fn configures_ipv4_disabled() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_disabled()
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert_eq!(ipv4.get("method"), Some(&Value::from("disabled")));
    }

    #[test]
    fn configures_ipv4_dns() {
        let dns = vec!["8.8.8.8".parse().unwrap(), "1.1.1.1".parse().unwrap()];
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_auto()
            .ipv4_dns(dns)
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert!(ipv4.contains_key("dns"));
    }

    #[test]
    fn configures_ipv6_auto() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv6_auto()
            .build();

        let ipv6 = settings.get("ipv6").unwrap();
        assert_eq!(ipv6.get("method"), Some(&Value::from("auto")));
    }

    #[test]
    fn configures_ipv6_ignore() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv6_ignore()
            .build();

        let ipv6 = settings.get("ipv6").unwrap();
        assert_eq!(ipv6.get("method"), Some(&Value::from("ignore")));
    }

    #[test]
    fn adds_custom_section() {
        let mut bridge = HashMap::new();
        bridge.insert("stp", Value::from(true));

        let settings = ConnectionBuilder::new("bridge", "br0")
            .with_section("bridge", bridge)
            .build();

        assert!(settings.contains_key("bridge"));
        let bridge_section = settings.get("bridge").unwrap();
        assert_eq!(bridge_section.get("stp"), Some(&Value::from(true)));
    }

    #[test]
    fn updates_existing_section() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_auto()
            .update_section("ipv4", |ipv4| {
                ipv4.insert("may-fail", Value::from(false));
            })
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert_eq!(ipv4.get("may-fail"), Some(&Value::from(false)));
    }

    #[test]
    fn configures_complete_static_ipv4() {
        let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
            .ipv4_manual(vec![IpConfig::new("192.168.1.100", 24)])
            .ipv4_gateway("192.168.1.1".parse().unwrap())
            .ipv4_dns(vec!["8.8.8.8".parse().unwrap()])
            .build();

        let ipv4 = settings.get("ipv4").unwrap();
        assert_eq!(ipv4.get("method"), Some(&Value::from("manual")));
        assert!(ipv4.contains_key("address-data"));
        assert!(ipv4.contains_key("gateway"));
        assert!(ipv4.contains_key("dns"));
    }
}