Skip to content

Commit

Permalink
doc: fix panic in SerPolicy
Browse files Browse the repository at this point in the history
The `SerPolicy` was panicking due to attempting to split a string in the middle
of a utf8 character. Updates the policy to instead truncate strings at the
nearest character boundary.
  • Loading branch information
psFried committed Nov 10, 2023
1 parent 5f7ea6c commit 191c5d9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/doc/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ impl<'p, N: AsNode> serde::Serialize for SerNode<'p, N> {
})),
Node::String(mut s) => {
if s.len() > self.policy.str_truncate_after {
s = &s[..self.policy.str_truncate_after];
// Find the greatest index that is <= `str_truncate_after` and falls at a utf8
// character boundary
let mut truncate_at = self.policy.str_truncate_after;
while !s.is_char_boundary(truncate_at) {
truncate_at -= 1;
}
s = &s[..truncate_at];
}
serializer.serialize_str(s)
}
Expand Down

0 comments on commit 191c5d9

Please sign in to comment.