baoyachi / duration-str Goto Github PK
View Code? Open in Web Editor NEWduration string parser write by Rust
Home Page: https://docs.rs/duration-str
duration string parser write by Rust
Home Page: https://docs.rs/duration-str
The time
crate supports negative Duration
, it would be cool if this crate also supported negative value in parse_time
.
Hi,
Thank you for this crate!
I'm not sure how difficult this is, but can we have the Serialize
impl as well? So we can go back and forth?
Of course, if you have some spare time.
Thanks,
Nico
#[structopt(short, long, parse(/*a structopt compatible parser*/))]
pub duration: Duration,
Sometimes when use config duration-str
into xxx.toml
file. But to ensure safety, in code also add config duration check.
So add check config validate function is very useful.
I needed to write a wrapper around one of the parse functions to make it play nicely with clap
's derive functionality, but I found I could not do this without introducing a dependency on anyhow
since aliases for the error and result types used aren't available in the crate directly.
When using deserialize_option_duration_time
and deserializing a yaml file, if the field is missing from the yaml file, serde_yaml::from_reader
will raise an error that the field is missing. This should not happen for fields of type Option<T>
.
#[derive(Default, Deserialize)]
pub struct Config {
pub default_shell: Option<ShellType>,
#[serde(deserialize_with = "deserialize_option_duration_time")]
pub session_lifetime: Option<time::Duration>,
}
impl Config {
pub fn load(config_dir: &Path) -> Result<Self> {
let config_file_path = config_dir.join("config.yaml");
let config_file = match std::fs::File::open(config_file_path) {
Ok(c) => c,
Err(e) => {
eprintln!("Failed to open config file: {e}");
eprintln!("Using defaults");
return Ok(Self::default());
}
};
Ok(serde_yaml::from_reader(config_file)?)
}
}
For these two tests, the first one passes and the second one fails:
#[test]
fn test_load_config() -> Result<()> {
let test_dir = "testdata/test_config1";
std::fs::create_dir_all(test_dir)?;
let config_path = test_dir.to_string() + "/config.yaml";
let mut file = File::create(config_path)?;
file.write_all(b"default_shell: fish\nsession_lifetime: 6h")?;
let config = Config::load(&Path::new(test_dir))?;
assert_eq!(config.default_shell, Some(ShellType::Fish));
assert_eq!(config.session_lifetime, Some(Duration::hours(6)));
std::fs::remove_dir_all(test_dir)?;
Ok(())
}
#[test]
fn test_load_config_missing_field() -> Result<()> {
let test_dir = "testdata/test_config2";
std::fs::create_dir_all(test_dir)?;
let config_path = test_dir.to_string() + "/config.yaml";
let mut file = File::create(config_path)?;
file.write_all(b"default_shell: fish")?;
let config = Config::load(&Path::new(test_dir))?;
assert_eq!(config.default_shell, Some(ShellType::Fish));
assert_eq!(config.session_lifetime, None);
std::fs::remove_dir_all(test_dir)?;
Ok(())
}
failures:
---- config::session_tests::test_load_config_missing_field stdout ----
Error: missing field `session_lifetime`
Furthermore, if I remove deserialize_option_duration_time
and change session_lifetime
to Option<String>
, the test does not raise an error for a missing field.
It seems like Humantime hasn't been updated for three years, so we should be compatible with it.
Ref:https://crates.io/crates/humantime
I was wondering if the license declared in the Cargo.toml file is indeed supposed to be MIT AND Apache-2.0 or if you actually meant that I can choose to use your code under either MIT or Apache-2.0 (or both for that matter)?
Most Rust libs seem to use MIT OR Apache-2.0
and explicitly state so in their Readme.
error detail:
called `Result::unwrap()` on an `Err` value: DSLError("Parsing Error: Error { input: \"\", code: Alpha }")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
println!("{:?}",parse("0mxx"));
Err(DSLError("0mxx\n ^\n"))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.