diff --git a/crates/jrsonnet-evaluator/src/builtin/manifest.rs b/crates/jrsonnet-evaluator/src/builtin/manifest.rs index a5a6827d..47dfae3b 100644 --- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs +++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs @@ -189,40 +189,19 @@ fn yaml_needs_quotes(string: &str) -> bool { string.starts_with(' ') || string.ends_with(' ') } - string == "" + string.is_empty() || need_quotes_spaces(string) - || string.starts_with(|character: char| match character { - '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@' => true, - _ => false, - }) || string.contains(|character: char| match character { - ':' - | '{' - | '}' - | '[' - | ']' - | ',' - | '#' - | '`' - | '\"' - | '\'' - | '\\' - | '\0'..='\x06' - | '\t' - | '\n' - | '\r' - | '\x0e'..='\x1a' - | '\x1c'..='\x1f' => true, - _ => false, - }) || [ - // http://yaml.org/type/bool.html - // Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse - // them as string, not booleans, although it is violating the YAML 1.1 specification. - // See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088. - "yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false", - "on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html - "null", "Null", "NULL", "~", - ] - .contains(&string) + || string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@')) + || string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f')) + || [ + // http://yaml.org/type/bool.html + // Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse + // them as string, not booleans, although it is violating the YAML 1.1 specification. + // See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088. + "yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false", + "on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html + "null", "Null", "NULL", "~", + ].contains(&string) || (string.chars().all(|c| matches!(c, '0'..='9' | '-')) && string.chars().filter(|c| *c == '-').count() == 2) || string.starts_with('.') @@ -262,8 +241,8 @@ fn manifest_yaml_ex_buf( buf.push_str(options.padding); buf.push_str(line); } - } else if !options.quote_keys && !yaml_needs_quotes(&s) { - buf.push_str(&s); + } else if !options.quote_keys && !yaml_needs_quotes(s) { + buf.push_str(s); } else { escape_string_json_buf(s, buf); } @@ -311,8 +290,8 @@ fn manifest_yaml_ex_buf( buf.push('\n'); buf.push_str(cur_padding); } - if !options.quote_keys && !yaml_needs_quotes(&key) { - buf.push_str(&key); + if !options.quote_keys && !yaml_needs_quotes(key) { + buf.push_str(key); } else { escape_string_json_buf(key, buf); }