Skip to content

Commit 48a61a3

Browse files
committed
chore: Update to toml v0.6, toml_edit v0.18
`toml` replaces `toml_edit::easy`, using `toml_edit` as its parser.
1 parent 2286d5f commit 48a61a3

33 files changed

+150
-182
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ tar = { version = "0.4.38", default-features = false }
6666
tempfile = "3.0"
6767
termcolor = "1.1"
6868
time = { version = "0.3", features = ["parsing", "formatting"]}
69-
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
69+
toml_edit = "0.19.0"
70+
toml = "0.7.0"
7071
unicode-xid = "0.2.0"
7172
url = "2.2.2"
7273
walkdir = "2.2"

benches/capture/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ description = "Tool for capturing a real-world workspace for benchmarking."
99
cargo_metadata = "0.14.0"
1010
flate2 = { version = "1.0.3", default-features = false, features = ["zlib"] }
1111
tar = { version = "0.4.38", default-features = false }
12-
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
12+
toml = "0.7.0"

benches/capture/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use flate2::{Compression, GzBuilder};
88
use std::fs;
99
use std::path::{Path, PathBuf};
1010
use std::process::Command;
11-
use toml_edit::easy as toml;
1211

1312
fn main() {
1413
let force = std::env::args().any(|arg| arg == "-f");

crates/cargo-test-support/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ serde = { version = "1.0.123", features = ["derive"] }
2525
serde_json = "1.0"
2626
tar = { version = "0.4.38", default-features = false }
2727
termcolor = "1.1.2"
28-
toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
28+
toml = "0.7.0"
2929
url = "2.2.2"
3030

3131
[target.'cfg(windows)'.dependencies]

crates/cargo-test-support/src/registry.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1391,10 +1391,13 @@ impl Package {
13911391
let mut manifest = String::new();
13921392

13931393
if !self.cargo_features.is_empty() {
1394-
manifest.push_str(&format!(
1395-
"cargo-features = {}\n\n",
1396-
toml_edit::ser::to_item(&self.cargo_features).unwrap()
1397-
));
1394+
let mut features = String::new();
1395+
serde::Serialize::serialize(
1396+
&self.cargo_features,
1397+
toml::ser::ValueSerializer::new(&mut features),
1398+
)
1399+
.unwrap();
1400+
manifest.push_str(&format!("cargo-features = {}\n\n", features));
13981401
}
13991402

14001403
manifest.push_str(&format!(

src/cargo/core/manifest.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use anyhow::Context as _;
99
use semver::Version;
1010
use serde::ser;
1111
use serde::Serialize;
12-
use toml_edit::easy as toml;
1312
use url::Url;
1413

1514
use crate::core::compiler::rustdoc::RustdocScrapeExamples;

src/cargo/core/package.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use lazycell::LazyCell;
1616
use log::{debug, warn};
1717
use semver::Version;
1818
use serde::Serialize;
19-
use toml_edit::easy as toml;
2019

2120
use crate::core::compiler::{CompileKind, RustcTargetData};
2221
use crate::core::dependency::DepKind;

src/cargo/core/workspace.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use anyhow::{anyhow, bail, Context as _};
88
use glob::glob;
99
use itertools::Itertools;
1010
use log::debug;
11-
use toml_edit::easy as toml;
1211
use url::Url;
1312

1413
use crate::core::compiler::Unit;

src/cargo/ops/cargo_config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ fn print_toml(config: &Config, opts: &GetOptions<'_>, key: &ConfigKey, cv: &CV)
137137
drop_println!(
138138
config,
139139
" {}, # {}",
140-
toml_edit::ser::to_item(&val).unwrap(),
140+
serde::Serialize::serialize(val, toml_edit::ser::ValueSerializer::new())
141+
.unwrap(),
141142
def
142143
);
143144
}

src/cargo/ops/cargo_new.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::io::{BufRead, BufReader, ErrorKind};
1212
use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
1414
use std::{fmt, slice};
15-
use toml_edit::easy as toml;
1615

1716
#[derive(Clone, Copy, Debug, PartialEq)]
1817
pub enum VersionControl {

src/cargo/ops/cargo_output_metadata.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use cargo_platform::Platform;
1111
use serde::Serialize;
1212
use std::collections::BTreeMap;
1313
use std::path::PathBuf;
14-
use toml_edit::easy as toml;
1514

1615
const VERSION: u32 = 1;
1716

src/cargo/ops/common_for_install_and_uninstall.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::task::Poll;
99
use anyhow::{bail, format_err, Context as _};
1010
use ops::FilterRule;
1111
use serde::{Deserialize, Serialize};
12-
use toml_edit::easy as toml;
1312

1413
use crate::core::compiler::{DirtyReason, Freshness};
1514
use crate::core::Target;

src/cargo/ops/lockfile.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::util::toml as cargo_toml;
66
use crate::util::Filesystem;
77

88
use anyhow::Context as _;
9-
use toml_edit::easy as toml;
109

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

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

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

106105
let mut out = String::new();
107106

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

141140
let deps = toml["package"].as_array().unwrap();
142141
for dep in deps {
143-
let dep = dep.as_inline_table().unwrap();
142+
let dep = dep.as_table().unwrap();
144143

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

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

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

src/cargo/ops/vendor.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::ffi::OsStr;
1414
use std::fs::{self, File, OpenOptions};
1515
use std::io::{Read, Write};
1616
use std::path::{Path, PathBuf};
17-
use toml_edit::easy as toml;
1817

1918
pub struct VendorOptions<'a> {
2019
pub no_delete: bool,

src/cargo/util/config/key.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ fn escape_key_part<'a>(part: &'a str) -> Cow<'a, str> {
111111
Cow::Borrowed(part)
112112
} else {
113113
// This is a bit messy, but toml doesn't expose a function to do this.
114-
Cow::Owned(toml_edit::Value::from(part).to_string())
114+
Cow::Owned(toml::Value::from(part).to_string())
115115
}
116116
}

src/cargo/util/config/mod.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ use anyhow::{anyhow, bail, format_err, Context as _};
8080
use cargo_util::paths;
8181
use curl::easy::Easy;
8282
use lazycell::LazyCell;
83+
use serde::de::IntoDeserializer as _;
8384
use serde::Deserialize;
84-
use toml_edit::{easy as toml, Item};
85+
use toml_edit::Item;
8586
use url::Url;
8687

8788
mod de;
@@ -895,17 +896,11 @@ impl Config {
895896
let def = Definition::Environment(key.as_env_key().to_string());
896897
if self.cli_unstable().advanced_env && env_val.starts_with('[') && env_val.ends_with(']') {
897898
// Parse an environment string as a TOML array.
898-
let toml_s = format!("value={}", env_val);
899-
let toml_v: toml::Value = toml::de::from_str(&toml_s).map_err(|e| {
900-
ConfigError::new(format!("could not parse TOML list: {}", e), def.clone())
901-
})?;
902-
let values = toml_v
903-
.as_table()
904-
.unwrap()
905-
.get("value")
906-
.unwrap()
907-
.as_array()
908-
.expect("env var was not array");
899+
let toml_v = toml::Value::deserialize(toml::de::ValueDeserializer::new(&env_val))
900+
.map_err(|e| {
901+
ConfigError::new(format!("could not parse TOML list: {}", e), def.clone())
902+
})?;
903+
let values = toml_v.as_array().expect("env var was not array");
909904
for value in values {
910905
// TODO: support other types.
911906
let s = value.as_str().ok_or_else(|| {
@@ -1180,14 +1175,14 @@ impl Config {
11801175
}
11811176
let contents = fs::read_to_string(path)
11821177
.with_context(|| format!("failed to read configuration file `{}`", path.display()))?;
1183-
let toml = cargo_toml::parse(&contents, path, self).with_context(|| {
1178+
let toml = cargo_toml::parse_document(&contents, path, self).with_context(|| {
11841179
format!("could not parse TOML configuration in `{}`", path.display())
11851180
})?;
11861181
let def = match why_load {
11871182
WhyLoad::Cli => Definition::Cli(Some(path.into())),
11881183
WhyLoad::FileDiscovery => Definition::Path(path.into()),
11891184
};
1190-
let value = CV::from_toml(def, toml).with_context(|| {
1185+
let value = CV::from_toml(def, toml::Value::Table(toml)).with_context(|| {
11911186
format!(
11921187
"failed to load TOML configuration from `{}`",
11931188
path.display()
@@ -1302,8 +1297,10 @@ impl Config {
13021297
format!("failed to parse value from --config argument `{arg}` as a dotted key expression")
13031298
})?;
13041299
fn non_empty_decor(d: &toml_edit::Decor) -> bool {
1305-
d.prefix().map_or(false, |p| !p.trim().is_empty())
1306-
|| d.suffix().map_or(false, |s| !s.trim().is_empty())
1300+
d.prefix()
1301+
.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
1302+
|| d.suffix()
1303+
.map_or(false, |s| !s.as_str().unwrap_or_default().trim().is_empty())
13071304
}
13081305
let ok = {
13091306
let mut got_to_value = false;
@@ -1363,9 +1360,10 @@ impl Config {
13631360
);
13641361
}
13651362

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

13701368
if toml_v
13711369
.get("registry")
@@ -2171,14 +2169,12 @@ pub fn save_credentials(
21712169
)
21722170
})?;
21732171

2174-
let mut toml = cargo_toml::parse(&contents, file.path(), cfg)?;
2172+
let mut toml = cargo_toml::parse_document(&contents, file.path(), cfg)?;
21752173

21762174
// Move the old token location to the new one.
2177-
if let Some(token) = toml.as_table_mut().unwrap().remove("token") {
2175+
if let Some(token) = toml.remove("token") {
21782176
let map = HashMap::from([("token".to_string(), token)]);
2179-
toml.as_table_mut()
2180-
.unwrap()
2181-
.insert("registry".into(), map.into());
2177+
toml.insert("registry".into(), map.into());
21822178
}
21832179

21842180
if let Some(token) = token {
@@ -2225,17 +2221,16 @@ pub fn save_credentials(
22252221
};
22262222

22272223
if registry.is_some() {
2228-
if let Some(table) = toml.as_table_mut().unwrap().remove("registries") {
2224+
if let Some(table) = toml.remove("registries") {
22292225
let v = CV::from_toml(path_def, table)?;
22302226
value.merge(v, false)?;
22312227
}
22322228
}
2233-
toml.as_table_mut().unwrap().insert(key, value.into_toml());
2229+
toml.insert(key, value.into_toml());
22342230
} else {
22352231
// logout
2236-
let table = toml.as_table_mut().unwrap();
22372232
if let Some(registry) = registry {
2238-
if let Some(registries) = table.get_mut("registries") {
2233+
if let Some(registries) = toml.get_mut("registries") {
22392234
if let Some(reg) = registries.get_mut(registry) {
22402235
let rtable = reg.as_table_mut().ok_or_else(|| {
22412236
format_err!("expected `[registries.{}]` to be a table", registry)
@@ -2245,7 +2240,7 @@ pub fn save_credentials(
22452240
rtable.remove("secret-key-subject");
22462241
}
22472242
}
2248-
} else if let Some(registry) = table.get_mut("registry") {
2243+
} else if let Some(registry) = toml.get_mut("registry") {
22492244
let reg_table = registry
22502245
.as_table_mut()
22512246
.ok_or_else(|| format_err!("expected `[registry]` to be a table"))?;

src/cargo/util/config/target.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::util::CargoResult;
44
use serde::Deserialize;
55
use std::collections::{BTreeMap, HashMap};
66
use std::path::PathBuf;
7-
use toml_edit::easy as toml;
87

98
/// Config definition of a `[target.'cfg(…)']` table.
109
///

src/cargo/util/toml/mod.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use lazycell::LazyCell;
1313
use log::{debug, trace};
1414
use semver::{self, VersionReq};
1515
use serde::de;
16+
use serde::de::IntoDeserializer as _;
1617
use serde::ser;
1718
use serde::{Deserialize, Serialize};
18-
use toml_edit::easy as toml;
1919
use url::Url;
2020

2121
use crate::core::compiler::{CompileKind, CompileTarget};
@@ -36,9 +36,6 @@ use crate::util::{
3636
mod targets;
3737
use self::targets::targets;
3838

39-
pub use toml_edit::de::Error as TomlDeError;
40-
pub use toml_edit::TomlError as TomlEditError;
41-
4239
/// Loads a `Cargo.toml` from a file on disk.
4340
///
4441
/// This could result in a real or virtual manifest being returned.
@@ -90,21 +87,16 @@ pub fn read_manifest_from_str(
9087
// Provide a helpful error message for a common user error.
9188
if let Some(package) = toml.get("package").or_else(|| toml.get("project")) {
9289
if let Some(feats) = package.get("cargo-features") {
93-
let mut feats = feats.clone();
94-
if let Some(value) = feats.as_value_mut() {
95-
// Only keep formatting inside of the `[]` and not formatting around it
96-
value.decor_mut().clear();
97-
}
9890
bail!(
9991
"cargo-features = {} was found in the wrong location: it \
10092
should be set at the top of Cargo.toml before any tables",
101-
feats.to_string()
93+
feats
10294
);
10395
}
10496
}
10597

10698
let mut unused = BTreeSet::new();
107-
let manifest: TomlManifest = serde_ignored::deserialize(toml, |path| {
99+
let manifest: TomlManifest = serde_ignored::deserialize(toml.into_deserializer(), |path| {
108100
let mut key = String::new();
109101
stringify(&mut key, &path);
110102
unused.insert(key);
@@ -178,23 +170,7 @@ pub fn read_manifest_from_str(
178170
}
179171
}
180172

181-
/// Attempts to parse a string into a [`toml::Value`]. This is not specific to any
182-
/// particular kind of TOML file.
183-
///
184-
/// The purpose of this wrapper is to detect invalid TOML which was previously
185-
/// accepted and display a warning to the user in that case. The `file` and `config`
186-
/// parameters are only used by this fallback path.
187-
pub fn parse(toml: &str, _file: &Path, _config: &Config) -> CargoResult<toml::Value> {
188-
// At the moment, no compatibility checks are needed.
189-
toml.parse()
190-
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))
191-
}
192-
193-
pub fn parse_document(
194-
toml: &str,
195-
_file: &Path,
196-
_config: &Config,
197-
) -> CargoResult<toml_edit::Document> {
173+
pub fn parse_document(toml: &str, _file: &Path, _config: &Config) -> CargoResult<toml::Table> {
198174
// At the moment, no compatibility checks are needed.
199175
toml.parse()
200176
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))

0 commit comments

Comments
 (0)