-
Notifications
You must be signed in to change notification settings - Fork 74
implement net.cidr_is_valid builtin
#422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Anand Krishnamoorthi (anakrish)
merged 5 commits into
microsoft:main
from
tjons:tjons/net-cidr-is-valid
Aug 6, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53a2ce5
wip on parsing ipv4 addresses
tjons 4487529
finish ipv4 cidr_is_valid
tjons 5797da6
implement v6 and handle code review
tjons e322f5b
make errors more informative
tjons 74b2b25
rework control flow to return a bool, not an error; enable builtin tests
tjons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| use core::net::IpAddr; | ||
| use std::format; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::ast::{Expr, Ref}; | ||
| use crate::builtins; | ||
| use crate::builtins::utils::ensure_args_count; | ||
| use crate::lexer::Span; | ||
| use crate::value::Value; | ||
|
|
||
| use anyhow::{anyhow, bail, Result}; | ||
|
|
||
| use super::utils::ensure_string; | ||
|
|
||
| pub fn register(m: &mut builtins::BuiltinsMap<&'static str, builtins::BuiltinFcn>) { | ||
| m.insert("net.cidr_is_valid", (cidr_is_valid, 1)); | ||
| } | ||
|
|
||
| /// Checks if a CIDR string is valid or invalid. Uses the | ||
| /// `net::IpAddr` type to determine if the string is a valid IP, | ||
| /// and checks to ensure that the mask is in bounds for the parsed | ||
| /// IP address type (v4 or v6). | ||
| pub fn cidr_is_valid( | ||
| span: &Span, | ||
| params: &[Ref<Expr>], | ||
| args: &[Value], | ||
| _strict: bool, | ||
| ) -> Result<Value> { | ||
| ensure_args_count(span, "cidr_is_valid", params, args, 1)?; | ||
| let cidr = ensure_string("cidr_is_valid", ¶ms[0], &args[0])?; | ||
|
|
||
| is_valid_cidr(cidr).map_err(|e| span.error(&format!("{}", e))) | ||
| } | ||
|
|
||
| fn is_valid_cidr(cidr: Arc<str>) -> Result<Value> { | ||
| let Some((ip_addr, prefix_len)) = cidr.split_once("/") else { | ||
| bail!("invalid CIDR") | ||
| }; | ||
| match ip_addr.parse::<IpAddr>() { | ||
| Ok(addr) => { | ||
| let mask = prefix_len | ||
| .parse::<i16>() | ||
| .map_err(|_| anyhow!("failed to parse prefix length"))?; | ||
|
|
||
| match addr { | ||
| IpAddr::V4(_) => { | ||
| if !(0..=32).contains(&mask) { | ||
| bail!("invalid CIDR: {} is not a valid IPv4 CIDR mask", &mask) | ||
| } | ||
| } | ||
| IpAddr::V6(_) => { | ||
| if !(0..=128).contains(&mask) { | ||
| bail!("invalid CIDR: {} is not a valid IPv6 CIDR mask", &mask) | ||
| } | ||
| } | ||
| } | ||
| Ok(Value::Bool(true)) | ||
| } | ||
| Err(_) => bail!( | ||
| "Invalid CIDR: {} could not be parsed as a IPv4 or IPv6 address", | ||
| ip_addr | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod net_tests { | ||
| use super::*; | ||
| use std::format; | ||
| use std::vec::Vec; | ||
|
|
||
| #[test] | ||
| fn test_cidr_is_valid() { | ||
| let valids = Vec::from(["127.0.0.1/32", "10.0.0.0/8", "0.1.2.3/32", "::1/128"]); | ||
| let invalids = Vec::from(["256.0.0.0/8", "127.0.0.1/33", "::1/129"]); | ||
|
|
||
| for cidr in valids { | ||
| assert_eq!( | ||
| is_valid_cidr(Arc::from(cidr)).unwrap(), | ||
| Value::Bool(true), | ||
| "Valid CIDR {} deemed invalid", | ||
| cidr | ||
| ); | ||
| } | ||
|
|
||
| for cidr in invalids { | ||
| is_valid_cidr(Arc::from(cidr)) | ||
| .expect_err(format!("Invalid CIDR {} deemed valid", cidr).as_str()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.