-
Notifications
You must be signed in to change notification settings - Fork 75
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(stdlib): allow parsing multiple durations at once in parse_duration
function
#1197
Conversation
parse_duration
functionparse_duration
function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @titaneric! I left a few comments. The most interesting one is around the RE
change.
changelog.d/1198.feature.md
Outdated
@@ -0,0 +1,3 @@ | |||
Added `parse_bytes` function to parse bytes either from SI or binary unit depending on the given `base` argument. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please git rm
this file.
changelog.d/1197.feature.md
Outdated
@@ -0,0 +1 @@ | |||
Support multiple-units duration string for `parse_duration` function such as `1h2s`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support multiple-units duration string for `parse_duration` function such as `1h2s`. | |
Added support for multi-unit duration strings (e.g., `1h2s`, `2m3s`) in the `parse_duration` function. |
src/stdlib/parse_duration.rs
Outdated
let captures = RE | ||
.captures(value) | ||
.ok_or(format!("unable to parse duration: '{value}'"))?; | ||
let capture_match = captures.get(0).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use unwrap()
, we can return an error here.
src/stdlib/parse_duration.rs
Outdated
.to_f64() | ||
.ok_or(format!("unable to format duration: '{number}'"))?; | ||
Ok(Value::from_f64_or_zero(number)) | ||
let mut num = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
let mut num = 0.0; | |
let mut sum = 0.0; |
} | ||
|
||
static RE: Lazy<Regex> = Lazy::new(|| { | ||
Regex::new( | ||
r"(?ix) # i: case-insensitive, x: ignore whitespace + comments | ||
\A |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed you remove the anchors here, I suppose this is needed for iterative parsing. What are the implications of this? Would this function now accept strings like 1h2s extrablah
?
I wonder if we can leverage https://docs.rs/humantime/latest/humantime/ here without breaking existing behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I remove it since I want to iterate the regex matching.
The duration 1h2s extrablah
would not be accepted, and its error is Err("unable to parse duration: ' extrablah'")
humantime
seems promising for me! I would try it in the next commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @titaneric, this function is now more powerful than before and easier to maintain.
src/stdlib/parse_duration.rs
Outdated
.to_f64() | ||
.ok_or(format!("unable to format duration: '{number}'"))?; | ||
let duration = ht_parse_duration(&trimmed_value) | ||
.map_err(|_| format!("unable to parse duration: '{value}'"))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.map_err(|_| format!("unable to parse duration: '{value}'"))?; | |
.map_err(|e| format!("unable to parse duration: '{e}'"))?; |
This will require updates to the test cases.
Note: there are multiple failing CI checks, you reproduce them locally with |
A little awkward... We need to update MSRV to 1.80 to support There is another |
No worries, we can bump the MSRV. Do you mind doing the bump in a separate PR please? |
ok, I could raise another PR to bump the MSRV |
ea72949
to
35dc8e2
Compare
I've rebased the main branch and update the LICENSE, please check! |
Thank you @titaneric, this looks great. |
Summary
Hi, I hope that
parse_duration
could parse multiple duration at once, so I have opened #1198 as a referenced issue.Change Type
Is this a breaking change?
How did you test this PR?
Does this PR include user facing changes?
our guidelines.
Checklist
run
dd-rust-license-tool write
and commit the changes. More details here.References
Closes #1196