Skip to content

Commit

Permalink
improved safety of serde_yaml::Value -> value conversion (#328)
Browse files Browse the repository at this point in the history
* improved safety of serde_yaml -> value conversion

* PR to add thiserror to guard

* Revert "PR to add thiserror to guard"

This reverts commit fb57763.
  • Loading branch information
joshfried-aws committed Feb 9, 2023
1 parent 9d761aa commit 71df534
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions guard/src/rules/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,20 @@ impl<'a> TryFrom<&'a serde_yaml::Value> for Value {
}
}
serde_yaml::Value::Bool(b) => Ok(Value::Bool(*b)),
serde_yaml::Value::Sequence(sequence) => {
Ok(Value::List(sequence.iter().fold(vec![], |mut res, val| {
res.push(Value::try_from(val).unwrap());
res
})))
}
serde_yaml::Value::Mapping(mapping) => Ok(Value::Map(mapping.iter().fold(
serde_yaml::Value::Sequence(sequence) => Ok(Value::List(sequence.iter().try_fold(
vec![],
|mut res, val| -> Result<Vec<Self>, Self::Error> {
res.push(Value::try_from(val)?);
Ok(res)
},
)?)),
serde_yaml::Value::Mapping(mapping) => Ok(Value::Map(mapping.iter().try_fold(
IndexMap::with_capacity(mapping.len()),
|mut res, (key, val)| {
res.insert(
key.as_str().unwrap().to_owned(),
Value::try_from(val).unwrap(),
);
res
|mut res, (key, val)| -> Result<IndexMap<String, Self>, Self::Error> {
res.insert(key.as_str().unwrap().to_owned(), Value::try_from(val)?);
Ok(res)
},
))),
)?)),
serde_yaml::Value::Tagged(tag) => Ok(Value::try_from(tag.value.clone())?),
serde_yaml::Value::Null => Ok(Value::Null),
}
Expand Down

0 comments on commit 71df534

Please sign in to comment.