Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
0xflotus committed Jan 9, 2024
1 parent 5fec76f commit 1600f8a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ip2d"
description = "A converter for IPv4 addresses"
version = "0.3.0"
version = "0.4.0"
license = "MIT"
authors = ["0xflotus <0xflotus@gmail.com>"]
edition = "2021"
Expand Down
31 changes: 13 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
use clap::{Command, Arg};
use clap::{Arg, ArgAction, Command};

fn main() {
let matches = Command::new("ip2d")
.author("0xflotus")
.version("0.3.0")
.version("0.4.0")
.about("A converter for IPv4 Addresses")
.arg(
Arg::new("ip")
.help("Converts an IPv4 Address to an integer")
.required(false)
.index(1_usize),
)
.arg(
Arg::new("reverse")
.short('r')
.long("reverse")
.takes_value(true)
.value_name("number")
.help("Converts an integer to an IPv4 Address"),
)
.arg(
Arg::new("hex")
.short('x')
.long("hex")
.takes_value(false)
.action(ArgAction::SetTrue)
.help("Converts an IPv4 Address to a hex number"),
)
.get_matches();

if matches.is_present("reverse") {
if let Some(number) = matches.value_of("reverse") {
let num = number.parse::<u32>().unwrap();
let vec: Vec<String> = vec![0x18, 0x10, 0x8, 0x0]
.into_iter()
.map(|x| (num >> x) & 0xff)
.map(|x| x.to_string())
.collect();
println!("{}", vec.join("."));
}
if let Some(number) = matches.get_one::<String>("reverse") {
let num = number.parse::<u32>().unwrap();
let vec: Vec<String> = vec![0x18, 0x10, 0x8, 0x0]
.into_iter()
.map(|x| (num >> x) & 0xff)
.map(|x| x.to_string())
.collect();
println!("{}", vec.join("."));
return;
}

if let Some(ip) = matches.value_of("ip") {
if let Some(ip) = matches.get_one::<String>("ip") {
let splitted = ip
.split(".")
.map(|x| x.parse::<u32>().unwrap())
.fold(0, |x, y| (x << 0x8) | y);
if matches.is_present("hex") {
if matches.get_flag("hex") {
println!("{:#010x}", splitted);
} else {
println!("{}", splitted);
Expand Down

0 comments on commit 1600f8a

Please sign in to comment.