netconv-core 0.1.0

Vendor-agnostic IR (intermediate representation) types and traits for network device configuration conversion. Used by ios-config and the netconv renderers.
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
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
787
788
789
790
791
792
793
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;

// ---------------------------------------------------------------------------
// Confidence — каждый элемент IR знает насколько точно он был распознан
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Confidence {
    /// Точное соответствие — гарантировано корректная конвертация
    Exact,
    /// Есть аналог, но с нюансами — объясняем в репорте
    Approximate { note: String },
    /// Нет аналога на целевой платформе — требует ручного решения
    Manual { reason: String },
    /// Парсер не распознал команду — сохраняем raw текст
    Unknown { raw: String },
}

// ---------------------------------------------------------------------------
// NetworkConfig — корневой объект IR
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NetworkConfig {
    pub hostname: Option<String>,
    pub domain_name: Option<String>,
    pub interfaces: Vec<Interface>,
    pub vlans: Vec<Vlan>,
    pub routing: RoutingConfig,
    pub acls: Vec<Acl>,
    pub nat: Vec<NatRule>,
    pub ntp: Vec<NtpServer>,
    pub dns: Vec<IpAddr>,
    pub snmp: Option<SnmpConfig>,
    pub aaa: Option<AaaConfig>,
    pub stp: Option<GlobalStp>,
    pub logging: Option<LoggingConfig>,
    pub users: Vec<LocalUser>,
    pub line_vty: Option<LineVty>,
    pub ssh: Option<SshConfig>,
    pub banner: Option<String>,
    /// Всё что парсер не распознал — не выбрасываем, храним
    pub unknown_blocks: Vec<UnknownBlock>,
    /// Платформо-специфичные команды без аналога
    pub platform_specific: Vec<UnknownBlock>,
}

// ---------------------------------------------------------------------------
// Global STP
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalStp {
    pub mode: StpMode,
    pub loopguard: bool,
    pub portfast_default: bool,
    pub bpduguard_default: bool,
    pub vlan_priorities: Vec<StpVlanPriority>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StpMode {
    RapidPvst,
    Pvst,
    Mst,
    Rstp,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StpVlanPriority {
    pub vlans: Vec<u16>,
    pub priority: u32,
}

// ---------------------------------------------------------------------------
// Logging
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    pub buffered_size: Option<u32>,
    pub console_level: Option<String>,
    pub hosts: Vec<std::net::IpAddr>,
}

// ---------------------------------------------------------------------------
// Local users
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocalUser {
    pub name: String,
    pub privilege: u8,
    pub password_type: PasswordType,
    pub password_hash: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PasswordType {
    Plaintext,
    Type7,  // Cisco Type 7 (reversible)
    Md5,    // enable secret 5
    Scrypt, // enable secret 9
}

// ---------------------------------------------------------------------------
// Line VTY
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LineVty {
    pub exec_timeout_min: u32,
    pub exec_timeout_sec: u32,
    pub transport_input: Vec<String>,
    pub logging_synchronous: bool,
}

// ---------------------------------------------------------------------------
// SSH
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SshConfig {
    pub version: u8,
    pub timeout: Option<u32>,
    pub retries: Option<u8>,
}

// ---------------------------------------------------------------------------
// Interface
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Interface {
    /// Нормализованное имя: "GigabitEthernet0/0" → "GigabitEthernet0/0"
    /// Рендерер сам адаптирует под целевой вендор
    pub name: InterfaceName,
    pub description: Option<String>,
    pub addresses: Vec<IpAddress>,
    pub shutdown: bool,
    pub mtu: Option<u32>,
    pub speed: Option<InterfaceSpeed>,
    pub duplex: Option<Duplex>,
    pub l2: Option<L2Config>,
    pub helper_addresses: Vec<IpAddr>,
    pub acl_in: Option<String>,
    pub acl_out: Option<String>,
    pub nat_direction: Option<NatDirection>,
    pub hsrp: Vec<HsrpGroup>,
    pub ospf: Option<InterfaceOspf>,
    pub voice_vlan: Option<u16>,
    pub storm_control: Option<StormControl>,
    pub stp: InterfaceStp,
    pub confidence: Confidence,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StormControl {
    pub broadcast_level: Option<f32>,
    pub multicast_level: Option<f32>,
    pub unicast_level: Option<f32>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InterfaceStp {
    pub portfast: bool,
    pub bpduguard: bool,
    pub bpdufilter: bool,
    pub guard_root: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterfaceName {
    pub kind: InterfaceKind,
    /// Слот/порт как строка — "0/0", "0/0/0", "1" и т.д.
    pub id: String,
    /// Оригинальное имя из конфига — для репорта
    pub original: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum InterfaceKind {
    GigabitEthernet,
    FastEthernet,
    TenGigabitEthernet,
    Loopback,
    Vlan,
    Tunnel,
    Serial,
    BundleEther,
    Management,
    Unknown(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IpAddress {
    pub prefix: IpNet,
    pub secondary: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InterfaceSpeed {
    Mbps(u32),
    Auto,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Duplex {
    Full,
    Half,
    Auto,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct L2Config {
    pub mode: L2Mode,
    pub access_vlan: Option<u16>,
    pub trunk_allowed: Option<Vec<u16>>,
    pub trunk_native: Option<u16>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum L2Mode {
    Access,
    Trunk,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NatDirection {
    Inside,
    Outside,
}

// ---------------------------------------------------------------------------
// HSRP → будет рендериться как VRRP на Huawei (Approximate)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsrpGroup {
    pub group_id: u16,
    pub virtual_ip: IpAddr,
    pub priority: Option<u16>,
    pub preempt: bool,
    pub preempt_delay: Option<u32>,
    pub timers: Option<HsrpTimers>,
    pub track: Vec<HsrpTrack>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsrpTimers {
    pub hello_ms: u32,
    pub hold_ms: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsrpTrack {
    pub object: u32,
    pub decrement: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterfaceOspf {
    pub process_id: u32,
    pub area: OspfArea,
    pub cost: Option<u32>,
    pub priority: Option<u8>,
    pub timers: Option<OspfIfTimers>,
    pub auth: Option<OspfAuth>,
    pub passive: bool,
    pub network_type: Option<OspfNetworkType>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfIfTimers {
    pub hello_interval: u32,
    pub dead_interval: u32,
}

// ---------------------------------------------------------------------------
// VLAN
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vlan {
    pub id: u16,
    pub name: Option<String>,
    pub active: bool,
}

// ---------------------------------------------------------------------------
// Routing
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RoutingConfig {
    pub static_routes: Vec<StaticRoute>,
    pub ospf: Vec<OspfProcess>,
    pub bgp: Option<BgpConfig>,
    pub eigrp: Vec<EigrpProcess>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StaticRoute {
    pub prefix: IpNet,
    pub next_hop: NextHop,
    pub distance: Option<u8>,
    pub tag: Option<u32>,
    pub name: Option<String>,
    pub permanent: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NextHop {
    Ip(IpAddr),
    Interface(String),
    IpAndInterface(IpAddr, String),
    Null0,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfProcess {
    pub process_id: u32,
    pub router_id: Option<IpAddr>,
    pub areas: Vec<OspfAreaConfig>,
    pub passive_interfaces: Vec<String>,
    pub default_originate: Option<OspfDefaultOriginate>,
    pub redistribute: Vec<OspfRedistribute>,
    pub max_metric: bool,
    pub auth: Option<OspfAuth>,
    pub log_adjacency: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfAreaConfig {
    pub area: OspfArea,
    pub networks: Vec<OspfNetwork>,
    pub area_type: OspfAreaType,
    pub auth: Option<OspfAuth>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OspfArea {
    Backbone, // area 0
    Normal(u32),
    IpFormat(IpAddr), // area 0.0.0.1 формат
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfNetwork {
    pub prefix: IpNet,
    pub wildcard: bool, // если true — это wildcard маска, не prefix length
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OspfAreaType {
    Normal,
    Stub,
    StubNoSummary,
    Nssa,
    NssaNoSummary,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OspfAuth {
    Simple(String),
    Md5 { key_id: u8, key: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OspfNetworkType {
    Broadcast,
    PointToPoint,
    PointToMultipoint,
    NonBroadcast,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfDefaultOriginate {
    pub always: bool,
    pub metric: Option<u32>,
    pub metric_type: Option<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OspfRedistribute {
    pub source: RedistributeSource,
    pub metric: Option<u32>,
    pub metric_type: Option<u8>,
    pub subnets: bool,
    pub tag: Option<u32>,
    pub route_map: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RedistributeSource {
    Connected,
    Static,
    Bgp(u32),
    Eigrp(u32),
    Rip,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgpConfig {
    pub asn: u32,
    pub router_id: Option<IpAddr>,
    pub neighbors: Vec<BgpNeighbor>,
    pub peer_groups: Vec<BgpPeerGroup>,
    /// Сети из глобального контекста (без address-family)
    pub networks: Vec<IpNet>,
    /// address-family блоки
    pub address_families: Vec<BgpAddressFamily>,
    pub redistribute: Vec<OspfRedistribute>,
    pub log_neighbor_changes: bool,
    pub bestpath: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgpNeighbor {
    pub address: BgpNeighborAddr,
    pub remote_as: u32,
    pub description: Option<String>,
    pub update_source: Option<String>,
    pub next_hop_self: bool,
    pub password: Option<String>,
    pub shutdown: bool,
    pub peer_group: Option<String>,
    pub route_map_in: Option<String>,
    pub route_map_out: Option<String>,
    pub prefix_list_in: Option<String>,
    pub prefix_list_out: Option<String>,
    pub soft_reconfiguration: bool,
    pub send_community: bool,
    pub remove_private_as: bool,
    pub default_originate: bool,
    pub activate: bool,
}

/// Адрес соседа — IP или имя peer-group
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BgpNeighborAddr {
    Ip(IpAddr),
    PeerGroup(String),
}

impl std::fmt::Display for BgpNeighborAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BgpNeighborAddr::Ip(ip) => write!(f, "{}", ip),
            BgpNeighborAddr::PeerGroup(name) => write!(f, "{}", name),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgpPeerGroup {
    pub name: String,
    pub remote_as: Option<u32>,
    pub update_source: Option<String>,
    pub next_hop_self: bool,
    pub route_map_in: Option<String>,
    pub route_map_out: Option<String>,
    pub send_community: bool,
}

/// address-family ipv4/ipv6/vpnv4 unicast/multicast
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgpAddressFamily {
    pub afi: BgpAfi,
    pub safi: BgpSafi,
    pub networks: Vec<IpNet>,
    pub redistribute: Vec<OspfRedistribute>,
    /// neighbor activate / no neighbor activate
    pub activated_neighbors: Vec<BgpNeighborAddr>,
    pub deactivated_neighbors: Vec<BgpNeighborAddr>,
    pub neighbor_settings: Vec<BgpNeighbor>,
    pub default_information: bool,
    pub aggregate_addresses: Vec<BgpAggregate>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BgpAfi {
    Ipv4,
    Ipv6,
    Vpnv4,
    L2vpn,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BgpSafi {
    Unicast,
    Multicast,
    Labeled,
    Evpn,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgpAggregate {
    pub prefix: IpNet,
    pub summary_only: bool,
    pub as_set: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EigrpProcess {
    pub asn: u32,
    pub networks: Vec<OspfNetwork>,
    pub passive_interfaces: Vec<String>,
    pub redistribute: Vec<OspfRedistribute>,
}

// ---------------------------------------------------------------------------
// ACL
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Acl {
    pub name: AclName,
    pub acl_type: AclType,
    pub entries: Vec<AclEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AclName {
    Named(String),
    Numbered(u32),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AclType {
    Standard, // только src IP
    Extended, // src+dst+proto+port
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AclEntry {
    pub sequence: Option<u32>,
    pub action: AclAction,
    pub protocol: Option<AclProtocol>,
    pub src: AclMatch,
    pub dst: Option<AclMatch>,
    pub src_port: Option<AclPort>,
    pub dst_port: Option<AclPort>,
    pub established: bool,
    pub log: bool,
    pub remark: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AclAction {
    Permit,
    Deny,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AclProtocol {
    Ip,
    Tcp,
    Udp,
    Icmp,
    Esp,
    Ahp,
    Number(u8),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AclMatch {
    Any,
    Host(IpAddr),
    Network { addr: IpAddr, wildcard: IpAddr },
    Prefix(IpNet),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AclPort {
    Eq(u16),
    Ne(u16),
    Lt(u16),
    Gt(u16),
    Range(u16, u16),
}

// ---------------------------------------------------------------------------
// NAT
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatRule {
    pub rule_type: NatType,
    pub acl: Option<String>,
    pub pool: Option<NatPool>,
    pub interface_overload: bool,
    pub static_entry: Option<NatStaticEntry>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NatType {
    Dynamic,  // source list ACL pool
    Overload, // PAT
    Static,   // фиксированный маппинг
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatPool {
    pub name: String,
    pub start: IpAddr,
    pub end: IpAddr,
    pub prefix: Option<IpNet>,
    pub overload: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatStaticEntry {
    pub local: IpAddr,
    pub global: IpAddr,
    pub local_port: Option<u16>,
    pub global_port: Option<u16>,
    pub protocol: Option<AclProtocol>,
}

// ---------------------------------------------------------------------------
// NTP / DNS / SNMP / AAA
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NtpServer {
    pub address: IpAddr,
    pub prefer: bool,
    pub key: Option<u32>,
    pub source_interface: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnmpConfig {
    pub communities: Vec<SnmpCommunity>,
    pub location: Option<String>,
    pub contact: Option<String>,
    pub traps: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnmpCommunity {
    pub name: String,
    pub access: SnmpAccess,
    pub acl: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SnmpAccess {
    Ro,
    Rw,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AaaConfig {
    pub new_model: bool,
    pub authentication: Vec<AaaMethod>,
    pub authorization: Vec<AaaMethod>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AaaMethod {
    pub list_name: String,
    pub methods: Vec<String>,
}

// ---------------------------------------------------------------------------
// UnknownBlock — всё нераспознанное, с контекстом
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnknownBlock {
    /// Строка в исходном конфиге
    pub line: usize,
    /// Контекст: в каком блоке находилась команда
    pub context: String,
    /// Оригинальный текст
    pub raw: String,
}

// ---------------------------------------------------------------------------
// Вспомогательные типы
// ---------------------------------------------------------------------------

pub type ProcessId = u32;

impl Default for Interface {
    fn default() -> Self {
        Interface {
            name: InterfaceName {
                kind: InterfaceKind::Unknown(String::new()),
                id: String::new(),
                original: String::new(),
            },
            description: None,
            addresses: vec![],
            shutdown: false,
            mtu: None,
            speed: None,
            duplex: None,
            l2: None,
            helper_addresses: vec![],
            acl_in: None,
            acl_out: None,
            nat_direction: None,
            hsrp: vec![],
            ospf: None,
            voice_vlan: None,
            storm_control: None,
            stp: InterfaceStp::default(),
            confidence: Confidence::Exact,
        }
    }
}

impl InterfaceName {
    pub fn parse(raw: &str) -> Self {
        let original = raw.to_string();

        // Нормализуем сокращения Cisco
        let expanded = expand_interface_name(raw);

        // Разбиваем на kind + id
        let (kind_str, id) = split_interface_name(&expanded);

        let kind = match kind_str.to_lowercase().as_str() {
            "gigabitethernet" => InterfaceKind::GigabitEthernet,
            "fastethernet" => InterfaceKind::FastEthernet,
            "tengigabitethernet" | "tengige" => InterfaceKind::TenGigabitEthernet,
            "loopback" => InterfaceKind::Loopback,
            "vlan" => InterfaceKind::Vlan,
            "tunnel" => InterfaceKind::Tunnel,
            "serial" => InterfaceKind::Serial,
            "bundle-ether" | "bundleether" => InterfaceKind::BundleEther,
            "management" => InterfaceKind::Management,
            other => InterfaceKind::Unknown(other.to_string()),
        };

        InterfaceName { kind, id, original }
    }
}

fn expand_interface_name(raw: &str) -> String {
    // Cisco сокращения → полные имена
    let prefixes = [
        ("Gi", "GigabitEthernet"),
        ("Fa", "FastEthernet"),
        ("Te", "TenGigabitEthernet"),
        ("Lo", "Loopback"),
        ("Tu", "Tunnel"),
        ("Se", "Serial"),
        ("Vl", "Vlan"),
        ("Mg", "Management"),
    ];

    for (short, full) in &prefixes {
        if raw.starts_with(short) && !raw.to_lowercase().starts_with(&full.to_lowercase()) {
            return format!("{}{}", full, &raw[short.len()..]);
        }
    }
    raw.to_string()
}

fn split_interface_name(name: &str) -> (&str, String) {
    // Ищем первую цифру — всё до неё это тип, после — id
    let pos = name.find(|c: char| c.is_ascii_digit());
    match pos {
        Some(i) => (&name[..i], name[i..].to_string()),
        None => (name, String::new()),
    }
}

impl OspfArea {
    pub fn parse(s: &str) -> Self {
        if let Ok(n) = s.parse::<u32>() {
            if n == 0 {
                OspfArea::Backbone
            } else {
                OspfArea::Normal(n)
            }
        } else if let Ok(ip) = s.parse::<IpAddr>() {
            if ip == "0.0.0.0".parse::<IpAddr>().unwrap() {
                OspfArea::Backbone
            } else {
                OspfArea::IpFormat(ip)
            }
        } else {
            OspfArea::Normal(0) // fallback
        }
    }
}