Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
add TryFrom<IpNet> impls
Browse files Browse the repository at this point in the history
  • Loading branch information
benmaddison committed Aug 21, 2021
1 parent 0fc209f commit ac59f7f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prefixset"
version = "0.1.0-rc.1"
version = "0.1.0-rc.2"
authors = ["Ben Maddison <benm@workonline.africa>"]
description = "An efficient set container for IP address prefixes"
edition = "2018"
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Error {
/// The error returned during parsing, if any.
source: Option<ParseIntError>,
},
/// Incompatible address families.
AddressFamiltMismatch,
}

impl std::error::Error for Error {
Expand All @@ -34,6 +36,7 @@ impl fmt::Display for Error {
Self::AddrParse(ref err) => err.fmt(f),
Self::PrefixLen(ref err) => err.fmt(f),
Self::RangeParse { .. } => f.write_str("invalid IP prefix range"),
Self::AddressFamiltMismatch => f.write_str("mismatched address families"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//!
//! [`bgpq3`]: https://github.com/snar/bgpq3
//!
#![doc(html_root_url = "https://docs.rs/prefixset/0.1.0-rc.1")]
#![doc(html_root_url = "https://docs.rs/prefixset/0.1.0-rc.2")]
#![warn(missing_docs)]

extern crate ipnet;
Expand Down
15 changes: 14 additions & 1 deletion src/prefix/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::convert::TryFrom;
use std::fmt;
use std::str::FromStr;

use ipnet::{Ipv4Net, PrefixLenError};
use ipnet::{IpNet, Ipv4Net, PrefixLenError};

use crate::error::{Error, Result};

Expand Down Expand Up @@ -45,6 +46,18 @@ impl FromStr for Ipv4Prefix {
}
}

impl TryFrom<IpNet> for Ipv4Prefix {
type Error = Error;

fn try_from(n: IpNet) -> Result<Self> {
if let IpNet::V4(ipnet) = n {
Ok(ipnet.into())
} else {
Err(Error::AddressFamiltMismatch)
}
}
}

impl fmt::Display for Ipv4Prefix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_ipnet().fmt(f)
Expand Down
15 changes: 14 additions & 1 deletion src/prefix/ipv6.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::convert::TryFrom;
use std::fmt;
use std::str::FromStr;

use ipnet::{Ipv6Net, PrefixLenError};
use ipnet::{IpNet, Ipv6Net, PrefixLenError};

use crate::error::{Error, Result};

Expand Down Expand Up @@ -45,6 +46,18 @@ impl FromStr for Ipv6Prefix {
}
}

impl TryFrom<IpNet> for Ipv6Prefix {
type Error = Error;

fn try_from(n: IpNet) -> Result<Self> {
if let IpNet::V6(ipnet) = n {
Ok(ipnet.into())
} else {
Err(Error::AddressFamiltMismatch)
}
}
}

impl fmt::Display for Ipv6Prefix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_ipnet().fmt(f)
Expand Down

0 comments on commit ac59f7f

Please sign in to comment.