Skip to content

Commit

Permalink
Avoid slicing a string in DocParsingError.
Browse files Browse the repository at this point in the history
Fixes #1339.
  • Loading branch information
waywardmonkeys committed Sep 26, 2022
1 parent 10f10a3 commit 79a93bb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,8 @@ pub enum DocParsingError {
impl DocParsingError {
/// Builds a NotJson DocParsingError
fn invalid_json(invalid_json: &str) -> Self {
let sample_json: String = if invalid_json.len() < 20 {
invalid_json.to_string()
} else {
format!("{:?}...", &invalid_json[0..20])
};
DocParsingError::InvalidJson(sample_json)
let sample = invalid_json.chars().take(20).collect();
DocParsingError::InvalidJson(sample)
}
}

Expand Down Expand Up @@ -793,6 +789,11 @@ mod tests {
))
);
}
{
// Short JSON, under the 20 char take.
let json_err = schema.parse_document(r#"{"count": 50,}"#);
assert_matches!(json_err, Err(InvalidJson(_)));
}
{
let json_err = schema.parse_document(
r#"{
Expand Down

0 comments on commit 79a93bb

Please sign in to comment.