Skip to content

Commit 87c6b54

Browse files
committed
Feature gate custom error messages
1 parent 115dcb1 commit 87c6b54

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ version = "1.0.80"
2222
serde_derive = "1.0.80"
2323

2424
[features]
25+
custom-error-messages = []
2526
std = ["serde/std"]
2627

2728
[badges]

src/de/mod.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ pub enum Error {
6868
TrailingComma,
6969

7070
/// Error with a custom message that we had to discard.
71-
CustomError(heapless::String<heapless::consts::U64>),
71+
CustomError,
72+
73+
/// Error with a custom message that was preserved.
74+
#[cfg(feature = "custom-error-messages")]
75+
CustomErrorWithMessage(heapless::String<heapless::consts::U64>),
7276

7377
#[doc(hidden)]
7478
__Extensible,
@@ -611,17 +615,24 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
611615
}
612616

613617
impl de::Error for Error {
618+
#[cfg_attr(not(feature = "custom-error-messages"), allow(unused_variables))]
614619
fn custom<T>(msg: T) -> Self
615620
where
616621
T: fmt::Display,
617622
{
618-
use core::fmt::Write;
619-
620-
let mut string = heapless::String::new();
621-
// Intentionally discard the result here, which ignores overflow and lets us keep the first
622-
// N error message characters
623-
let _ = write!(string, "{}", msg);
624-
Error::CustomError(string)
623+
#[cfg(not(feature = "custom-error-messages"))]
624+
{
625+
Error::CustomError
626+
}
627+
#[cfg(feature = "custom-error-messages")]
628+
{
629+
use core::fmt::Write;
630+
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);
634+
Error::CustomErrorWithMessage(string)
635+
}
625636
}
626637
}
627638

@@ -661,7 +672,9 @@ impl fmt::Display for Error {
661672
value."
662673
}
663674
Error::TrailingComma => "JSON has a comma after the last value in an array or map.",
664-
Error::CustomError(msg) => msg.as_str(),
675+
Error::CustomError => "JSON does not match deserializer’s expected format.",
676+
#[cfg(feature = "custom-error-messages")]
677+
Error::CustomErrorWithMessage(msg) => msg.as_str(),
665678
_ => "Invalid JSON",
666679
}
667680
)
@@ -870,6 +883,27 @@ mod tests {
870883
}
871884

872885
#[test]
886+
#[cfg(not(feature = "custom-error-messages"))]
887+
fn struct_tuple() {
888+
#[derive(Debug, Deserialize, PartialEq)]
889+
struct Xy(i8, i8);
890+
891+
assert_eq!(crate::from_str(r#"[10, 20]"#), Ok(Xy(10, 20)));
892+
assert_eq!(crate::from_str(r#"[10, -20]"#), Ok(Xy(10, -20)));
893+
894+
// wrong number of args
895+
assert_eq!(
896+
crate::from_str::<Xy>(r#"[10]"#),
897+
Err(crate::de::Error::CustomError)
898+
);
899+
assert_eq!(
900+
crate::from_str::<Xy>(r#"[10, 20, 30]"#),
901+
Err(crate::de::Error::TrailingCharacters)
902+
);
903+
}
904+
905+
#[test]
906+
#[cfg(feature = "custom-error-messages")]
873907
fn struct_tuple() {
874908
#[derive(Debug, Deserialize, PartialEq)]
875909
struct Xy(i8, i8);
@@ -880,7 +914,7 @@ mod tests {
880914
// wrong number of args
881915
assert_eq!(
882916
crate::from_str::<Xy>(r#"[10]"#),
883-
Err(crate::de::Error::CustomError(
917+
Err(crate::de::Error::CustomErrorWithMessage(
884918
"invalid length 1, expected tuple struct Xy with 2 elements".into()
885919
))
886920
);

0 commit comments

Comments
 (0)