Skip to content

Commit 3ee36bc

Browse files
committed
Use more elegant truncating method and unit test
1 parent 87c6b54 commit 3ee36bc

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/de/mod.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,9 @@ impl de::Error for Error {
627627
#[cfg(feature = "custom-error-messages")]
628628
{
629629
use core::fmt::Write;
630+
630631
let mut string = heapless::String::new();
631-
// Intentionally discard the result here, which ignores overflow and lets us keep the first
632-
// N error message characters
633-
let _ = write!(string, "{}", msg);
632+
write!(string, "{:.64}", msg).unwrap();
634633
Error::CustomErrorWithMessage(string)
635634
}
636635
}
@@ -976,6 +975,30 @@ mod tests {
976975
);
977976
}
978977

978+
#[test]
979+
#[cfg(feature = "custom-error-messages")]
980+
fn preserve_short_error_message() {
981+
use serde::de::Error;
982+
assert_eq!(
983+
crate::de::Error::custom("something bad happened"),
984+
crate::de::Error::CustomErrorWithMessage(
985+
"something bad happened".into()
986+
)
987+
);
988+
}
989+
990+
#[test]
991+
#[cfg(feature = "custom-error-messages")]
992+
fn truncate_error_message() {
993+
use serde::de::Error;
994+
assert_eq!(
995+
crate::de::Error::custom("0123456789012345678901234567890123456789012345678901234567890123 <- after here the message should be truncated"),
996+
crate::de::Error::CustomErrorWithMessage(
997+
"0123456789012345678901234567890123456789012345678901234567890123".into()
998+
)
999+
);
1000+
}
1001+
9791002
// See https://iot.mozilla.org/wot/#thing-resource
9801003
#[test]
9811004
#[ignore]

0 commit comments

Comments
 (0)