Skip to content

Commit

Permalink
chore: Update to toml v0.6, toml_edit v0.18
Browse files Browse the repository at this point in the history
`toml` replaces `toml_edit::easy`, using `toml_edit` as its parser.
  • Loading branch information
epage committed Jan 19, 2023
1 parent 2286d5f commit 48a61a3
Show file tree
Hide file tree
Showing 33 changed files with 150 additions and 182 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ tar = { version = "0.4.38", default-features = false }
tempfile = "3.0"
termcolor = "1.1"
time = { version = "0.3", features = ["parsing", "formatting"]}
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
toml_edit = "0.19.0"
toml = "0.7.0"
unicode-xid = "0.2.0"
url = "2.2.2"
walkdir = "2.2"
Expand Down
2 changes: 1 addition & 1 deletion benches/capture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ description = "Tool for capturing a real-world workspace for benchmarking."
cargo_metadata = "0.14.0"
flate2 = { version = "1.0.3", default-features = false, features = ["zlib"] }
tar = { version = "0.4.38", default-features = false }
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
toml = "0.7.0"
1 change: 0 additions & 1 deletion benches/capture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use flate2::{Compression, GzBuilder};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use toml_edit::easy as toml;

fn main() {
let force = std::env::args().any(|arg| arg == "-f");
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ serde = { version = "1.0.123", features = ["derive"] }
serde_json = "1.0"
tar = { version = "0.4.38", default-features = false }
termcolor = "1.1.2"
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
toml = "0.7.0"
url = "2.2.2"

[target.'cfg(windows)'.dependencies]
Expand Down
11 changes: 7 additions & 4 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,13 @@ impl Package {
let mut manifest = String::new();

if !self.cargo_features.is_empty() {
manifest.push_str(&format!(
"cargo-features = {}\n\n",
toml_edit::ser::to_item(&self.cargo_features).unwrap()
));
let mut features = String::new();
serde::Serialize::serialize(
&self.cargo_features,
toml::ser::ValueSerializer::new(&mut features),
)
.unwrap();
manifest.push_str(&format!("cargo-features = {}\n\n", features));
}

manifest.push_str(&format!(
Expand Down
1 change: 0 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use anyhow::Context as _;
use semver::Version;
use serde::ser;
use serde::Serialize;
use toml_edit::easy as toml;
use url::Url;

use crate::core::compiler::rustdoc::RustdocScrapeExamples;
Expand Down
1 change: 0 additions & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use lazycell::LazyCell;
use log::{debug, warn};
use semver::Version;
use serde::Serialize;
use toml_edit::easy as toml;

use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
Expand Down
1 change: 0 additions & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use anyhow::{anyhow, bail, Context as _};
use glob::glob;
use itertools::Itertools;
use log::debug;
use toml_edit::easy as toml;
use url::Url;

use crate::core::compiler::Unit;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ fn print_toml(config: &Config, opts: &GetOptions<'_>, key: &ConfigKey, cv: &CV)
drop_println!(
config,
" {}, # {}",
toml_edit::ser::to_item(&val).unwrap(),
serde::Serialize::serialize(val, toml_edit::ser::ValueSerializer::new())
.unwrap(),
def
);
}
Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::io::{BufRead, BufReader, ErrorKind};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{fmt, slice};
use toml_edit::easy as toml;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VersionControl {
Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use cargo_platform::Platform;
use serde::Serialize;
use std::collections::BTreeMap;
use std::path::PathBuf;
use toml_edit::easy as toml;

const VERSION: u32 = 1;

Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::task::Poll;
use anyhow::{bail, format_err, Context as _};
use ops::FilterRule;
use serde::{Deserialize, Serialize};
use toml_edit::easy as toml;

use crate::core::compiler::{DirtyReason, Freshness};
use crate::core::Target;
Expand Down
21 changes: 10 additions & 11 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::util::toml as cargo_toml;
use crate::util::Filesystem;

use anyhow::Context as _;
use toml_edit::easy as toml;

pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
if !ws.root().join("Cargo.lock").exists() {
Expand All @@ -21,7 +20,7 @@ pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
.with_context(|| format!("failed to read file: {}", f.path().display()))?;

let resolve = (|| -> CargoResult<Option<Resolve>> {
let resolve: toml::Value = cargo_toml::parse(&s, f.path(), ws.config())?;
let resolve: toml::Table = cargo_toml::parse_document(&s, f.path(), ws.config())?;
let v: resolver::EncodableResolve = resolve.try_into()?;
Ok(Some(v.into_resolve(&s, ws)?))
})()
Expand Down Expand Up @@ -101,7 +100,7 @@ fn resolve_to_string_orig(
}

fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
let toml = toml_edit::ser::to_item(resolve).unwrap();
let toml = toml::Table::try_from(resolve).unwrap();

let mut out = String::new();

Expand Down Expand Up @@ -140,7 +139,7 @@ fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {

let deps = toml["package"].as_array().unwrap();
for dep in deps {
let dep = dep.as_inline_table().unwrap();
let dep = dep.as_table().unwrap();

out.push_str("[[package]]\n");
emit_package(dep, &mut out);
Expand All @@ -150,7 +149,7 @@ fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
let list = patch["unused"].as_array().unwrap();
for entry in list {
out.push_str("[[patch.unused]]\n");
emit_package(entry.as_inline_table().unwrap(), &mut out);
emit_package(entry.as_table().unwrap(), &mut out);
out.push('\n');
}
}
Expand All @@ -160,11 +159,11 @@ fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
// (which `toml_edit::Table::to_string` only shows)
// 2. We need to ensure all children tables have `metadata.` prefix
let meta_table = meta
.clone()
.into_table()
.expect("validation ensures this is a table");
let mut meta_doc = toml_edit::Document::new();
meta_doc["metadata"] = toml_edit::Item::Table(meta_table);
.as_table()
.expect("validation ensures this is a table")
.clone();
let mut meta_doc = toml::Table::new();
meta_doc.insert("metadata".to_owned(), toml::Value::Table(meta_table));

out.push_str(&meta_doc.to_string());
}
Expand Down Expand Up @@ -200,7 +199,7 @@ fn are_equal_lockfiles(orig: &str, current: &str, ws: &Workspace<'_>) -> bool {
orig.lines().eq(current.lines())
}

fn emit_package(dep: &toml_edit::InlineTable, out: &mut String) {
fn emit_package(dep: &toml::Table, out: &mut String) {
out.push_str(&format!("name = {}\n", &dep["name"]));
out.push_str(&format!("version = {}\n", &dep["version"]));

Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::ffi::OsStr;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use toml_edit::easy as toml;

pub struct VendorOptions<'a> {
pub no_delete: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/config/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ fn escape_key_part<'a>(part: &'a str) -> Cow<'a, str> {
Cow::Borrowed(part)
} else {
// This is a bit messy, but toml doesn't expose a function to do this.
Cow::Owned(toml_edit::Value::from(part).to_string())
Cow::Owned(toml::Value::from(part).to_string())
}
}
53 changes: 24 additions & 29 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ use anyhow::{anyhow, bail, format_err, Context as _};
use cargo_util::paths;
use curl::easy::Easy;
use lazycell::LazyCell;
use serde::de::IntoDeserializer as _;
use serde::Deserialize;
use toml_edit::{easy as toml, Item};
use toml_edit::Item;
use url::Url;

mod de;
Expand Down Expand Up @@ -895,17 +896,11 @@ impl Config {
let def = Definition::Environment(key.as_env_key().to_string());
if self.cli_unstable().advanced_env && env_val.starts_with('[') && env_val.ends_with(']') {
// Parse an environment string as a TOML array.
let toml_s = format!("value={}", env_val);
let toml_v: toml::Value = toml::de::from_str(&toml_s).map_err(|e| {
ConfigError::new(format!("could not parse TOML list: {}", e), def.clone())
})?;
let values = toml_v
.as_table()
.unwrap()
.get("value")
.unwrap()
.as_array()
.expect("env var was not array");
let toml_v = toml::Value::deserialize(toml::de::ValueDeserializer::new(&env_val))
.map_err(|e| {
ConfigError::new(format!("could not parse TOML list: {}", e), def.clone())
})?;
let values = toml_v.as_array().expect("env var was not array");
for value in values {
// TODO: support other types.
let s = value.as_str().ok_or_else(|| {
Expand Down Expand Up @@ -1180,14 +1175,14 @@ impl Config {
}
let contents = fs::read_to_string(path)
.with_context(|| format!("failed to read configuration file `{}`", path.display()))?;
let toml = cargo_toml::parse(&contents, path, self).with_context(|| {
let toml = cargo_toml::parse_document(&contents, path, self).with_context(|| {
format!("could not parse TOML configuration in `{}`", path.display())
})?;
let def = match why_load {
WhyLoad::Cli => Definition::Cli(Some(path.into())),
WhyLoad::FileDiscovery => Definition::Path(path.into()),
};
let value = CV::from_toml(def, toml).with_context(|| {
let value = CV::from_toml(def, toml::Value::Table(toml)).with_context(|| {
format!(
"failed to load TOML configuration from `{}`",
path.display()
Expand Down Expand Up @@ -1302,8 +1297,10 @@ impl Config {
format!("failed to parse value from --config argument `{arg}` as a dotted key expression")
})?;
fn non_empty_decor(d: &toml_edit::Decor) -> bool {
d.prefix().map_or(false, |p| !p.trim().is_empty())
|| d.suffix().map_or(false, |s| !s.trim().is_empty())
d.prefix()
.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
|| d.suffix()
.map_or(false, |s| !s.as_str().unwrap_or_default().trim().is_empty())
}
let ok = {
let mut got_to_value = false;
Expand Down Expand Up @@ -1363,9 +1360,10 @@ impl Config {
);
}

let toml_v: toml::Value = toml::from_document(doc).with_context(|| {
format!("failed to parse value from --config argument `{arg}`")
})?;
let toml_v: toml::Value = toml::Value::deserialize(doc.into_deserializer())
.with_context(|| {
format!("failed to parse value from --config argument `{arg}`")
})?;

if toml_v
.get("registry")
Expand Down Expand Up @@ -2171,14 +2169,12 @@ pub fn save_credentials(
)
})?;

let mut toml = cargo_toml::parse(&contents, file.path(), cfg)?;
let mut toml = cargo_toml::parse_document(&contents, file.path(), cfg)?;

// Move the old token location to the new one.
if let Some(token) = toml.as_table_mut().unwrap().remove("token") {
if let Some(token) = toml.remove("token") {
let map = HashMap::from([("token".to_string(), token)]);
toml.as_table_mut()
.unwrap()
.insert("registry".into(), map.into());
toml.insert("registry".into(), map.into());
}

if let Some(token) = token {
Expand Down Expand Up @@ -2225,17 +2221,16 @@ pub fn save_credentials(
};

if registry.is_some() {
if let Some(table) = toml.as_table_mut().unwrap().remove("registries") {
if let Some(table) = toml.remove("registries") {
let v = CV::from_toml(path_def, table)?;
value.merge(v, false)?;
}
}
toml.as_table_mut().unwrap().insert(key, value.into_toml());
toml.insert(key, value.into_toml());
} else {
// logout
let table = toml.as_table_mut().unwrap();
if let Some(registry) = registry {
if let Some(registries) = table.get_mut("registries") {
if let Some(registries) = toml.get_mut("registries") {
if let Some(reg) = registries.get_mut(registry) {
let rtable = reg.as_table_mut().ok_or_else(|| {
format_err!("expected `[registries.{}]` to be a table", registry)
Expand All @@ -2245,7 +2240,7 @@ pub fn save_credentials(
rtable.remove("secret-key-subject");
}
}
} else if let Some(registry) = table.get_mut("registry") {
} else if let Some(registry) = toml.get_mut("registry") {
let reg_table = registry
.as_table_mut()
.ok_or_else(|| format_err!("expected `[registry]` to be a table"))?;
Expand Down
1 change: 0 additions & 1 deletion src/cargo/util/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::util::CargoResult;
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
use toml_edit::easy as toml;

/// Config definition of a `[target.'cfg(…)']` table.
///
Expand Down
32 changes: 4 additions & 28 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use lazycell::LazyCell;
use log::{debug, trace};
use semver::{self, VersionReq};
use serde::de;
use serde::de::IntoDeserializer as _;
use serde::ser;
use serde::{Deserialize, Serialize};
use toml_edit::easy as toml;
use url::Url;

use crate::core::compiler::{CompileKind, CompileTarget};
Expand All @@ -36,9 +36,6 @@ use crate::util::{
mod targets;
use self::targets::targets;

pub use toml_edit::de::Error as TomlDeError;
pub use toml_edit::TomlError as TomlEditError;

/// Loads a `Cargo.toml` from a file on disk.
///
/// This could result in a real or virtual manifest being returned.
Expand Down Expand Up @@ -90,21 +87,16 @@ pub fn read_manifest_from_str(
// Provide a helpful error message for a common user error.
if let Some(package) = toml.get("package").or_else(|| toml.get("project")) {
if let Some(feats) = package.get("cargo-features") {
let mut feats = feats.clone();
if let Some(value) = feats.as_value_mut() {
// Only keep formatting inside of the `[]` and not formatting around it
value.decor_mut().clear();
}
bail!(
"cargo-features = {} was found in the wrong location: it \
should be set at the top of Cargo.toml before any tables",
feats.to_string()
feats
);
}
}

let mut unused = BTreeSet::new();
let manifest: TomlManifest = serde_ignored::deserialize(toml, |path| {
let manifest: TomlManifest = serde_ignored::deserialize(toml.into_deserializer(), |path| {
let mut key = String::new();
stringify(&mut key, &path);
unused.insert(key);
Expand Down Expand Up @@ -178,23 +170,7 @@ pub fn read_manifest_from_str(
}
}

/// Attempts to parse a string into a [`toml::Value`]. This is not specific to any
/// particular kind of TOML file.
///
/// The purpose of this wrapper is to detect invalid TOML which was previously
/// accepted and display a warning to the user in that case. The `file` and `config`
/// parameters are only used by this fallback path.
pub fn parse(toml: &str, _file: &Path, _config: &Config) -> CargoResult<toml::Value> {
// At the moment, no compatibility checks are needed.
toml.parse()
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))
}

pub fn parse_document(
toml: &str,
_file: &Path,
_config: &Config,
) -> CargoResult<toml_edit::Document> {
pub fn parse_document(toml: &str, _file: &Path, _config: &Config) -> CargoResult<toml::Table> {
// At the moment, no compatibility checks are needed.
toml.parse()
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))
Expand Down
Loading

0 comments on commit 48a61a3

Please sign in to comment.