Handle blank input a little more cleanly

This commit is contained in:
John Doty 2024-08-12 09:11:21 -07:00
parent 03de4a4661
commit 4647226ee7

View file

@ -105,8 +105,14 @@ impl JsonValue {
pub fn parse(blob: &[u8]) -> Result<Self> {
Self::parse_impl(blob).with_context(|| {
match std::str::from_utf8(blob) {
Ok(s) => format!("Failed to parse: {s}"),
Err(_) => format!("Failed to parse {blob:?}"),
Ok(s) => format!("Failed to parse {} bytes: '{}'", s.len(), s),
Err(_) => {
format!(
"Failed to parse {} bytes (not utf-8): {:?}",
blob.len(),
blob
)
}
}
})
}
@ -295,10 +301,11 @@ impl JsonValue {
}
}
match stack.pop().expect("underflow somehow") {
Tok::Val(v) => Ok(v),
Tok::StartObject => bail!("unterminated object"),
Tok::StartArray => bail!("unterminated array"),
match stack.pop() {
Some(Tok::Val(v)) => Ok(v),
Some(Tok::StartObject) => bail!("unterminated object"),
Some(Tok::StartArray) => bail!("unterminated array"),
None => bail!("No JSON found in input"),
}
}
@ -501,6 +508,11 @@ mod test {
}
}
#[test]
pub fn json_decode_empty() {
assert!(JsonValue::parse(b" ").is_err());
}
#[test]
pub fn json_decode_docker() {
use pretty_assertions::assert_eq;