commit 4b6edf53fe2e349b70d6ac74cae11f5b2640e3af Author: Chad Baxter Date: Thu Jun 8 22:46:29 2023 -0400 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5f3d758 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "libax" +version = "0.1.0" +edition = "2021" +authors = ["cbax "] +description = "Helper functions for cbax projects" +readme = "README.md" +homepage = "https://cbax.dev/libax" +repository = "https://github.com/LogoiLab/libax" +license = "GPL-3.0-or-later" +keywords = ["helper", "auxilary"] +publish = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/README.md b/README.md new file mode 100644 index 0000000..3690221 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# libax + +A small collection of functions I use all the time. + +Currently only implements to_u32 on Ipv4Addr and to_ipv4 on u32. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bb493df --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +mod net; diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000..65a0807 --- /dev/null +++ b/src/net.rs @@ -0,0 +1,37 @@ +use std::net::Ipv4Addr; + +trait ToIpv4 { + fn to_ipv4(self) -> Ipv4Addr; +} + +impl ToIpv4 for u32 { + fn to_ipv4(self) -> Ipv4Addr { + return Ipv4Addr::from(self); + } +} + +trait Tou32 { + fn to_u32(self) -> u32; +} + +impl Tou32 for Ipv4Addr { + fn to_u32(self) -> u32 { + return self.into(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn u32_to_ipv4() { + let ip: Ipv4Addr = 123456789.to_ipv4(); + assert_eq!(ip, Ipv4Addr::new(7, 91, 205, 21)); + } + #[test] + fn ipv4_to_u32() { + let num: u32 = Ipv4Addr::new(7, 91, 205, 21).to_u32(); + assert_eq!(num, 123456789); + } +}