@@ -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
613617impl 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