rust/treefile: Use HashMap to collect extra fields

Instead of the `Strict` and `Permissive` variants of
`TreeComposeConfig`, just collapse all the excess fields in a new
`extra` member and check for any keys there in the strict YAML path.
This will also allow us to drop the hardcoded list of architectures in
the next patch.

Closes: #1749
Approved by: cgwalters
This commit is contained in:
Jonathan Lebon 2019-03-01 12:49:45 -05:00 committed by Atomic Bot
parent 90790cabaf
commit 24335bdf1a

View File

@ -63,6 +63,7 @@ struct ConfigAndExternals {
externals: TreefileExternals,
}
#[derive(PartialEq)]
enum InputFormat {
YAML,
JSON,
@ -77,22 +78,22 @@ fn treefile_parse_stream<R: io::Read>(
) -> Fallible<TreeComposeConfig> {
let mut treefile: TreeComposeConfig = match fmt {
InputFormat::YAML => {
let tf: StrictTreeComposeConfig = serde_yaml::from_reader(input).map_err(|e| {
let tf: TreeComposeConfig = serde_yaml::from_reader(input).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("serde-yaml: {}", e.to_string()),
)
})?;
tf.config
tf
}
InputFormat::JSON => {
let tf: PermissiveTreeComposeConfig = serde_json::from_reader(input).map_err(|e| {
let tf: TreeComposeConfig = serde_json::from_reader(input).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("serde-json: {}", e.to_string()),
)
})?;
tf.config
tf
}
};
@ -113,6 +114,14 @@ fn treefile_parse_stream<R: io::Read>(
(_, None) => None,
};
if fmt == InputFormat::YAML && !treefile.extra.is_empty() {
let keys: Vec<&str> = treefile.extra.keys().map(|k| k.as_str()).collect();
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Unknown fields: {}", keys.join(", ")),
).into());
}
// Substitute ${basearch}
treefile.treeref = match (basearch, treefile.treeref.take()) {
(Some(basearch), Some(treeref)) => {
@ -637,19 +646,9 @@ struct TreeComposeConfig {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "remove-from-packages")]
remove_from_packages: Option<Vec<Vec<String>>>,
}
#[derive(Serialize, Deserialize, Debug)]
struct PermissiveTreeComposeConfig {
#[serde(flatten)]
config: TreeComposeConfig,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct StrictTreeComposeConfig {
#[serde(flatten)]
config: TreeComposeConfig,
extra: HashMap<String, serde_json::Value>,
}
#[cfg(test)]