Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to toml 0.6 #6

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/.cspell/rust-dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ clap
config
criterion
duct
edit
escape
expr
home
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ home = "0.5"
once_cell = { version = "1", default-features = false }
serde = { version = "1.0.103", features = ["derive"] }
shell-escape = "0.1.5"
toml_edit = { version = "0.17", features = ["easy"] }
toml = { version = "0.6", default-features = false, features = ["parse"] }

[dev-dependencies]
anyhow = "1"
Expand All @@ -42,4 +42,5 @@ rustversion = "1"
serde_json = "1"
static_assertions = "1"
tempfile = "3"
toml = "0.6"
walkdir = "2"
3 changes: 0 additions & 3 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ publish = false
cargo-config2 = { path = ".." }

criterion = { default-features = false, git = "https://github.com/taiki-e/criterion.rs.git", branch = "dev" } # reduce deps
fs-err = "2"
toml = "0.5"
toml_edit = { version = "0.17", features = ["easy"] }

[[bench]]
name = "bench"
Expand Down
11 changes: 0 additions & 11 deletions bench/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{collections::HashMap, hint::black_box, path::Path};

use cargo_config2::{PathAndArgs, ResolveOptions};
use criterion::{criterion_group, criterion_main, Criterion};
use fs_err as fs;

fn fixtures_path() -> &'static Path {
Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/fixtures"))
Expand All @@ -20,16 +19,6 @@ fn test_options() -> ResolveOptions {
fn reference(c: &mut Criterion) {
let mut g = c.benchmark_group("reference");
let dir = &fixtures_path().join("reference");
let config_path = &dir.join(".cargo/config.toml");
let buf = &black_box(fs::read(config_path).unwrap());
g.bench_function("parse_toml_rs", |b| {
b.iter(|| black_box(toml::from_slice::<cargo_config2::de::Config>(buf).unwrap()));
});
g.bench_function("parse_toml_edit", |b| {
b.iter(|| {
black_box(toml_edit::easy::from_slice::<cargo_config2::de::Config>(buf).unwrap())
});
});
g.bench_function("load_config_easy", |b| {
b.iter(|| {
let config = cargo_config2::Config::load_with_options(dir, test_options()).unwrap();
Expand Down
37 changes: 9 additions & 28 deletions examples/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,41 +83,22 @@ fn print_config(writer: &mut dyn Write, format: Format, config: &Config) -> Resu
// ```
//
// Neither toml nor toml_edit supports this output format, so format it manually.
fn print_item(
writer: &mut dyn Write,
path: &str,
item: &toml_edit::Item,
) -> Result<()> {
match item {
toml_edit::Item::None => {}
toml_edit::Item::Value(value) => print_value(writer, path, value)?,
toml_edit::Item::Table(table) => {
for (key, item) in table.iter() {
print_item(writer, &format!("{path}.{key}"), item)?;
}
}
toml_edit::Item::ArrayOfTables(_) => todo!(),
}
Ok(())
}
fn print_value(
writer: &mut dyn Write,
path: &str,
value: &toml_edit::Value,
) -> Result<()> {
fn print_value(writer: &mut dyn Write, path: &str, value: &toml::Value) -> Result<()> {
match value {
toml_edit::Value::InlineTable(table) => {
for (key, value) in table.iter() {
print_value(writer, &format!("{path}.{key}"), value)?;
toml::Value::Table(table) => {
for (key, item) in table.iter() {
print_value(writer, &format!("{path}.{key}"), item)?;
}
}
_ => writeln!(writer, "{path} = {value}")?,
}
Ok(())
}
let doc = toml_edit::easy::to_document(&config)?;
for (key, item) in doc.iter() {
print_item(writer, key, item)?;
let doc = toml::from_str::<toml::Value>(&toml::to_string(&config)?)?;
if let Some(table) = doc.as_table() {
for (key, value) in table {
print_value(writer, key, value)?;
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ impl Config {
Self::_load_file(path.as_ref())
}
fn _load_file(path: &Path) -> Result<Self> {
let buf = fs::read(path).with_context(|| format!("failed to read `{}`", path.display()))?;
let mut config: Config = toml_edit::easy::from_slice(&buf).with_context(|| {
let buf = fs::read_to_string(path)
.with_context(|| format!("failed to read `{}`", path.display()))?;
let mut config: Config = toml::from_str(&buf).with_context(|| {
format!("failed to parse `{}` as cargo configuration", path.display())
})?;
config.set_path(path);
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl From<Error> for io::Error {
ErrorKind::WithContext(msg, Some(source)) => {
let kind = if let Some(e) = source.downcast_ref::<io::Error>() {
e.kind()
} else if source.downcast_ref::<toml_edit::de::Error>().is_some() {
} else if source.downcast_ref::<toml::de::Error>().is_some() {
io::ErrorKind::InvalidData
} else {
io::ErrorKind::Other
Expand Down
7 changes: 3 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{collections::HashMap, path::Path, str};
use anyhow::{Context as _, Result};
use build_info::TARGET;
use cargo_config2::*;
use toml_edit::easy as toml;

fn test_options() -> ResolveOptions {
ResolveOptions::default()
Expand Down Expand Up @@ -131,7 +130,7 @@ fn assert_reference_example(de: fn(&Path, ResolveOptions) -> Result<Config>) ->
assert_eq!(config.term.progress.when, Some(When::Auto));
assert_eq!(config.term.progress.width, Some(80));

let _config = toml_edit::easy::to_string(&config).unwrap();
let _config = toml::to_string(&config).unwrap();

Ok(())
}
Expand Down Expand Up @@ -162,7 +161,7 @@ fn de() {
let base_config = &de(dir, test_options()).unwrap();
let config = base_config.clone();

let _config = toml_edit::easy::to_string(&config).unwrap();
let _config = toml::to_string(&config).unwrap();

assert_eq!("", ser(&de::Config::default()));
}
Expand Down Expand Up @@ -210,7 +209,7 @@ fn custom_target() {
);
assert_eq!(config.build_target_for_cli([spec_file_name])?, vec![spec_file_name.to_owned()]);

let _config = toml_edit::easy::to_string(&config).unwrap();
let _config = toml::to_string(&config).unwrap();

Ok(())
}
Expand Down