Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ipv4Block and Ipv6Block. #298

Merged
merged 11 commits into from
Jul 31, 2024
136 changes: 132 additions & 4 deletions src/repository/resources/ipres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,12 +1709,128 @@ impl AddressFamily {
}


//------------ Ipv4Block -----------------------------------------------------

/// Contains an IPv4 address block.
partim marked this conversation as resolved.
Show resolved Hide resolved
///
/// This type is a thin wrapper around [`IpBlock`] ensuring that this is an
partim marked this conversation as resolved.
Show resolved Hide resolved
/// IPv4 address block.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv4Block(IpBlock);

impl Ipv4Block {
/// Creates a new block covering all addresses.
partim marked this conversation as resolved.
Show resolved Hide resolved
pub fn all() -> Self {
Self(IpBlock::all())
}

/// Returns whether the block is prefix with address length zero.
partim marked this conversation as resolved.
Show resolved Hide resolved
pub fn is_slash_zero(&self) -> bool {
self.0.is_slash_zero()
}

/// The smallest address of the block.
pub fn min(&self) -> Ipv4Addr {
self.0.min().into()
}

/// The largest address of the block.
pub fn max(&self) -> Ipv4Addr {
self.0.max().into()
}
}


//--- From and FromStr

impl From<Ipv4Block> for IpBlock {
fn from(src: Ipv4Block) -> IpBlock {
src.0
}
}

impl str::FromStr for Ipv4Block {
type Err = FromStrError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(IpBlock::from_v4_str(s)?))
}
}

partim marked this conversation as resolved.
Show resolved Hide resolved

partim marked this conversation as resolved.
Show resolved Hide resolved
//--- Display
partim marked this conversation as resolved.
Show resolved Hide resolved

impl fmt::Display for Ipv4Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_v4(f)
}
}


//------------ Ipv6Block -----------------------------------------------------
partim marked this conversation as resolved.
Show resolved Hide resolved

/// Contains an IPv6 address block.
///
/// This type is a thin wrapper around [`IpBlock`] ensuring that this is an
/// IPv6 address block.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6Block(IpBlock);

impl Ipv6Block {
/// Creates a new block covering all addresses.
pub fn all() -> Self {
Self(IpBlock::all())
}

/// Returns whether the block is prefix with address length zero.
pub fn is_slash_zero(&self) -> bool {
self.0.is_slash_zero()
}

/// The smallest address of the block.
pub fn min(&self) -> Ipv6Addr {
self.0.min().into()
}

/// The largest address of the block.
pub fn max(&self) -> Ipv6Addr {
self.0.max().into()
}
}


//--- From and FromStr

impl From<Ipv6Block> for IpBlock {
fn from(src: Ipv6Block) -> IpBlock {
src.0
}
}

impl str::FromStr for Ipv6Block {
type Err = FromStrError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(IpBlock::from_v6_str(s)?))
}
}


//--- Display

impl fmt::Display for Ipv6Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_v6(f)
}
}


//------------ Ipv4Blocks ----------------------------------------------------

/// Contains IPv4 resources. This type is a thin wrapper around the underlying
/// [`IpBlocks`] type intended to help with serializing/deserializing and
/// formatting using IPv4 syntax.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Ipv4Blocks(IpBlocks);

impl Ipv4Blocks {
Expand All @@ -1740,7 +1856,7 @@ impl fmt::Display for Ipv4Blocks {
}
}

//--- FromStr
//--- FromStr and FromIterator

impl FromStr for Ipv4Blocks {
type Err = FromStrError;
Expand All @@ -1764,6 +1880,12 @@ impl FromStr for Ipv4Blocks {
}
}

impl FromIterator<Ipv4Block> for Ipv4Blocks {
fn from_iter<I: IntoIterator<Item = Ipv4Block>>(iter: I) -> Self {
Self(IpBlocks::from_iter(iter.into_iter().map(Into::into)))
}
}

//--- Serialize and Deserialize

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -1805,7 +1927,7 @@ impl std::ops::Deref for Ipv4Blocks {
/// Contains IPv6 resources. This type is a thin wrapper around the underlying
/// [`IpBlocks`] type intended to help with serializing/deserializing and
/// formatting using IPv6 syntax.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Ipv6Blocks(IpBlocks);

impl Ipv6Blocks {
Expand All @@ -1831,7 +1953,7 @@ impl fmt::Display for Ipv6Blocks {
}
}

//--- FromStr
//--- FromStr and FromIterator

impl FromStr for Ipv6Blocks {
type Err = FromStrError;
Expand All @@ -1855,6 +1977,12 @@ impl FromStr for Ipv6Blocks {
}
}

impl FromIterator<Ipv6Block> for Ipv6Blocks {
fn from_iter<I: IntoIterator<Item = Ipv6Block>>(iter: I) -> Self {
Self(IpBlocks::from_iter(iter.into_iter().map(Into::into)))
}
}

//--- Serialize and Deserialize

#[cfg(feature = "serde")]
Expand Down
8 changes: 4 additions & 4 deletions src/repository/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub use self::asres::{
};
pub use self::choice::ResourcesChoice;
pub use self::ipres::{
Addr, AddressFamily, InheritedIpResources, IpBlock, IpBlocks, Ipv4Blocks,
Ipv6Blocks, IpBlocksBuilder, IpBlocksForFamily, IpResources,
IpResourcesBuilder, OverclaimedIpResources, OverclaimedIpv4Resources,
OverclaimedIpv6Resources, Prefix
Addr, AddressFamily, InheritedIpResources, IpBlock, IpBlocks, Ipv4Block,
Ipv4Blocks, Ipv6Block, Ipv6Blocks, IpBlocksBuilder, IpBlocksForFamily,
IpResources, IpResourcesBuilder, OverclaimedIpResources,
OverclaimedIpv4Resources, OverclaimedIpv6Resources, Prefix
};
pub use self::set::{
ResourceDiff, ResourceSet
Expand Down
Loading