mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-17 01:08:53 +00:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
1b6904bfc7 | |||
ab6f1012b3 | |||
5146125c35 | |||
86dbbd997a | |||
372f9bdbb7 | |||
c0e04d534a | |||
7c9ff1bc30 | |||
6d4e4ba47a | |||
e842e0edc0 | |||
61550eda45 | |||
3503e4c094 | |||
91528476a9 | |||
501e5533d7 | |||
7120b93837 | |||
a57afd9e1a | |||
8ad43541cd | |||
9651b3b721 | |||
5764406000 | |||
4cbd1f2424 | |||
772db6c569 |
7
.github/dependabot.yml
vendored
Normal file
7
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: cargo
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: daily
|
||||
open-pull-requests-limit: 10
|
19
.github/workflows/publish.yml
vendored
Normal file
19
.github/workflows/publish.yml
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
name: Publish to crates.io
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
tags:
|
||||
- 'v.*.*'
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
override: true
|
||||
- uses: katyo/publish-crates@v1
|
||||
with:
|
||||
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
2
.github/workflows/rust.yml
vendored
2
.github/workflows/rust.yml
vendored
@ -23,6 +23,6 @@ jobs:
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
run: cargo test --verbose --all-features
|
||||
- name: Build docs
|
||||
run: cargo doc --verbose
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ipnetwork"
|
||||
version = "0.18.0" # When updating version, also modify html_root_url in the lib.rs
|
||||
version = "0.20.0"
|
||||
authors = ["Abhishek Chanda <abhishek.becs@gmail.com>", "Linus Färnstrand <faern@faern.net>"]
|
||||
description = "A library to work with IP CIDRs in Rust"
|
||||
license = "MIT OR Apache-2.0"
|
||||
@ -8,15 +8,16 @@ repository = "https://github.com/achanda/ipnetwork"
|
||||
keywords = ["network", "ip", "address", "cidr"]
|
||||
readme = "README.md"
|
||||
categories = ["network-programming", "parser-implementations"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", optional = true }
|
||||
schemars = { version = "0.8.10", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0"
|
||||
serde_derive = "1"
|
||||
criterion = {version = "0.3.4", features= ["html_reports"]}
|
||||
does-it-json = "0.0.3"
|
||||
|
||||
[badges]
|
||||
travis-ci = { repository = "achanda/ipnetwork" }
|
||||
|
34
src/ipv4.rs
34
src/ipv4.rs
@ -1,5 +1,5 @@
|
||||
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
||||
use std::{fmt, net::Ipv4Addr, str::FromStr, convert::TryFrom};
|
||||
use std::{convert::TryFrom, fmt, net::Ipv4Addr, str::FromStr};
|
||||
|
||||
const IPV4_BITS: u8 = 32;
|
||||
|
||||
@ -27,7 +27,37 @@ impl serde::Serialize for Ipv4Network {
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
serializer.collect_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl schemars::JsonSchema for Ipv4Network {
|
||||
fn schema_name() -> String {
|
||||
"Ipv4Network".to_string()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||
schemars::schema::SchemaObject {
|
||||
instance_type: Some(schemars::schema::InstanceType::String.into()),
|
||||
string: Some(Box::new(schemars::schema::StringValidation {
|
||||
pattern: Some(
|
||||
concat!(
|
||||
r#"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"#,
|
||||
r#"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"#,
|
||||
r#"\/(3[0-2]|[0-2]?[0-9])$"#,
|
||||
)
|
||||
.to_string(),
|
||||
),
|
||||
..Default::default()
|
||||
})),
|
||||
extensions: [("x-rust-type".to_string(), "ipnetwork::Ipv4Network".into())]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
44
src/ipv6.rs
44
src/ipv6.rs
@ -1,5 +1,5 @@
|
||||
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
||||
use std::{cmp, fmt, net::Ipv6Addr, str::FromStr, convert::TryFrom};
|
||||
use std::{cmp, convert::TryFrom, fmt, net::Ipv6Addr, str::FromStr};
|
||||
|
||||
const IPV6_BITS: u8 = 128;
|
||||
const IPV6_SEGMENT_BITS: u8 = 16;
|
||||
@ -28,7 +28,47 @@ impl serde::Serialize for Ipv6Network {
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
serializer.collect_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl schemars::JsonSchema for Ipv6Network {
|
||||
fn schema_name() -> String {
|
||||
"Ipv6Network".to_string()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||
schemars::schema::SchemaObject {
|
||||
instance_type: Some(schemars::schema::InstanceType::String.into()),
|
||||
string: Some(Box::new(schemars::schema::StringValidation {
|
||||
pattern: Some(
|
||||
concat!(
|
||||
r#"^("#,
|
||||
r#"([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,7}:"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}"#,
|
||||
r#"|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})"#,
|
||||
r#"|:((:[0-9a-fA-F]{1,4}){1,7}|:)"#,
|
||||
r#"|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}"#,
|
||||
r#"|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"#,
|
||||
r#"|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"#,
|
||||
r#"")[/](12[0-8]|1[0-1][0-9]|[0-9]?[0-9])$"#,
|
||||
).to_string(),
|
||||
),
|
||||
..Default::default()
|
||||
})),
|
||||
extensions: [("x-rust-type".to_string(), "ipnetwork::Ipv6Network".into())]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
77
src/lib.rs
77
src/lib.rs
@ -1,15 +1,14 @@
|
||||
//! The `ipnetwork` crate provides a set of APIs to work with IP CIDRs in
|
||||
//! Rust.
|
||||
#![crate_type = "lib"]
|
||||
#![doc(html_root_url = "https://docs.rs/ipnetwork/0.18.0")]
|
||||
|
||||
#![deny(
|
||||
missing_debug_implementations,
|
||||
unsafe_code,
|
||||
unused_extern_crates,
|
||||
unused_import_braces)]
|
||||
unused_import_braces
|
||||
)]
|
||||
|
||||
use std::{fmt, net::IpAddr, str::FromStr, convert::TryFrom};
|
||||
use std::{convert::TryFrom, fmt, net::IpAddr, str::FromStr};
|
||||
|
||||
mod common;
|
||||
mod ipv4;
|
||||
@ -46,7 +45,75 @@ impl serde::Serialize for IpNetwork {
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
serializer.collect_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl schemars::JsonSchema for IpNetwork {
|
||||
fn schema_name() -> String {
|
||||
"IpNetwork".to_string()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||
schemars::schema::SchemaObject {
|
||||
metadata: Some(
|
||||
schemars::schema::Metadata {
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
subschemas: Some(
|
||||
schemars::schema::SubschemaValidation {
|
||||
one_of: Some(vec![
|
||||
schemars::schema::SchemaObject {
|
||||
metadata: Some(
|
||||
schemars::schema::Metadata {
|
||||
title: Some("v4".to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
subschemas: Some(
|
||||
schemars::schema::SubschemaValidation {
|
||||
all_of: Some(vec![gen.subschema_for::<Ipv4Network>()]),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
schemars::schema::SchemaObject {
|
||||
metadata: Some(
|
||||
schemars::schema::Metadata {
|
||||
title: Some("v6".to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
subschemas: Some(
|
||||
schemars::schema::SubschemaValidation {
|
||||
all_of: Some(vec![gen.subschema_for::<Ipv6Network>()]),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
]),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
extensions: [("x-rust-type".to_string(), "ipnetwork::IpNetwork".into())]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
#[test]
|
||||
@ -11,6 +11,7 @@ mod tests {
|
||||
let json_string = r#"{"ipnetwork":"127.1.0.0/24"}"#;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
struct MyStruct {
|
||||
ipnetwork: Ipv4Network,
|
||||
}
|
||||
@ -21,6 +22,11 @@ mod tests {
|
||||
assert_eq!(mystruct.ipnetwork.prefix(), 24);
|
||||
|
||||
assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string);
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
if let Err(s) = does_it_json::validate_with_output(&mystruct) {
|
||||
panic!("{}", s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -28,6 +34,7 @@ mod tests {
|
||||
let json_string = r#"{"ipnetwork":"::1/0"}"#;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
struct MyStruct {
|
||||
ipnetwork: Ipv6Network,
|
||||
}
|
||||
@ -41,6 +48,11 @@ mod tests {
|
||||
assert_eq!(mystruct.ipnetwork.prefix(), 0);
|
||||
|
||||
assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string);
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
if let Err(s) = does_it_json::validate_with_output(&mystruct) {
|
||||
panic!("{}", s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -48,6 +60,7 @@ mod tests {
|
||||
let json_string = r#"{"ipnetwork":["127.1.0.0/24","::1/0"]}"#;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
struct MyStruct {
|
||||
ipnetwork: Vec<IpNetwork>,
|
||||
}
|
||||
@ -63,5 +76,10 @@ mod tests {
|
||||
assert_eq!(mystruct.ipnetwork[1].prefix(), 0);
|
||||
|
||||
assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string);
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
if let Err(s) = does_it_json::validate_with_output(&mystruct) {
|
||||
panic!("{}", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user