Skip to content

Commit

Permalink
Some house keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiasson committed Jul 4, 2023
1 parent 8fbd3f0 commit 1a008a0
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 265 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

# github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
# patreon: # Replace with a single Patreon username
open_collective: nicholaschiasson
ko_fi: nicholaschiasson
# tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
# community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: nicholaschiasson
issuehunt: nicholaschiasson
# otechie: # Replace with a single Otechie username
# custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
150 changes: 150 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: Rust

on:
push:
branches:
- 'main'
pull_request:

env:
CARGO_TERM_COLOR: always

jobs:
validate:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Check
run: cargo check --verbose
- name: Format
run: cargo fmt --check --verbose
- name: Lint
run: rustup component add clippy && cargo clippy --verbose
- name: Test
run: cargo test --verbose

tag:
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest
needs: [validate]
outputs:
version: ${{ steps.stamp.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Check semver bump
id: check-semver
run: |
if [[ "${{ github.event.head_commit.message }}" =~ ^Merge\ pull\ request\ #[0-9]+\ from\ [^/]+/patch/.+$ ]]
then
echo "semver=patch" >> $GITHUB_OUTPUT
elif [[ "${{ github.event.head_commit.message }}" =~ ^Merge\ pull\ request\ #[0-9]+\ from\ [^/]+/major/.+$ ]]
then
echo "semver=major" >> $GITHUB_OUTPUT
else
echo "semver=minor" >> $GITHUB_OUTPUT
fi
- name: Bump major version and push tag
id: bump-major
if: ${{ steps.check-semver.outputs.semver == 'major' }}
uses: anothrNick/github-tag-action@1.65.0
env:
DEFAULT_BUMP: major
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Bump minor version and push tag
id: bump-minor
if: ${{ steps.check-semver.outputs.semver == 'minor' }}
uses: anothrNick/github-tag-action@1.65.0
env:
DEFAULT_BUMP: minor
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Bump patch version and push tag
id: bump-patch
if: ${{ steps.check-semver.outputs.semver == 'patch' }}
uses: anothrNick/github-tag-action@1.65.0
env:
DEFAULT_BUMP: patch
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Stamp version
id: stamp
run: |
if [[ "${{ steps.check-semver.outputs.semver }}" == patch ]]
then
VERSION=${{ steps.bump-patch.outputs.new_tag }}
elif [[ "${{ steps.check-semver.outputs.semver }}" == major ]]
then
VERSION=${{ steps.bump-major.outputs.new_tag }}
else
VERSION=${{ steps.bump-minor.outputs.new_tag }}
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
sed -i "s/version = \"0.0.0\"/version = \"${VERSION}\"/" Cargo.toml
- name: Upload Build Artifact
uses: actions/upload-artifact@v3.1.2
with:
name: 'Cargo.toml'
path: 'Cargo.toml'

build:
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true)
strategy:
matrix:
platform: [macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
needs: [tag]
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Download Build Artifacts
uses: actions/download-artifact@v3.0.2
with:
name: 'Cargo.toml'
- name: Build
shell: bash
run: |
RAW_BINARY_NAME=fcidr
BINARY_NAME=${RAW_BINARY_NAME}
if [[ ${{ startsWith(matrix.platform, 'windows') }} == true ]]
then
BINARY_NAME=${BINARY_NAME}.exe
fi
cargo build --release --verbose
cp target/release/${BINARY_NAME} ./
tar czf ${RAW_BINARY_NAME}-${{ runner.os }}-${{ runner.arch }}.tar.gz ${BINARY_NAME}
- name: Upload Build Artifact
uses: actions/upload-artifact@v3.1.2
with:
path: '*.tar.gz'

publish:
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest
needs: [tag]
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Download Build Artifacts
uses: actions/download-artifact@v3.0.2
with:
name: 'Cargo.toml'
- name: Publish to crates.io
run: |
cargo login ${{ secrets.CRATES_IO_API_TOKEN }}
cargo publish --allow-dirty --verbose
release:
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest
needs: [tag, build]
steps:
- name: Download Build Artifacts
uses: actions/download-artifact@v3.0.2
- name: Release
uses: softprops/action-gh-release@v0.1.15
with:
files: 'artifact/*.tar.gz'
tag_name: ${{ needs.tag.outputs.version }}
16 changes: 14 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
/target
/Cargo.lock
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
19 changes: 17 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
[package]
name = "fcidr"
version = "0.1.0"
version = "0.0.0"
authors = ["Nicholas Omer Chiasson <nicholasomerchiasson@gmail.com>"]
edition = "2021"
license = "MIT"
description = """
Fragmented Classless Inter-Domain Routing (FCIDR)
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
A library exposing a data structure to represent a set of CIDR ranges and
easily manipulate its entries using set-like operations.
"""
readme = "README.md"
homepage = "https://github.com/nicholaschiasson/fcidr"
repository = "https://github.com/nicholaschiasson/fcidr"
keywords = ["network", "ip", "ipv4", "cidr"]
categories = ["data-structures", "network-programming"]

[badges]
github = { repository = "nicholaschiasson/fcidr" }
maintenance = { status = "passively-maintained" }

[dependencies]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nicholas Omer Chiasson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# fcidr

[![crates.io](https://img.shields.io/crates/v/fcidr)](https://crates.io/crates/fcidr)

Fragmented Classless Inter-Domain Routing (FCIDR)

A library exposing a data structure to represent a set of CIDR ranges and
easily manipulate its entries using set-like operations.

This data structure can be applied, for example, in configuring firewalls that
*implicitly deny* (AWS Security Groups) using a rule set that explicitly
expresses rules for both allow and deny.
38 changes: 18 additions & 20 deletions src/cidr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ pub struct Cidr {

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Error {
CidrBoundsError(String),
InvalidNetworkError(String),
PrefixRangeError(String),
ParseError(String),
TypeCastError(String),
ImpossibleError(String),
CidrNotInRange(String),
InvalidNetwork(String),
InvalidPrefix(String),
Parse(String),
TypeCast(String),
Impossible(String),
}

impl Cidr {
pub fn new(network: Ipv4Addr, prefix: u8) -> Result<Self, Error> {
if prefix as u32 > u32::BITS {
return Err(Error::PrefixRangeError(format!(
return Err(Error::InvalidPrefix(format!(
"network prefix '{prefix}' must be 32 or less"
)));
}
Expand All @@ -37,7 +37,7 @@ impl Cidr {
(if i > 0 { o } else { o << offset >> offset }) != 0
})
{
return Err(Error::InvalidNetworkError(format!(
return Err(Error::InvalidNetwork(format!(
"network address '{network}' must be clear after the first {prefix} bits"
)));
}
Expand Down Expand Up @@ -67,12 +67,12 @@ impl Cidr {
pub fn last(&self) -> Ipv4Addr {
let mut last = self.network.octets();
let first_octet: usize = (self.prefix() / 8).into();
for i in first_octet..last.len() {
if i > first_octet {
last[i] = u8::MAX
for (i, o) in last.iter_mut().skip(first_octet).enumerate() {
if i > 0 {
*o = u8::MAX
} else {
let offset = self.prefix % 8;
last[i] |= u8::MAX << offset >> offset;
*o |= u8::MAX << offset >> offset;
}
}
Ipv4Addr::from(last)
Expand All @@ -82,9 +82,9 @@ impl Cidr {
where
T: Copy + Debug + TryInto<Cidr>,
{
let cidr: Cidr = net.try_into().map_err(|_| {
Error::TypeCastError(format!("could not cast value '{:?}' to cidr", net))
})?;
let cidr: Cidr = net
.try_into()
.map_err(|_| Error::TypeCast(format!("could not cast value '{:?}' to cidr", net)))?;
Ok(cidr.first() >= self.first() && cidr.last() <= self.last())
}

Expand Down Expand Up @@ -128,15 +128,13 @@ impl FromStr for Cidr {
Self::new(
network
.parse::<Ipv4Addr>()
.map_err(|e| Error::ParseError(e.to_string()))?,
.map_err(|e| Error::Parse(e.to_string()))?,
prefix
.parse::<u8>()
.map_err(|e| Error::ParseError(e.to_string()))?,
.map_err(|e| Error::Parse(e.to_string()))?,
)
} else {
Err(Error::ParseError(
"missing network prefix delimiter".to_owned(),
))
Err(Error::Parse("missing network prefix delimiter".to_owned()))
}
}
}
Expand Down
Loading

0 comments on commit 1a008a0

Please sign in to comment.