Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Add some null and boolean representations from YAML 1.2 #330

Merged
merged 2 commits into from Sep 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/de.rs
Expand Up @@ -923,20 +923,17 @@ fn parse_borrowed_str<'de>(
}

fn parse_null(scalar: &[u8]) -> Option<()> {
if scalar == b"~" || scalar == b"null" {
Some(())
} else {
None
match scalar {
b"null" | b"Null" | b"NULL" | b"~" => Some(()),
_ => None,
}
}

fn parse_bool(scalar: &str) -> Option<bool> {
if scalar == "true" {
Some(true)
} else if scalar == "false" {
Some(false)
} else {
None
match scalar {
"true" | "True" | "TRUE" => Some(true),
"false" | "False" | "FALSE" => Some(false),
_ => None,
}
}

Expand Down
66 changes: 66 additions & 0 deletions tests/test_de.rs
Expand Up @@ -594,3 +594,69 @@ fn test_python_safe_dump() {
let expected = Frob { foo: 7200 };
test_de(yaml, &expected);
}

#[test]
fn test_tag_resolution() {
// https://yaml.org/spec/1.2.2/#1032-tag-resolution
let yaml = indoc! {"
- null
- Null
- NULL
- ~
-
- true
- True
- TRUE
- false
- False
- FALSE
- y
- Y
- yes
- Yes
- YES
- n
- N
- no
- No
- NO
- on
- On
- ON
- off
- Off
- OFF
"};

let expected = vec![
Value::Null,
Value::Null,
Value::Null,
Value::Null,
Value::Null,
Value::Bool(true),
Value::Bool(true),
Value::Bool(true),
Value::Bool(false),
Value::Bool(false),
Value::Bool(false),
Value::String("y".to_owned()),
Value::String("Y".to_owned()),
Value::String("yes".to_owned()),
Value::String("Yes".to_owned()),
Value::String("YES".to_owned()),
Value::String("n".to_owned()),
Value::String("N".to_owned()),
Value::String("no".to_owned()),
Value::String("No".to_owned()),
Value::String("NO".to_owned()),
Value::String("on".to_owned()),
Value::String("On".to_owned()),
Value::String("ON".to_owned()),
Value::String("off".to_owned()),
Value::String("Off".to_owned()),
Value::String("OFF".to_owned()),
];

test_de(yaml, &expected);
}
2 changes: 1 addition & 1 deletion tests/test_value.rs
Expand Up @@ -97,7 +97,7 @@ fn test_merge() {
#[test]
fn test_debug() {
let yaml = indoc! {"
Null: ~
'Null': ~
Bool: true
Number: 1
String: ...
Expand Down