Skip to content

Commit

Permalink
feat: Refactor CIDR parsing functions for improved value handling and…
Browse files Browse the repository at this point in the history
… add tests for new functionality

Signed-off-by: Akash <akashsingh2210670@gmail.com>
  • Loading branch information
SkySingh04 committed Dec 21, 2024
1 parent 53a8e18 commit cbe3cc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
21 changes: 11 additions & 10 deletions kclvm/runtime/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,17 @@ pub extern "C" fn kclvm_net_parse_CIDR(
let mask = parts[1];
if let Ok(ip) = Ipv4Addr::from_str(ip) {
if let Ok(mask) = mask.parse::<u8>() {
return ValueRef::dict(
vec![
("ip", ValueRef::str(ip.to_string().as_str())),
("mask", ValueRef::i_int(mask as i64)),
],
None,
)
let ip_value = ValueRef::str(ip.to_string().as_str());
let mask_value = ValueRef::int(mask as i64);
return ValueRef::dict(Some(&[
("ip", &ip_value),
("mask", &mask_value),
]))
.into_raw(ctx);
}
}
}
return ValueRef::dict(vec![], None).into_raw(ctx);
return ValueRef::dict(None).into_raw(ctx);
}

panic!("parse_CIDR() missing 1 required positional argument: 'cidr'");
Expand Down Expand Up @@ -455,7 +454,8 @@ pub extern "C" fn kclvm_net_hosts_in_CIDR (
let ip = u32::from_be_bytes(ip.octets()) + i;
hosts.push(ValueRef::str(Ipv4Addr::from(ip).to_string().as_str()));
}
return ValueRef::list(Some(hosts)).into_raw(ctx);
let hosts_refs: Vec<&ValueRef> = hosts.iter().collect();
return ValueRef::list(Some(&hosts_refs[..])).into_raw(ctx);
}
}
}
Expand Down Expand Up @@ -489,7 +489,8 @@ pub extern "C" fn kclvm_net_subnets_from_CIDR (
let ip = u32::from_be_bytes(ip.octets()) + i;
subnets.push(ValueRef::str(format!("{}/{}", Ipv4Addr::from(ip), mask).as_str()));
}
return ValueRef::list(Some(subnets)).into_raw(ctx);
let subnets_refs: Vec<&ValueRef> = subnets.iter().collect();
return ValueRef::list(Some(&subnets_refs)).into_raw(ctx);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/grammar/builtins/net/is_ip_2/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ isip2 = net.is_link_local_multicast_IP("224.0.0.0")
isip3 = net.is_link_local_unicast_IP("fe80::2012:1")
isip4 = net.is_global_unicast_IP("220.181.108.89")
isip5 = net.is_unspecified_IP("0.0.0.0")
isip6 = net.parse_CIDR("192.168.1.0/24")
isip7 = net.hosts_in_CIDR("192.168.1.0/24")
isip8 = net.subnets_from_CIDR("192.168.1.0/24")
isip9 = net.is_IP_in_CIDR("192.168.1.1", "192.168.1.0/24")

0 comments on commit cbe3cc6

Please sign in to comment.