Git Product home page Git Product logo

duration-str's People

Contributors

baoyachi avatar leo1003 avatar llvm-x86 avatar nstinus avatar pouriya avatar sno2 avatar thealgorythm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

duration-str's Issues

[request] Serialize impl

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

add config check function

motivation

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.

`duration-str` does not export its `Result` or `Error` type

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.

Optional fields fail to deserialize when missing

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.

License

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.

duration_str::parse("0") error

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.