Skip to content

Commit ad8fb2f

Browse files
committed
refactor(manifest): Switch from toml_edit to toml
Technically this will cause a small regression in performance from `toml_edit@0.23` but not `toml_edit@0.22`
1 parent 6e04449 commit ad8fb2f

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

src/cargo/core/manifest.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl EitherManifest {
6363
pub struct Manifest {
6464
// alternate forms of manifests:
6565
contents: Rc<String>,
66-
document: Rc<toml_edit::Document<String>>,
66+
document: Rc<toml::Spanned<toml::de::DeTable<'static>>>,
6767
original_toml: Rc<TomlManifest>,
6868
normalized_toml: Rc<TomlManifest>,
6969
summary: Summary,
@@ -109,7 +109,7 @@ pub struct Warnings(Vec<DelayedWarning>);
109109
pub struct VirtualManifest {
110110
// alternate forms of manifests:
111111
contents: Rc<String>,
112-
document: Rc<toml_edit::Document<String>>,
112+
document: Rc<toml::Spanned<toml::de::DeTable<'static>>>,
113113
original_toml: Rc<TomlManifest>,
114114
normalized_toml: Rc<TomlManifest>,
115115

@@ -496,7 +496,7 @@ compact_debug! {
496496
impl Manifest {
497497
pub fn new(
498498
contents: Rc<String>,
499-
document: Rc<toml_edit::Document<String>>,
499+
document: Rc<toml::Spanned<toml::de::DeTable<'static>>>,
500500
original_toml: Rc<TomlManifest>,
501501
normalized_toml: Rc<TomlManifest>,
502502
summary: Summary,
@@ -565,7 +565,7 @@ impl Manifest {
565565
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
566566
}
567567
/// Collection of spans for the original TOML
568-
pub fn document(&self) -> &toml_edit::Document<String> {
568+
pub fn document(&self) -> &toml::Spanned<toml::de::DeTable<'static>> {
569569
&self.document
570570
}
571571
/// The [`TomlManifest`] as parsed from [`Manifest::document`]
@@ -738,7 +738,7 @@ impl Manifest {
738738
impl VirtualManifest {
739739
pub fn new(
740740
contents: Rc<String>,
741-
document: Rc<toml_edit::Document<String>>,
741+
document: Rc<toml::Spanned<toml::de::DeTable<'static>>>,
742742
original_toml: Rc<TomlManifest>,
743743
normalized_toml: Rc<TomlManifest>,
744744
replace: Vec<(PackageIdSpec, Dependency)>,
@@ -766,7 +766,7 @@ impl VirtualManifest {
766766
self.contents.as_str()
767767
}
768768
/// Collection of spans for the original TOML
769-
pub fn document(&self) -> &toml_edit::Document<String> {
769+
pub fn document(&self) -> &toml::Spanned<toml::de::DeTable<'static>> {
770770
&self.document
771771
}
772772
/// The [`TomlManifest`] as parsed from [`VirtualManifest::document`]

src/cargo/util/lints.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use pathdiff::diff_paths;
66
use std::fmt::Display;
77
use std::ops::Range;
88
use std::path::Path;
9-
use toml_edit::Document;
109

1110
const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
1211
pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNKNOWN_LINTS];
@@ -16,7 +15,7 @@ pub fn analyze_cargo_lints_table(
1615
path: &Path,
1716
pkg_lints: &TomlToolLints,
1817
ws_contents: &str,
19-
ws_document: &Document<String>,
18+
ws_document: &toml::Spanned<toml::de::DeTable<'static>>,
2019
ws_path: &Path,
2120
gctx: &GlobalContext,
2221
) -> CargoResult<()> {
@@ -116,7 +115,7 @@ fn verify_feature_enabled(
116115
manifest: &Manifest,
117116
manifest_path: &str,
118117
ws_contents: &str,
119-
ws_document: &Document<String>,
118+
ws_document: &toml::Spanned<toml::de::DeTable<'static>>,
120119
ws_path: &str,
121120
error_count: &mut usize,
122121
gctx: &GlobalContext,
@@ -191,43 +190,33 @@ fn verify_feature_enabled(
191190
}
192191

193192
pub fn get_span(
194-
document: &Document<String>,
193+
document: &toml::Spanned<toml::de::DeTable<'static>>,
195194
path: &[&str],
196195
get_value: bool,
197196
) -> Option<Range<usize>> {
198-
let mut table = document.as_item().as_table_like()?;
197+
let mut table = document.get_ref();
199198
let mut iter = path.into_iter().peekable();
200199
while let Some(key) = iter.next() {
201-
let (key, item) = table.get_key_value(key)?;
200+
let key_s: &str = key.as_ref();
201+
let (key, item) = table.get_key_value(key_s)?;
202202
if iter.peek().is_none() {
203203
return if get_value {
204-
item.span()
204+
Some(item.span())
205205
} else {
206-
let leaf_decor = key.dotted_decor();
207-
let leaf_prefix_span = leaf_decor.prefix().and_then(|p| p.span());
208-
let leaf_suffix_span = leaf_decor.suffix().and_then(|s| s.span());
209-
if let (Some(leaf_prefix_span), Some(leaf_suffix_span)) =
210-
(leaf_prefix_span, leaf_suffix_span)
211-
{
212-
Some(leaf_prefix_span.start..leaf_suffix_span.end)
213-
} else {
214-
key.span()
215-
}
206+
Some(key.span())
216207
};
217208
}
218-
if item.is_table_like() {
219-
table = item.as_table_like().unwrap();
209+
if let Some(next_table) = item.get_ref().as_table() {
210+
table = next_table;
220211
}
221-
if item.is_array() && iter.peek().is_some() {
222-
let array = item.as_array().unwrap();
223-
let next = iter.next().unwrap();
224-
return array.iter().find_map(|item| {
225-
if next == &item.to_string() {
226-
item.span()
227-
} else {
228-
None
229-
}
230-
});
212+
if iter.peek().is_some() {
213+
if let Some(array) = item.get_ref().as_array() {
214+
let next = iter.next().unwrap();
215+
return array.iter().find_map(|item| match item.get_ref() {
216+
toml::de::DeValue::String(s) if s == next => Some(item.span()),
217+
_ => None,
218+
});
219+
}
231220
}
232221
}
233222
None
@@ -511,7 +500,7 @@ fn output_unknown_lints(
511500
manifest_path: &str,
512501
pkg_lints: &TomlToolLints,
513502
ws_contents: &str,
514-
ws_document: &Document<String>,
503+
ws_document: &toml::Spanned<toml::de::DeTable<'static>>,
515504
ws_path: &str,
516505
error_count: &mut usize,
517506
gctx: &GlobalContext,

src/cargo/util/toml/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use cargo_util_schemas::manifest::{RustVersion, StringOrBool};
1919
use itertools::Itertools;
2020
use lazycell::LazyCell;
2121
use pathdiff::diff_paths;
22-
use toml_edit::Document;
2322
use url::Url;
2423

2524
use crate::core::compiler::{CompileKind, CompileTarget};
@@ -166,16 +165,28 @@ fn read_toml_string(path: &Path, is_embedded: bool, gctx: &GlobalContext) -> Car
166165
}
167166

168167
#[tracing::instrument(skip_all)]
169-
fn parse_document(contents: &str) -> Result<toml_edit::Document<String>, toml_edit::de::Error> {
170-
toml_edit::Document::parse(contents.to_owned()).map_err(Into::into)
168+
fn parse_document(
169+
contents: &str,
170+
) -> Result<toml::Spanned<toml::de::DeTable<'static>>, toml::de::Error> {
171+
let mut table = toml::de::DeTable::parse(contents)?;
172+
table.get_mut().make_owned();
173+
// SAFETY: `DeTable::make_owned` ensures no borrows remain and the lifetime does not affect
174+
// layout
175+
let table = unsafe {
176+
std::mem::transmute::<
177+
toml::Spanned<toml::de::DeTable<'_>>,
178+
toml::Spanned<toml::de::DeTable<'static>>,
179+
>(table)
180+
};
181+
Ok(table)
171182
}
172183

173184
#[tracing::instrument(skip_all)]
174185
fn deserialize_toml(
175-
document: &toml_edit::Document<String>,
176-
) -> Result<manifest::TomlManifest, toml_edit::de::Error> {
186+
document: &toml::Spanned<toml::de::DeTable<'static>>,
187+
) -> Result<manifest::TomlManifest, toml::de::Error> {
177188
let mut unused = BTreeSet::new();
178-
let deserializer = toml_edit::de::Deserializer::from(document.clone());
189+
let deserializer = toml::de::Deserializer::from(document.clone());
179190
let mut document: manifest::TomlManifest = serde_ignored::deserialize(deserializer, |path| {
180191
let mut key = String::new();
181192
stringify(&mut key, &path);
@@ -1256,7 +1267,7 @@ fn deprecated_ws_default_features(
12561267
#[tracing::instrument(skip_all)]
12571268
pub fn to_real_manifest(
12581269
contents: String,
1259-
document: toml_edit::Document<String>,
1270+
document: toml::Spanned<toml::de::DeTable<'static>>,
12601271
original_toml: manifest::TomlManifest,
12611272
normalized_toml: manifest::TomlManifest,
12621273
features: Features,
@@ -1843,7 +1854,7 @@ pub fn to_real_manifest(
18431854
fn missing_dep_diagnostic(
18441855
missing_dep: &MissingDependencyError,
18451856
orig_toml: &TomlManifest,
1846-
document: &Document<String>,
1857+
document: &toml::Spanned<toml::de::DeTable<'static>>,
18471858
contents: &str,
18481859
manifest_file: &Path,
18491860
gctx: &GlobalContext,
@@ -1921,7 +1932,7 @@ fn missing_dep_diagnostic(
19211932

19221933
fn to_virtual_manifest(
19231934
contents: String,
1924-
document: toml_edit::Document<String>,
1935+
document: toml::Spanned<toml::de::DeTable<'static>>,
19251936
original_toml: manifest::TomlManifest,
19261937
normalized_toml: manifest::TomlManifest,
19271938
features: Features,
@@ -2761,7 +2772,7 @@ fn lints_to_rustflags(lints: &manifest::TomlLints) -> CargoResult<Vec<String>> {
27612772
}
27622773

27632774
fn emit_diagnostic(
2764-
e: toml_edit::de::Error,
2775+
e: toml::de::Error,
27652776
contents: &str,
27662777
manifest_file: &Path,
27672778
gctx: &GlobalContext,

0 commit comments

Comments
 (0)