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
use core::fmt;
use core::str::FromStr;

#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
    arbitrary::{any, Arbitrary},
    prop_oneof,
    strategy::{BoxedStrategy, Strategy},
};

use super::{delegate, Address, Prefix, PrefixLength};
use crate::{
    concrete::{self, Ipv4, Ipv6},
    error::Error,
    traits,
};

/// Either an IPv4 or IPv6 interface.
///
/// # Memory Use
///
/// Rust enums are sized to accomodate their largest variant, with smaller
/// variants being padded to fill up any unused space.
///
/// As a result, users should avoid using this type in a context where only
/// [`Interface::Ipv4`] variants are expected.
///
/// # Examples
///
/// ``` rust
/// use ip::{
///     traits::{Address as _, Interface as _},
///     Any, Interface,
/// };
///
/// let interface = "192.0.2.0/24".parse::<Interface<Any>>()?;
///
/// assert!(interface.network().is_documentation());
/// # Ok::<(), ip::Error>(())
/// ```
#[allow(variant_size_differences)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Interface {
    /// IPv4 interface variant.
    Ipv4(concrete::Interface<Ipv4>),
    /// IPv6 interface variant.
    Ipv6(concrete::Interface<Ipv6>),
}

impl traits::Interface for Interface {
    type Address = Address;
    type Prefix = Prefix;
    type PrefixLength = PrefixLength;

    delegate! {
        fn network(&self) -> Self::Address;
        fn addr(&self) -> Self::Address;
        fn trunc(&self) -> Self::Prefix;
        fn prefix_len(&self) -> Self::PrefixLength;
        fn broadcast(&self) -> Self::Address;
    }
}

impl From<concrete::Interface<Ipv4>> for Interface {
    fn from(interface: concrete::Interface<Ipv4>) -> Self {
        Self::Ipv4(interface)
    }
}

impl From<concrete::Interface<Ipv6>> for Interface {
    fn from(interface: concrete::Interface<Ipv6>) -> Self {
        Self::Ipv6(interface)
    }
}

impl FromStr for Interface {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        concrete::Interface::<Ipv4>::from_str(s)
            .map(Self::from)
            .or_else(|_| concrete::Interface::<Ipv6>::from_str(s).map(Self::from))
    }
}

impl fmt::Display for Interface {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ipv4(interface) => interface.fmt(f),
            Self::Ipv6(interface) => interface.fmt(f),
        }
    }
}

#[cfg(any(test, feature = "arbitrary"))]
impl Arbitrary for Interface {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        prop_oneof![
            any::<concrete::Interface<Ipv4>>().prop_map(Self::Ipv4),
            any::<concrete::Interface<Ipv6>>().prop_map(Self::Ipv6),
        ]
        .boxed()
    }
}