Replace toml with toml_edit (#233)

This commit is contained in:
Félix Saparelli 2022-07-24 03:33:55 +12:00 committed by GitHub
parent e93f0beb4b
commit 6401f2bfa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 20 deletions

39
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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<R: io::Read>(mut reader: R) -> Result<Self, CratesTomlParseError> {
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<Path>) -> Result<Self, CratesTomlParseError> {
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<String>) {
@ -48,12 +47,12 @@ impl CratesToml {
}
pub fn write_to_writer<W: io::Write>(&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<Path>) -> 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<Self, Self::Err> {
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),