Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions guard/src/rules/libyaml/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ impl Loader {
Ok(i) => MarkedValue::Int(i, location),
Err(_) => match val.parse::<f64>() {
Ok(f) => MarkedValue::Float(f, location),
Err(_) => match val.parse::<bool>() {
Ok(b) => MarkedValue::Bool(b, location),
Err(_) => match val.to_lowercase().as_str() {
Err(_) => match self.parse_bool(&val) {
Some(b) => MarkedValue::Bool(b, location),
None => match val.to_lowercase().as_str() {
"~" | "null" => MarkedValue::Null(location),
_ => MarkedValue::String(val, location),
},
Expand All @@ -100,6 +100,23 @@ impl Loader {

self.stack.push(path_value);
}
fn is_bool_true(&self, s: &str) -> bool {
matches!(s, "true" | "yes" | "on" | "y")
}

fn is_bool_false(&self, s: &str) -> bool {
matches!(s, "false" | "no" | "off" | "n")
}

fn parse_bool(&self, val: &str) -> Option<bool> {
if self.is_bool_true(val) {
Some(true)
} else if self.is_bool_false(val) {
Some(false)
} else {
None
}
}

fn handle_sequence_end(&mut self) {
let array_idx = self.last_container_index.pop().unwrap();
Expand Down
42 changes: 42 additions & 0 deletions guard/src/rules/libyaml/loader_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ fn yaml_loader() -> Result<()> {
Ok(())
}

#[rstest::rstest]
#[case::standard_lowercase_true("true", true)]
#[case::standard_capitalized_true("True", true)]
#[case::standard_uppercase_true("TRUE", true)]
#[case::yaml_yes_lowercase("yes", true)]
#[case::yaml_yes_capitalized("Yes", true)]
#[case::yaml_yes_uppercase("YES", true)]
#[case::yaml_on_lowercase("on", true)]
#[case::yaml_on_capitalized("On", true)]
#[case::yaml_on_uppercase("ON", true)]
#[case::yaml_y_lowercase("y", true)]
#[case::yaml_y_uppercase("Y", true)]
#[case::standard_lowercase_false("false", false)]
#[case::standard_capitalized_false("False", false)]
#[case::standard_uppercase_false("FALSE", false)]
#[case::yaml_no_lowercase("no", false)]
#[case::yaml_no_capitalized("No", false)]
#[case::yaml_no_uppercase("NO", false)]
#[case::yaml_off_lowercase("off", false)]
#[case::yaml_off_capitalized("Off", false)]
#[case::yaml_off_uppercase("OFF", false)]
#[case::yaml_n_lowercase("n", false)]
#[case::yaml_n_uppercase("N", false)]
fn test_handle_bool_happy_path(#[case] arg: &str, #[case] expected: bool) -> Result<()> {
let docs = format!("check: {arg}");

let mut loader = Loader::new();
match loader.load(String::from(docs))? {
MarkedValue::Map(map, ..) => {
assert!(map.len() == 1);
let (.., result) = map.first().unwrap();

if let MarkedValue::Bool(result, ..) = *result {
assert_eq!(result, expected);
}
}
_ => unreachable!("this isn't possible"),
}

Ok(())
}

#[test]
fn yaml_loader2() -> Result<()> {
let docs = r###"
Expand Down
Loading