From 6401f2bfa02affd9ef5fed7a624e7b0dbca80a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Saparelli?= Date: Sun, 24 Jul 2022 03:33:55 +1200 Subject: [PATCH] Replace toml with toml_edit (#233) --- Cargo.lock | 39 ++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/metafiles/v1.rs | 28 ++++++++++------------------ 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 411b72c6..fab7b3cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,7 +131,7 @@ dependencies = [ "thiserror", "tinytemplate", "tokio", - "toml", + "toml_edit", "url", "xz2", "zip", @@ -223,6 +223,16 @@ dependencies = [ "cc", ] +[[package]] +name = "combine" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a604e93b79d1808327a6fca85a6f2d69de66461e7620f5a4cbf5fb4d1d7c948" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -295,6 +305,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "either" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" + [[package]] name = "embed-resource" version = "1.7.3" @@ -724,6 +740,15 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" +[[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.2" @@ -1622,6 +1647,18 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_edit" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5376256e44f2443f8896ac012507c19a012df0fe8758b55246ae51a2279db51f" +dependencies = [ + "combine", + "indexmap", + "itertools", + "serde", +] + [[package]] name = "tower-service" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index 26541c2a..e033ca74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ tempfile = "3.3.0" thiserror = "1.0.31" tinytemplate = "1.2.1" tokio = { version = "1.20.0", features = ["rt-multi-thread", "process", "sync"], default-features = false } -toml = "0.5.8" +toml_edit = { version = "0.14.4", features = ["easy"] } url = "2.2.2" xz2 = "0.1.7" diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index def69db6..b91f715a 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -1,10 +1,9 @@ use std::{ collections::{BTreeMap, BTreeSet}, - fs, + fs::File, io::{self, Seek}, iter::IntoIterator, path::{Path, PathBuf}, - str::FromStr, }; use miette::Diagnostic; @@ -31,12 +30,12 @@ impl CratesToml { pub fn load_from_reader(mut reader: R) -> Result { let mut vec = Vec::new(); reader.read_to_end(&mut vec)?; - Ok(toml::from_slice(&vec)?) + Ok(toml_edit::easy::from_slice(&vec)?) } pub fn load_from_path(path: impl AsRef) -> Result { - let file = fs::read_to_string(path)?; - Self::from_str(&file) + let file = File::open(path)?; + Self::load_from_reader(file) } pub fn insert(&mut self, cvs: &CrateVersionSource, bins: BTreeSet) { @@ -48,12 +47,12 @@ impl CratesToml { } pub fn write_to_writer(&self, mut writer: W) -> Result<(), CratesTomlParseError> { - let data = toml::to_vec(&self)?; + let data = toml_edit::easy::to_vec(&self)?; writer.write_all(&data)?; Ok(()) } - pub fn write_to_file(&self, file: &mut fs::File) -> Result<(), CratesTomlParseError> { + pub fn write_to_file(&self, file: &mut File) -> Result<(), CratesTomlParseError> { self.write_to_writer(&mut *file)?; let pos = file.stream_position()?; file.set_len(pos)?; @@ -62,8 +61,8 @@ impl CratesToml { } pub fn write_to_path(&self, path: impl AsRef) -> Result<(), CratesTomlParseError> { - fs::write(path, &toml::to_vec(&self)?)?; - Ok(()) + let mut file = File::create(path)?; + self.write_to_file(&mut file) } pub fn append_to_path<'a, Iter>( @@ -94,23 +93,16 @@ impl CratesToml { } } -impl FromStr for CratesToml { - type Err = CratesTomlParseError; - fn from_str(s: &str) -> Result { - Ok(toml::from_str(s)?) - } -} - #[derive(Debug, Diagnostic, Error)] pub enum CratesTomlParseError { #[error(transparent)] Io(#[from] io::Error), #[error(transparent)] - TomlParse(#[from] toml::de::Error), + TomlParse(#[from] toml_edit::easy::de::Error), #[error(transparent)] - TomlWrite(#[from] toml::ser::Error), + TomlWrite(#[from] toml_edit::easy::ser::Error), #[error(transparent)] CvsParse(#[from] super::CvsParseError),