@@ -20,7 +20,7 @@ use rmp::{encode, Marker};
2020use crate :: config:: {
2121 BinaryConfig , DefaultConfig , HumanReadableConfig , RuntimeConfig , SerializerConfig , StructMapConfig , StructTupleConfig
2222} ;
23- use crate :: MSGPACK_EXT_STRUCT_NAME ;
23+ use crate :: { MSGPACK_EXT_STRUCT_NAME , MSGPACK_TIMESTAMP_STRUCT_NAME } ;
2424
2525/// This type represents all possible errors that can occur when serializing or
2626/// deserializing MessagePack data.
@@ -685,6 +685,13 @@ where
685685 return ext_se. end ( ) ;
686686 }
687687
688+ if name == MSGPACK_TIMESTAMP_STRUCT_NAME {
689+ let mut ts_se = TimestampSerializer :: new ( & mut self . wr ) ;
690+ value. serialize ( & mut ts_se) ?;
691+
692+ return Ok ( ( ) ) ;
693+ }
694+
688695 // Encode as if it's inner type.
689696 value. serialize ( self )
690697 }
@@ -1269,3 +1276,186 @@ impl Write for FallibleWriter {
12691276 Ok ( ( ) )
12701277 }
12711278}
1279+
1280+ struct TimestampSerializer < ' a , W > {
1281+ wr : & ' a mut W
1282+ }
1283+
1284+ impl < ' a , W : Write > TimestampSerializer < ' a , W > {
1285+ fn new ( wr : & ' a mut W ) -> Self {
1286+ Self { wr }
1287+ }
1288+ }
1289+
1290+ impl < ' a , W : Write > serde:: Serializer for & ' a mut TimestampSerializer < ' a , W > {
1291+ type Ok = ( ) ;
1292+
1293+ type Error = Error ;
1294+
1295+ type SerializeSeq = serde:: ser:: Impossible < ( ) , Error > ;
1296+ type SerializeTuple = serde:: ser:: Impossible < ( ) , Error > ;
1297+ type SerializeTupleStruct = serde:: ser:: Impossible < ( ) , Error > ;
1298+ type SerializeTupleVariant = serde:: ser:: Impossible < ( ) , Error > ;
1299+ type SerializeMap = serde:: ser:: Impossible < ( ) , Error > ;
1300+ type SerializeStruct = serde:: ser:: Impossible < ( ) , Error > ;
1301+ type SerializeStructVariant = serde:: ser:: Impossible < ( ) , Error > ;
1302+
1303+ fn serialize_u128 ( self , v : u128 ) -> Result < Self :: Ok , Self :: Error > {
1304+ let ts = rmp:: Timestamp :: from_u128 ( v) . ok_or_else ( || Error :: Syntax ( format ! ( "Not a valid Timestamp" ) ) ) ?;
1305+ rmp:: encode:: write_timestamp ( self . wr , ts) . map_err ( |_| Error :: Syntax ( format ! ( "Not a valid Timestamp" ) ) ) ?;
1306+ Ok ( ( ) )
1307+ }
1308+
1309+ fn serialize_bool ( self , _v : bool ) -> Result < Self :: Ok , Self :: Error > {
1310+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1311+ }
1312+
1313+ fn serialize_i8 ( self , _v : i8 ) -> Result < Self :: Ok , Self :: Error > {
1314+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1315+ }
1316+
1317+ fn serialize_i16 ( self , _v : i16 ) -> Result < Self :: Ok , Self :: Error > {
1318+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1319+ }
1320+
1321+ fn serialize_i32 ( self , _v : i32 ) -> Result < Self :: Ok , Self :: Error > {
1322+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1323+ }
1324+
1325+ fn serialize_i64 ( self , _v : i64 ) -> Result < Self :: Ok , Self :: Error > {
1326+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1327+ }
1328+
1329+ fn serialize_u8 ( self , _v : u8 ) -> Result < Self :: Ok , Self :: Error > {
1330+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1331+ }
1332+
1333+ fn serialize_u16 ( self , _v : u16 ) -> Result < Self :: Ok , Self :: Error > {
1334+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1335+ }
1336+
1337+ fn serialize_u32 ( self , _v : u32 ) -> Result < Self :: Ok , Self :: Error > {
1338+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1339+ }
1340+
1341+ fn serialize_u64 ( self , _v : u64 ) -> Result < Self :: Ok , Self :: Error > {
1342+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1343+ }
1344+
1345+ fn serialize_f32 ( self , _v : f32 ) -> Result < Self :: Ok , Self :: Error > {
1346+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1347+ }
1348+
1349+ fn serialize_f64 ( self , _v : f64 ) -> Result < Self :: Ok , Self :: Error > {
1350+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1351+ }
1352+
1353+ fn serialize_char ( self , _v : char ) -> Result < Self :: Ok , Self :: Error > {
1354+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1355+ }
1356+
1357+ fn serialize_str ( self , _v : & str ) -> Result < Self :: Ok , Self :: Error > {
1358+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1359+ }
1360+
1361+ fn serialize_bytes ( self , _v : & [ u8 ] ) -> Result < Self :: Ok , Self :: Error > {
1362+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1363+ }
1364+
1365+ fn serialize_none ( self ) -> Result < Self :: Ok , Self :: Error > {
1366+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1367+ }
1368+
1369+ fn serialize_some < T > ( self , _value : & T ) -> Result < Self :: Ok , Self :: Error >
1370+ where
1371+ T : ?Sized + Serialize {
1372+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1373+ }
1374+
1375+ fn serialize_unit ( self ) -> Result < Self :: Ok , Self :: Error > {
1376+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1377+ }
1378+
1379+ fn serialize_unit_struct ( self , _name : & ' static str ) -> Result < Self :: Ok , Self :: Error > {
1380+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1381+ }
1382+
1383+ fn serialize_unit_variant (
1384+ self ,
1385+ _name : & ' static str ,
1386+ _variant_index : u32 ,
1387+ _variant : & ' static str ,
1388+ ) -> Result < Self :: Ok , Self :: Error > {
1389+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1390+ }
1391+
1392+ fn serialize_newtype_struct < T > (
1393+ self ,
1394+ _name : & ' static str ,
1395+ _value : & T ,
1396+ ) -> Result < Self :: Ok , Self :: Error >
1397+ where
1398+ T : ?Sized + Serialize {
1399+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1400+ }
1401+
1402+ fn serialize_newtype_variant < T > (
1403+ self ,
1404+ _name : & ' static str ,
1405+ _variant_index : u32 ,
1406+ _variant : & ' static str ,
1407+ _value : & T ,
1408+ ) -> Result < Self :: Ok , Self :: Error >
1409+ where
1410+ T : ?Sized + Serialize {
1411+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1412+ }
1413+
1414+ fn serialize_seq ( self , _len : Option < usize > ) -> Result < Self :: SerializeSeq , Self :: Error > {
1415+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1416+ }
1417+
1418+ fn serialize_tuple ( self , _len : usize ) -> Result < Self :: SerializeTuple , Self :: Error > {
1419+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1420+ }
1421+
1422+ fn serialize_tuple_struct (
1423+ self ,
1424+ _name : & ' static str ,
1425+ _len : usize ,
1426+ ) -> Result < Self :: SerializeTupleStruct , Self :: Error > {
1427+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1428+ }
1429+
1430+ fn serialize_tuple_variant (
1431+ self ,
1432+ _name : & ' static str ,
1433+ _variant_index : u32 ,
1434+ _variant : & ' static str ,
1435+ _len : usize ,
1436+ ) -> Result < Self :: SerializeTupleVariant , Self :: Error > {
1437+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1438+ }
1439+
1440+ fn serialize_map ( self , _len : Option < usize > ) -> Result < Self :: SerializeMap , Self :: Error > {
1441+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1442+ }
1443+
1444+ fn serialize_struct (
1445+ self ,
1446+ _name : & ' static str ,
1447+ _len : usize ,
1448+ ) -> Result < Self :: SerializeStruct , Self :: Error > {
1449+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1450+ }
1451+
1452+ fn serialize_struct_variant (
1453+ self ,
1454+ _name : & ' static str ,
1455+ _variant_index : u32 ,
1456+ _variant : & ' static str ,
1457+ _len : usize ,
1458+ ) -> Result < Self :: SerializeStructVariant , Self :: Error > {
1459+ Err ( Error :: InvalidDataModel ( "expected u128" ) )
1460+ }
1461+ }
0 commit comments