diff --git a/src/lib.rs b/src/lib.rs index ba53312..6c987c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,7 +136,7 @@ mod ser; pub use crate::de::{deserialize, Deserializer}; pub use crate::error::{Error, Result}; -pub use crate::ser::{serialize, Serialize, Serializer}; +pub use crate::ser::{convert_ser_error, serialize, Serialize, Serializer}; // Not public API. #[doc(hidden)] diff --git a/src/ser.rs b/src/ser.rs index 5507a30..234d094 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -287,6 +287,15 @@ mod erase { _ => unreachable!(), } } + + /// Takes the stored result of the erased serializer after serializing. + pub fn result(self) -> Result { + match self { + Serializer::Complete(ok) => Ok(ok), + Serializer::Error(err) => Err(err), + _ => panic!("Tried to take result of serializer before serializing or finished serializing an object.") + } + } } } @@ -688,6 +697,16 @@ where } } +/// Converts the result from a Serializer::erase_xxx function into a public-api result. +pub fn convert_ser_error(err: ErrorImpl) -> crate::Error { + match err { + ErrorImpl::ShortCircuit => { + serde::ser::Error::custom("Call `serializer::result()` to find out the error.") + } + ErrorImpl::Custom(msg) => serde::ser::Error::custom(msg), + } +} + serialize_trait_object!(Serialize); struct MakeSerializer(TraitObject);