ports-manager 0.1.0

A simple, fast, and reliable CLI tool for managing port mappings
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum PortSpec {
    Single(u16),
    Range { start: u16, end: u16 },
}

impl PortSpec {
    pub fn parse(input: &str) -> Result<Self, String> {
        if let Some((start_str, end_str)) = input.split_once('-') {
            let start = start_str
                .trim()
                .parse::<u16>()
                .map_err(|_| format!("Invalid start port: {}", start_str))?;
            let end = end_str
                .trim()
                .parse::<u16>()
                .map_err(|_| format!("Invalid end port: {}", end_str))?;

            if start >= end {
                return Err("Start port must be less than end port".to_string());
            }

            Ok(PortSpec::Range { start, end })
        } else {
            let port = input
                .trim()
                .parse::<u16>()
                .map_err(|_| format!("Invalid port: {}", input))?;
            Ok(PortSpec::Single(port))
        }
    }

    pub fn display(&self) -> String {
        match self {
            PortSpec::Single(port) => port.to_string(),
            PortSpec::Range { start, end } => format!("{}-{}", start, end),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PortMapping {
    pub name: String,
    pub port: PortSpec,
    pub description: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub ports: Vec<PortMapping>,
    #[serde(default)]
    pub ignored_defaults: Vec<String>,
}

impl Config {
    pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
        let config_path = Self::config_path()?;

        if !config_path.exists() {
            // Create default config if it doesn't exist
            let default_config = Config::default();
            default_config.save()?;
            return Ok(default_config);
        }

        let content = fs::read_to_string(&config_path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }

    pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
        let config_path = Self::config_path()?;

        // Create config directory if it doesn't exist
        if let Some(parent) = config_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let content = toml::to_string_pretty(self)?;
        fs::write(&config_path, content)?;
        Ok(())
    }

    fn config_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
        let home_dir = directories::BaseDirs::new()
            .ok_or("Could not find home directory")?
            .home_dir()
            .to_path_buf();

        Ok(home_dir
            .join(".config")
            .join("ports-manager")
            .join("config.toml"))
    }

    fn config_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
        let home_dir = directories::BaseDirs::new()
            .ok_or("Could not find home directory")?
            .home_dir()
            .to_path_buf();

        Ok(home_dir.join(".config").join("ports-manager"))
    }

    pub fn add_port(&mut self, name: String, port: PortSpec, description: Option<String>) {
        self.ports.push(PortMapping {
            name,
            port,
            description,
        });
    }

    pub fn remove_port(&mut self, name: &str) -> bool {
        let original_len = self.ports.len();
        self.ports.retain(|p| p.name != name);
        self.ports.len() < original_len
    }

    pub fn find_port(&self, name: &str) -> Option<PortMapping> {
        // First check user config
        if let Some(mapping) = self.ports.iter().find(|p| p.name == name) {
            return Some(mapping.clone());
        }

        // Check if this default is ignored
        if self.ignored_defaults.contains(&name.to_string()) {
            return None;
        }

        // Then check defaults
        if let Ok(defaults) = DefaultsConfig::load() {
            if let Some(mapping) = defaults.ports.iter().find(|p| p.name == name) {
                return Some(mapping.clone());
            }
        }

        None
    }

    pub fn list_ports(&self) -> &[PortMapping] {
        &self.ports
    }

    pub fn get_used_ports(&self) -> Vec<u16> {
        let mut used_ports = Vec::new();

        // Get ports from user config
        for mapping in &self.ports {
            match &mapping.port {
                PortSpec::Single(port) => used_ports.push(*port),
                PortSpec::Range { start, end } => {
                    for port in *start..=*end {
                        used_ports.push(port);
                    }
                }
            }
        }

        // Get ports from defaults
        if let Ok(defaults) = DefaultsConfig::load() {
            for mapping in &defaults.ports {
                match &mapping.port {
                    PortSpec::Single(port) => used_ports.push(*port),
                    PortSpec::Range { start, end } => {
                        for port in *start..=*end {
                            used_ports.push(port);
                        }
                    }
                }
            }
        }

        used_ports
    }
}

const DEFAULTS_VERSION: u32 = 1;

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct DefaultsConfig {
    #[serde(default = "default_version")]
    pub version: u32,
    #[serde(default)]
    pub ports: Vec<PortMapping>,
}

fn default_version() -> u32 {
    DEFAULTS_VERSION
}

impl DefaultsConfig {
    pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
        let defaults_path = Self::defaults_path()?;

        if !defaults_path.exists() {
            // Create default defaults if they don't exist
            let default_config = Self::create_defaults();
            default_config.save()?;
            return Ok(default_config);
        }

        let content = fs::read_to_string(&defaults_path)?;
        let config: DefaultsConfig = toml::from_str(&content)?;
        Ok(config)
    }

    pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
        let defaults_path = Self::defaults_path()?;

        // Create config directory if it doesn't exist
        if let Some(parent) = defaults_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let content = toml::to_string_pretty(self)?;
        fs::write(&defaults_path, content)?;
        Ok(())
    }

    fn defaults_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
        Ok(Config::config_dir()?.join("defaults.toml"))
    }

    pub fn reset() -> Result<(), Box<dyn std::error::Error>> {
        let defaults = Self::create_defaults();
        defaults.save()
    }

    pub fn sync() -> Result<isize, Box<dyn std::error::Error>> {
        let defaults_path = Self::defaults_path()?;
        let built_in_defaults = Self::create_defaults();

        let old_count = if defaults_path.exists() {
            Self::load().map(|d| d.ports.len()).unwrap_or(0)
        } else {
            0
        };

        // Just replace with new defaults
        built_in_defaults.save()?;

        let new_count = built_in_defaults.ports.len();
        Ok(new_count as isize - old_count as isize)
    }

    fn create_defaults() -> Self {
        let mut config = DefaultsConfig::default();

        // Databases
        config.ports.push(PortMapping {
            name: "postgres".to_string(),
            port: PortSpec::Single(5432),
            description: Some("PostgreSQL database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "mysql".to_string(),
            port: PortSpec::Single(3306),
            description: Some("MySQL database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "mongodb".to_string(),
            port: PortSpec::Single(27017),
            description: Some("MongoDB database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "redis".to_string(),
            port: PortSpec::Single(6379),
            description: Some("Redis cache".to_string()),
        });
        config.ports.push(PortMapping {
            name: "elasticsearch".to_string(),
            port: PortSpec::Single(9200),
            description: Some("Elasticsearch search engine".to_string()),
        });
        config.ports.push(PortMapping {
            name: "cassandra".to_string(),
            port: PortSpec::Single(9042),
            description: Some("Apache Cassandra database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "couchdb".to_string(),
            port: PortSpec::Single(5984),
            description: Some("Apache CouchDB database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "influxdb".to_string(),
            port: PortSpec::Single(8086),
            description: Some("InfluxDB time-series database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "neo4j".to_string(),
            port: PortSpec::Single(7687),
            description: Some("Neo4j graph database".to_string()),
        });
        config.ports.push(PortMapping {
            name: "clickhouse".to_string(),
            port: PortSpec::Single(9000),
            description: Some("ClickHouse analytics database".to_string()),
        });

        // Message Brokers
        config.ports.push(PortMapping {
            name: "rabbitmq".to_string(),
            port: PortSpec::Single(5672),
            description: Some("RabbitMQ message broker".to_string()),
        });
        config.ports.push(PortMapping {
            name: "kafka".to_string(),
            port: PortSpec::Single(9092),
            description: Some("Apache Kafka message broker".to_string()),
        });
        config.ports.push(PortMapping {
            name: "zookeeper".to_string(),
            port: PortSpec::Single(2181),
            description: Some("Apache ZooKeeper coordination service".to_string()),
        });

        // Caching
        config.ports.push(PortMapping {
            name: "memcached".to_string(),
            port: PortSpec::Single(11211),
            description: Some("Memcached cache".to_string()),
        });

        // Monitoring
        config.ports.push(PortMapping {
            name: "prometheus".to_string(),
            port: PortSpec::Single(9090),
            description: Some("Prometheus monitoring".to_string()),
        });
        config.ports.push(PortMapping {
            name: "grafana".to_string(),
            port: PortSpec::Single(3000),
            description: Some("Grafana dashboard".to_string()),
        });

        // Infrastructure
        config.ports.push(PortMapping {
            name: "docker".to_string(),
            port: PortSpec::Single(2375),
            description: Some("Docker daemon".to_string()),
        });
        config.ports.push(PortMapping {
            name: "etcd".to_string(),
            port: PortSpec::Single(2379),
            description: Some("etcd distributed key-value store".to_string()),
        });
        config.ports.push(PortMapping {
            name: "consul".to_string(),
            port: PortSpec::Single(8500),
            description: Some("Consul service mesh".to_string()),
        });
        config.ports.push(PortMapping {
            name: "vault".to_string(),
            port: PortSpec::Single(8200),
            description: Some("HashiCorp Vault secrets management".to_string()),
        });

        // CI/CD
        config.ports.push(PortMapping {
            name: "jenkins".to_string(),
            port: PortSpec::Single(8080),
            description: Some("Jenkins CI/CD".to_string()),
        });
        config.ports.push(PortMapping {
            name: "sonarqube".to_string(),
            port: PortSpec::Single(9000),
            description: Some("SonarQube code quality".to_string()),
        });

        // Storage
        config.ports.push(PortMapping {
            name: "minio".to_string(),
            port: PortSpec::Single(9000),
            description: Some("MinIO object storage".to_string()),
        });

        // Network Services
        config.ports.push(PortMapping {
            name: "http".to_string(),
            port: PortSpec::Single(80),
            description: Some("HTTP web server".to_string()),
        });
        config.ports.push(PortMapping {
            name: "https".to_string(),
            port: PortSpec::Single(443),
            description: Some("HTTPS web server".to_string()),
        });
        config.ports.push(PortMapping {
            name: "ssh".to_string(),
            port: PortSpec::Single(22),
            description: Some("SSH server".to_string()),
        });
        config.ports.push(PortMapping {
            name: "ftp".to_string(),
            port: PortSpec::Single(21),
            description: Some("FTP server".to_string()),
        });
        config.ports.push(PortMapping {
            name: "smtp".to_string(),
            port: PortSpec::Single(25),
            description: Some("SMTP mail server".to_string()),
        });
        config.ports.push(PortMapping {
            name: "dns".to_string(),
            port: PortSpec::Single(53),
            description: Some("DNS server".to_string()),
        });

        config
    }
}

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

    #[test]
    fn test_portspec_parse_single_port() {
        let result = PortSpec::parse("8080");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Single(port) => assert_eq!(port, 8080),
            _ => panic!("Expected Single port"),
        }
    }

    #[test]
    fn test_portspec_parse_single_port_with_whitespace() {
        let result = PortSpec::parse("  8080  ");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Single(port) => assert_eq!(port, 8080),
            _ => panic!("Expected Single port"),
        }
    }

    #[test]
    fn test_portspec_parse_range() {
        let result = PortSpec::parse("8000-8010");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Range { start, end } => {
                assert_eq!(start, 8000);
                assert_eq!(end, 8010);
            }
            _ => panic!("Expected Range"),
        }
    }

    #[test]
    fn test_portspec_parse_range_with_whitespace() {
        let result = PortSpec::parse(" 8000 - 8010 ");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Range { start, end } => {
                assert_eq!(start, 8000);
                assert_eq!(end, 8010);
            }
            _ => panic!("Expected Range"),
        }
    }

    #[test]
    fn test_portspec_parse_invalid_port() {
        let result = PortSpec::parse("invalid");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid port"));
    }

    #[test]
    fn test_portspec_parse_invalid_range_start() {
        let result = PortSpec::parse("invalid-8010");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid start port"));
    }

    #[test]
    fn test_portspec_parse_invalid_range_end() {
        let result = PortSpec::parse("8000-invalid");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid end port"));
    }

    #[test]
    fn test_portspec_parse_range_start_equals_end() {
        let result = PortSpec::parse("8000-8000");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Start port must be less than end port"));
    }

    #[test]
    fn test_portspec_parse_range_start_greater_than_end() {
        let result = PortSpec::parse("8010-8000");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Start port must be less than end port"));
    }

    #[test]
    fn test_portspec_parse_out_of_range() {
        let result = PortSpec::parse("99999");
        assert!(result.is_err());
    }

    #[test]
    fn test_portspec_parse_negative() {
        let result = PortSpec::parse("-1");
        assert!(result.is_err());
    }

    #[test]
    fn test_portspec_parse_zero() {
        let result = PortSpec::parse("0");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Single(port) => assert_eq!(port, 0),
            _ => panic!("Expected Single port"),
        }
    }

    #[test]
    fn test_portspec_parse_max_port() {
        let result = PortSpec::parse("65535");
        assert!(result.is_ok());
        match result.unwrap() {
            PortSpec::Single(port) => assert_eq!(port, 65535),
            _ => panic!("Expected Single port"),
        }
    }

    #[test]
    fn test_portspec_display_single() {
        let spec = PortSpec::Single(8080);
        assert_eq!(spec.display(), "8080");
    }

    #[test]
    fn test_portspec_display_range() {
        let spec = PortSpec::Range {
            start: 8000,
            end: 8010,
        };
        assert_eq!(spec.display(), "8000-8010");
    }

    #[test]
    fn test_config_add_port() {
        let mut config = Config::default();
        config.add_port(
            "test".to_string(),
            PortSpec::Single(8080),
            Some("Test port".to_string()),
        );

        assert_eq!(config.ports.len(), 1);
        assert_eq!(config.ports[0].name, "test");
        assert_eq!(config.ports[0].description, Some("Test port".to_string()));
    }

    #[test]
    fn test_config_add_multiple_ports() {
        let mut config = Config::default();
        config.add_port("test1".to_string(), PortSpec::Single(8080), None);
        config.add_port("test2".to_string(), PortSpec::Single(8081), None);
        config.add_port(
            "test3".to_string(),
            PortSpec::Range {
                start: 8100,
                end: 8110,
            },
            None,
        );

        assert_eq!(config.ports.len(), 3);
    }

    #[test]
    fn test_config_remove_port_existing() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);

        let removed = config.remove_port("test");
        assert!(removed);
        assert_eq!(config.ports.len(), 0);
    }

    #[test]
    fn test_config_remove_port_non_existing() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);

        let removed = config.remove_port("nonexistent");
        assert!(!removed);
        assert_eq!(config.ports.len(), 1);
    }

    #[test]
    fn test_config_remove_port_from_empty() {
        let mut config = Config::default();
        let removed = config.remove_port("test");
        assert!(!removed);
    }

    #[test]
    fn test_config_find_port_existing() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);

        let found = config.find_port("test");
        assert!(found.is_some());
        assert_eq!(found.as_ref().unwrap().name, "test");
    }

    #[test]
    fn test_config_find_port_non_existing() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);

        // Use a very specific name that won't be in defaults
        let found = config.find_port("this-definitely-does-not-exist-xyz123");
        // This specific name should not exist in defaults or config
        assert!(found.is_none());
    }

    #[test]
    fn test_config_list_ports_empty() {
        let config = Config::default();
        assert_eq!(config.list_ports().len(), 0);
    }

    #[test]
    fn test_config_list_ports() {
        let mut config = Config::default();
        config.add_port("test1".to_string(), PortSpec::Single(8080), None);
        config.add_port("test2".to_string(), PortSpec::Single(8081), None);

        let ports = config.list_ports();
        assert_eq!(ports.len(), 2);
        assert_eq!(ports[0].name, "test1");
        assert_eq!(ports[1].name, "test2");
    }

    #[test]
    fn test_config_get_used_ports_empty() {
        let config = Config::default();
        let used = config.get_used_ports();
        // May include defaults if defaults.toml exists, or be empty if not
        // Just verify it doesn't crash and returns a valid Vec
        let _ = used;
    }

    #[test]
    fn test_config_get_used_ports_single() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);

        let used = config.get_used_ports();
        // Must include the port we added, may include defaults
        assert!(!used.is_empty());
        assert!(used.contains(&8080));
    }

    #[test]
    fn test_config_get_used_ports_multiple_single() {
        let mut config = Config::default();
        config.add_port("test1".to_string(), PortSpec::Single(8080), None);
        config.add_port("test2".to_string(), PortSpec::Single(8081), None);
        config.add_port("test3".to_string(), PortSpec::Single(8082), None);

        let used = config.get_used_ports();
        // Must include the 3 ports we added, may include defaults
        assert!(used.len() >= 3);
        assert!(used.contains(&8080));
        assert!(used.contains(&8081));
        assert!(used.contains(&8082));
    }

    #[test]
    fn test_config_get_used_ports_range() {
        let mut config = Config::default();
        config.add_port(
            "test".to_string(),
            PortSpec::Range {
                start: 8000,
                end: 8010,
            },
            None,
        );

        let used = config.get_used_ports();
        // Must include all 11 ports from the range, may include defaults
        assert!(used.len() >= 11);
        for port in 8000..=8010 {
            assert!(used.contains(&port));
        }
    }

    #[test]
    fn test_config_get_used_ports_mixed() {
        let mut config = Config::default();
        config.add_port("single1".to_string(), PortSpec::Single(8080), None);
        config.add_port(
            "range1".to_string(),
            PortSpec::Range {
                start: 8000,
                end: 8002,
            },
            None,
        );
        config.add_port("single2".to_string(), PortSpec::Single(8090), None);

        let used = config.get_used_ports();
        // Must include 5 ports (2 singles + 3 from range), may include defaults
        assert!(used.len() >= 5);
        assert!(used.contains(&8080));
        assert!(used.contains(&8000));
        assert!(used.contains(&8001));
        assert!(used.contains(&8002));
        assert!(used.contains(&8090));
    }

    #[test]
    fn test_config_duplicate_names() {
        let mut config = Config::default();
        config.add_port("test".to_string(), PortSpec::Single(8080), None);
        config.add_port("test".to_string(), PortSpec::Single(8081), None);

        // Should allow duplicates (no uniqueness constraint)
        assert_eq!(config.ports.len(), 2);

        // Remove should remove all occurrences with matching name
        config.remove_port("test");
        assert_eq!(config.ports.len(), 0);
    }

    #[test]
    fn test_portspec_serialization_single() {
        let spec = PortSpec::Single(8080);
        let serialized = serde_json::to_string(&spec).unwrap();
        assert_eq!(serialized, "8080");
    }

    #[test]
    fn test_portspec_serialization_range() {
        let spec = PortSpec::Range {
            start: 8000,
            end: 8010,
        };
        let serialized = serde_json::to_string(&spec).unwrap();
        assert!(serialized.contains("\"start\":8000"));
        assert!(serialized.contains("\"end\":8010"));
    }

    #[test]
    fn test_portspec_deserialization_single() {
        let json = "8080";
        let spec: PortSpec = serde_json::from_str(json).unwrap();
        match spec {
            PortSpec::Single(port) => assert_eq!(port, 8080),
            _ => panic!("Expected Single port"),
        }
    }

    #[test]
    fn test_portspec_deserialization_range() {
        let json = r#"{"start":8000,"end":8010}"#;
        let spec: PortSpec = serde_json::from_str(json).unwrap();
        match spec {
            PortSpec::Range { start, end } => {
                assert_eq!(start, 8000);
                assert_eq!(end, 8010);
            }
            _ => panic!("Expected Range"),
        }
    }
}