Skip to content

Commit

Permalink
feat: added v6 option (#166)
Browse files Browse the repository at this point in the history
* feat: added v6 option

* test: fixed test name
  • Loading branch information
0xflotus authored Oct 10, 2024
1 parent 831c9a6 commit 3d86f4f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ pub fn to_integer(ip: &str) -> Result<u128> {
}
}

pub fn to_str(num: u128) -> String {
pub fn to_str(num: u128, v6: bool) -> String {
let addr = Ipv6Addr::from(num);
if v6 {
return addr.to_string();
}
// Try and see if we can return an IPv4 address, otherwise fallback to IPv6.
match addr.to_ipv4() {
Some(a) => a.to_string(),
Expand Down Expand Up @@ -69,25 +72,25 @@ mod tests {

#[test]
fn str_works_with_ipv6() {
let result = to_str(50543257694033307102031451402929180945);
let result = to_str(50543257694033307102031451402929180945, false);
assert_eq!(result, "2606:4700:4700::1111");
}

#[test]
fn str_works_with_4278255360() {
let result = to_str(4278255360);
let result = to_str(4278255360, false);
assert_eq!(result, "255.0.255.0");
}

#[test]
fn str_works_with_1() {
let result = to_str(1);
let result = to_str(1, false);
assert_eq!(result, "0.0.0.1");
}

#[test]
fn str_works_with_4294967295() {
let result = to_str(4294967295);
let result = to_str(4294967295, false);
assert_eq!(result, "255.255.255.255");
}
}
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ fn main() -> Result<()> {
.action(ArgAction::SetTrue)
.help("Converts an IP Address to a hex number"),
)
.arg(
Arg::new("v6")
.short('6')
.long("v6")
.action(ArgAction::SetTrue)
.help("Force print an IPv6"),
)
.get_matches();

if let Some(number) = matches.get_one::<String>("reverse") {
let num = number.parse::<u128>()?;
println!("{}", to_str(num));
if matches.get_flag("v6") {
println!("{}", to_str(num, true));
} else {
println!("{}", to_str(num, false));
}
return Ok(());
}

if let Some(ip) = matches.get_one::<String>("ip") {
if let Ok(splitted) = to_integer(ip) {
if matches.get_flag("hex") {
println!("{:#010x}", splitted);
if matches.get_flag("v6") {
println!("{:#034x}", splitted);
} else {
println!("{:#010x}", splitted);
}
} else {
println!("{}", splitted);
}
Expand Down
22 changes: 21 additions & 1 deletion test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,24 @@
@test "convert the right ip" {
OUTPUT=$(./target/release/ip2d -r 16843009)
[ "$OUTPUT" = "1.1.1.1" ]
}
}

@test "convert the right ipv6" {
OUTPUT=$(./target/release/ip2d -6 -r 16843009)
[ "$OUTPUT" = "::101:101" ]
}

@test "convert the right number from v6" {
OUTPUT=$(./target/release/ip2d ::101:101)
[ "$OUTPUT" = "16843009" ]
}

@test "convert the right hex number from v6 ::1" {
OUTPUT=$(./target/release/ip2d ::1 -x -6)
[ "$OUTPUT" = "0x00000000000000000000000000000001" ]
}

@test "convert the right hex number from v6 ffff:34::1" {
OUTPUT=$(./target/release/ip2d ffff:34::1 -x -6)
[ "$OUTPUT" = "0xffff0034000000000000000000000001" ]
}

0 comments on commit 3d86f4f

Please sign in to comment.