mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
refactor(gctx): extract toml dotted keys validation
This commit is contained in:
parent
24ef070a95
commit
49c92411a6
@ -1463,85 +1463,7 @@ impl GlobalContext {
|
||||
self._load_file(&self.cwd().join(&str_path), &mut seen, true, WhyLoad::Cli)
|
||||
.with_context(|| format!("failed to load config from `{}`", str_path))?
|
||||
} else {
|
||||
// We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys)
|
||||
// expressions followed by a value that's not an "inline table"
|
||||
// (https://toml.io/en/v1.0.0#inline-table). Easiest way to check for that is to
|
||||
// parse the value as a toml_edit::DocumentMut, and check that the (single)
|
||||
// inner-most table is set via dotted keys.
|
||||
let doc: toml_edit::DocumentMut = arg.parse().with_context(|| {
|
||||
format!("failed to parse value from --config argument `{arg}` as a dotted key expression")
|
||||
})?;
|
||||
fn non_empty(d: Option<&toml_edit::RawString>) -> bool {
|
||||
d.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
|
||||
}
|
||||
fn non_empty_decor(d: &toml_edit::Decor) -> bool {
|
||||
non_empty(d.prefix()) || non_empty(d.suffix())
|
||||
}
|
||||
fn non_empty_key_decor(k: &toml_edit::Key) -> bool {
|
||||
non_empty_decor(k.leaf_decor()) || non_empty_decor(k.dotted_decor())
|
||||
}
|
||||
let ok = {
|
||||
let mut got_to_value = false;
|
||||
let mut table = doc.as_table();
|
||||
let mut is_root = true;
|
||||
while table.is_dotted() || is_root {
|
||||
is_root = false;
|
||||
if table.len() != 1 {
|
||||
break;
|
||||
}
|
||||
let (k, n) = table.iter().next().expect("len() == 1 above");
|
||||
match n {
|
||||
Item::Table(nt) => {
|
||||
if table.key(k).map_or(false, non_empty_key_decor)
|
||||
|| non_empty_decor(nt.decor())
|
||||
{
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
includes non-whitespace decoration"
|
||||
)
|
||||
}
|
||||
table = nt;
|
||||
}
|
||||
Item::Value(v) if v.is_inline_table() => {
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
sets a value to an inline table, which is not accepted"
|
||||
);
|
||||
}
|
||||
Item::Value(v) => {
|
||||
if table
|
||||
.key(k)
|
||||
.map_or(false, |k| non_empty(k.leaf_decor().prefix()))
|
||||
|| non_empty_decor(v.decor())
|
||||
{
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
includes non-whitespace decoration"
|
||||
)
|
||||
}
|
||||
got_to_value = true;
|
||||
break;
|
||||
}
|
||||
Item::ArrayOfTables(_) => {
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
sets a value to an array of tables, which is not accepted"
|
||||
);
|
||||
}
|
||||
|
||||
Item::None => {
|
||||
bail!("--config argument `{arg}` doesn't provide a value")
|
||||
}
|
||||
}
|
||||
}
|
||||
got_to_value
|
||||
};
|
||||
if !ok {
|
||||
bail!(
|
||||
"--config argument `{arg}` was not a TOML dotted key expression (such as `build.jobs = 2`)"
|
||||
);
|
||||
}
|
||||
|
||||
let doc = toml_dotted_keys(arg)?;
|
||||
let toml_v: toml::Value = toml::Value::deserialize(doc.into_deserializer())
|
||||
.with_context(|| {
|
||||
format!("failed to parse value from --config argument `{arg}`")
|
||||
@ -3046,6 +2968,88 @@ fn parse_document(toml: &str, _file: &Path, _gctx: &GlobalContext) -> CargoResul
|
||||
toml.parse().map_err(Into::into)
|
||||
}
|
||||
|
||||
fn toml_dotted_keys(arg: &str) -> CargoResult<toml_edit::DocumentMut> {
|
||||
// We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys)
|
||||
// expressions followed by a value that's not an "inline table"
|
||||
// (https://toml.io/en/v1.0.0#inline-table). Easiest way to check for that is to
|
||||
// parse the value as a toml_edit::DocumentMut, and check that the (single)
|
||||
// inner-most table is set via dotted keys.
|
||||
let doc: toml_edit::DocumentMut = arg.parse().with_context(|| {
|
||||
format!("failed to parse value from --config argument `{arg}` as a dotted key expression")
|
||||
})?;
|
||||
fn non_empty(d: Option<&toml_edit::RawString>) -> bool {
|
||||
d.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
|
||||
}
|
||||
fn non_empty_decor(d: &toml_edit::Decor) -> bool {
|
||||
non_empty(d.prefix()) || non_empty(d.suffix())
|
||||
}
|
||||
fn non_empty_key_decor(k: &toml_edit::Key) -> bool {
|
||||
non_empty_decor(k.leaf_decor()) || non_empty_decor(k.dotted_decor())
|
||||
}
|
||||
let ok = {
|
||||
let mut got_to_value = false;
|
||||
let mut table = doc.as_table();
|
||||
let mut is_root = true;
|
||||
while table.is_dotted() || is_root {
|
||||
is_root = false;
|
||||
if table.len() != 1 {
|
||||
break;
|
||||
}
|
||||
let (k, n) = table.iter().next().expect("len() == 1 above");
|
||||
match n {
|
||||
Item::Table(nt) => {
|
||||
if table.key(k).map_or(false, non_empty_key_decor)
|
||||
|| non_empty_decor(nt.decor())
|
||||
{
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
includes non-whitespace decoration"
|
||||
)
|
||||
}
|
||||
table = nt;
|
||||
}
|
||||
Item::Value(v) if v.is_inline_table() => {
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
sets a value to an inline table, which is not accepted"
|
||||
);
|
||||
}
|
||||
Item::Value(v) => {
|
||||
if table
|
||||
.key(k)
|
||||
.map_or(false, |k| non_empty(k.leaf_decor().prefix()))
|
||||
|| non_empty_decor(v.decor())
|
||||
{
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
includes non-whitespace decoration"
|
||||
)
|
||||
}
|
||||
got_to_value = true;
|
||||
break;
|
||||
}
|
||||
Item::ArrayOfTables(_) => {
|
||||
bail!(
|
||||
"--config argument `{arg}` \
|
||||
sets a value to an array of tables, which is not accepted"
|
||||
);
|
||||
}
|
||||
|
||||
Item::None => {
|
||||
bail!("--config argument `{arg}` doesn't provide a value")
|
||||
}
|
||||
}
|
||||
}
|
||||
got_to_value
|
||||
};
|
||||
if !ok {
|
||||
bail!(
|
||||
"--config argument `{arg}` was not a TOML dotted key expression (such as `build.jobs = 2`)"
|
||||
);
|
||||
}
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
/// A type to deserialize a list of strings from a toml file.
|
||||
///
|
||||
/// Supports deserializing either a whitespace-separated list of arguments in a
|
||||
|
Loading…
x
Reference in New Issue
Block a user