@ -66,6 +66,73 @@ pub mod timestamp
}
}
pub mod serialise
{
use super ::* ;
#[ instrument(err, skip(value), fields(value_type=?std::any::type_name::<T>())) ]
pub fn into_text < T : Serialize > ( value : & T ) -> Result < String , Error >
{
let bytes = serde_cbor ::to_vec ( value ) . map_err ( Error ::Serialise ) ? ;
Ok ( base64 ::encode ( bytes ) )
}
#[ derive(Debug) ]
pub enum Error
{
Base64 ( base64 ::DecodeError ) ,
Serialise ( serde_cbor ::Error ) ,
Deserialise ( serde_cbor ::Error ) ,
}
impl std ::error ::Error for Error
{
fn source ( & self ) -> Option < & ( dyn std ::error ::Error + ' static ) > {
match & self {
Self ::Base64 ( b ) = > Some ( b ) ,
Self ::Serialise ( b ) = > Some ( b ) ,
Self ::Deserialise ( b ) = > Some ( b ) ,
}
}
}
impl std ::fmt ::Display for Error
{
fn fmt ( & self , f : & mut std ::fmt ::Formatter < ' _ > ) -> std ::fmt ::Result
{
match self {
Self ::Base64 ( _ ) = > write! ( f , "base64 decode failed" ) ,
Self ::Serialise ( _ ) = > write! ( f , "serialisation failed" ) ,
Self ::Deserialise ( _ ) = > write! ( f , "deserialisation failed" ) ,
}
}
}
#[ instrument(err, skip(string), fields(string = ?string.as_ref())) ]
pub fn from_text < T : serde ::de ::DeserializeOwned > ( string : impl AsRef < [ u8 ] > ) -> Result < T , Error >
{
let bytes = base64 ::decode ( string ) . map_err ( Error ::Base64 ) ? ;
serde_cbor ::from_reader ( & bytes [ .. ] ) . map_err ( Error ::Deserialise )
}
#[ cfg(test) ]
mod tests
{
use crate ::* ;
#[ test ]
fn test_ser ( ) -> Result < ( ) , eyre ::Report >
{
let object = "hello world" ;
let enc = super ::into_text ( & String ::from ( object ) ) . wrap_err ( eyre ::eyre ! ( "failed to encode" ) ) ? ;
println! ( "Enc: {:?}" , enc ) ;
let dec : String = super ::from_text ( enc ) . wrap_err ( eyre ::eyre ! ( "failed to decode" ) ) ? ;
assert_eq! ( object , dec ) ;
Ok ( ( ) )
}
}
}
/* / / / Dispatch params operations that can be handled at top level (i.e. `Help`)
async fn dispatch_args ( ) -> Result < args ::Operation , eyre ::Report >
{