-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
151 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 @@ | ||
//! Adapter for parsing [`Duration`] with a [`clap::builder::Arg::value_parser`]. | ||
|
||
use std::time::Duration; | ||
|
||
use clap::builder::StringValueParser; | ||
use clap::builder::TypedValueParser; | ||
use clap::builder::ValueParserFactory; | ||
use humantime::DurationError; | ||
use miette::LabeledSpan; | ||
use miette::MietteDiagnostic; | ||
use miette::Report; | ||
|
||
/// Adapter for parsing [`Duration`] with a [`clap::builder::Arg::value_parser`]. | ||
#[derive(Default, Clone)] | ||
pub struct DurationValueParser { | ||
inner: StringValueParser, | ||
} | ||
|
||
impl TypedValueParser for DurationValueParser { | ||
type Value = Duration; | ||
|
||
fn parse_ref( | ||
&self, | ||
cmd: &clap::Command, | ||
arg: Option<&clap::Arg>, | ||
value: &std::ffi::OsStr, | ||
) -> Result<Self::Value, clap::Error> { | ||
self.inner.parse_ref(cmd, arg, value).and_then(|str_value| { | ||
humantime::parse_duration(&str_value).map_err(|err| { | ||
let diagnostic = Report::new(MietteDiagnostic { | ||
message: match &err { | ||
DurationError::InvalidCharacter(_) => "Invalid character".to_owned(), | ||
DurationError::NumberExpected(_) => "Expected number".to_owned(), | ||
DurationError::UnknownUnit { unit, .. } => format!("Unknown unit `{unit}`"), | ||
DurationError::NumberOverflow => "Duration is too long".to_owned(), | ||
DurationError::Empty => "No duration given".to_owned(), | ||
}, | ||
code: None, | ||
severity: None, | ||
help: match &err { | ||
DurationError::InvalidCharacter(_) => { | ||
Some("Non-alphanumeric characters are prohibited".to_owned()) | ||
} | ||
DurationError::NumberExpected(_) => { | ||
Some("Did you split a unit into multiple words?".to_owned()) | ||
} | ||
DurationError::UnknownUnit { .. } => Some( | ||
"Valid units include `ms` (milliseconds) and `s` (seconds)".to_owned(), | ||
), | ||
DurationError::NumberOverflow => None, | ||
DurationError::Empty => None, | ||
}, | ||
url: None, | ||
labels: match err { | ||
DurationError::InvalidCharacter(offset) => Some(vec![LabeledSpan::at( | ||
offset..offset + 1, | ||
"Invalid character", | ||
)]), | ||
DurationError::NumberExpected(offset) => { | ||
Some(vec![LabeledSpan::at(offset..offset + 1, "Expected number")]) | ||
} | ||
DurationError::UnknownUnit { | ||
start, | ||
end, | ||
unit: _, | ||
value: _, | ||
} => Some(vec![LabeledSpan::at(start..end, "Unknown unit")]), | ||
DurationError::NumberOverflow => None, | ||
DurationError::Empty => None, | ||
}, | ||
}) | ||
.with_source_code(str_value); | ||
clap::Error::raw( | ||
clap::error::ErrorKind::ValueValidation, | ||
format!("{diagnostic:?}"), | ||
) | ||
.with_cmd(cmd) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
struct DurationValueParserFactory; | ||
|
||
impl ValueParserFactory for DurationValueParserFactory { | ||
type Parser = DurationValueParser; | ||
|
||
fn value_parser() -> Self::Parser { | ||
Self::Parser::default() | ||
} | ||
} |
This file contains 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,6 @@ | ||
//! Adapters for parsing [`clap`] arguments to various types. | ||
|
||
mod camino; | ||
mod humantime; | ||
|
||
pub use self::humantime::DurationValueParser; |
This file contains 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 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 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 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