From 197d3888540b0feab6c6b9c69546dcb1f2046601 Mon Sep 17 00:00:00 2001 From: Mike Lloyd Date: Thu, 25 Jan 2024 13:16:11 -0800 Subject: [PATCH] Replace yaml with toml --- Cargo.lock | 61 +++++++++++++++++++++++++++++++++++++++------------ Cargo.toml | 2 +- README.md | 10 ++++----- src/config.rs | 17 +++++++------- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 086144b..f2f6599 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1390,12 +1390,12 @@ dependencies = [ "regex", "rexpect", "serde", - "serde_yaml", "sqlx", "tabled", "tempfile", "time", "tokio", + "toml", "uuid", "xdg", ] @@ -1473,16 +1473,12 @@ dependencies = [ ] [[package]] -name = "serde_yaml" -version = "0.9.30" +name = "serde_spanned" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1bf28c79a99f70ee1f1d83d10c875d2e70618417fda01ad1785e027579d9d38" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" dependencies = [ - "indexmap", - "itoa", - "ryu", "serde", - "unsafe-libyaml", ] [[package]] @@ -1992,6 +1988,40 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tracing" version = "0.1.40" @@ -2069,12 +2099,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" -[[package]] -name = "unsafe-libyaml" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b" - [[package]] name = "url" version = "2.5.0" @@ -2422,6 +2446,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "winnow" +version = "0.5.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7cf47b659b318dccbd69cc4797a39ae128f533dce7902a1096044d1967b9c16" +dependencies = [ + "memchr", +] + [[package]] name = "xdg" version = "2.5.2" diff --git a/Cargo.toml b/Cargo.toml index a87b82b..f179c61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,8 @@ regex = "1.10.3" tabled = { version = "0.14.0", features = ["color"] } time = "0.3.31" clap_complete = "4.4.9" -serde_yaml = "0.9.30" duration-str = "0.7.1" +toml = "0.8.8" [dev-dependencies] assert_cmd = "2.0.13" diff --git a/README.md b/README.md index 036721e..1410a4d 100644 --- a/README.md +++ b/README.md @@ -108,17 +108,17 @@ If the `RUDRIC_SESSION` token is set in the environment: # Configuration -Rudric can be configured with a yaml file. By default, this file is stored in `XDG_CONFIG/rudric/config.yaml` (`$HOME/.config/rudric/config.yaml` on Linux and Mac). All config options are optional. An example config file might look like this: +Rudric can be configured with a toml file. By default, this file is stored in `XDG_CONFIG/rudric/config.toml` (`$HOME/.config/rudric/config.toml` on Linux and Mac). All config options are optional. An example config file might look like this: -```yaml +```toml # Options are: bash, zsh, fish, nu -default_shell: fish +default_shell = "fish" # Specify the default length of time that a session token is valid for -session_lifetime: 6h +session_lifetime = "6h" # Specify the name of the file to use in the CWD for setting environment variables (default ".renv") -renv_filename: .env +renv_filename = ".env" ``` # direnv diff --git a/src/config.rs b/src/config.rs index 674dfda..3b81d91 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::{fs, path::Path}; use anyhow::Result; use duration_str::deserialize_option_duration_time; @@ -16,15 +16,16 @@ pub struct Config { impl Config { pub fn load(config_dir: &Path) -> Result { - let config_file_path = config_dir.join("config.yaml"); - let config_file = match std::fs::File::open(config_file_path) { + let config_file_path = config_dir.join("config.toml"); + + let config_string = match fs::read_to_string(config_file_path) { Ok(c) => c, Err(_) => { return Ok(Self::default()); } }; - Ok(serde_yaml::from_reader(config_file)?) + Ok(toml::from_str(&config_string)?) } } @@ -41,9 +42,9 @@ mod session_tests { let test_dir = "testdata/test_config1"; std::fs::create_dir_all(test_dir)?; - let config_path = test_dir.to_string() + "/config.yaml"; + let config_path = test_dir.to_string() + "/config.toml"; let mut file = File::create(config_path)?; - file.write_all(b"default_shell: fish\nsession_lifetime: 6h")?; + file.write_all(b"default_shell = \"fish\"\nsession_lifetime = \"6h\"")?; let config = Config::load(Path::new(test_dir))?; @@ -60,9 +61,9 @@ mod session_tests { let test_dir = "testdata/test_config2"; std::fs::create_dir_all(test_dir)?; - let config_path = test_dir.to_string() + "/config.yaml"; + let config_path = test_dir.to_string() + "/config.toml"; let mut file = File::create(config_path)?; - file.write_all(b"default_shell: fish")?; + file.write_all(b"default_shell = \"fish\"")?; let config = Config::load(Path::new(test_dir))?;