11//! Deserialize AMF0 data to a Rust data structure.
22
3+ use std:: io;
4+
35use scuffle_bytes_util:: zero_copy:: ZeroCopyReader ;
46use serde:: de:: { EnumAccess , IntoDeserializer , MapAccess , SeqAccess , VariantAccess } ;
57
@@ -11,7 +13,7 @@ mod stream;
1113pub use stream:: * ;
1214
1315/// Deserialize a value from a given [`bytes::Buf`].
14- pub fn from_bytes < ' de , T > ( buf : impl bytes:: Buf ) -> crate :: Result < T >
16+ pub fn from_buf < ' de , T > ( buf : impl bytes:: Buf ) -> crate :: Result < T >
1517where
1618 T : serde:: de:: Deserialize < ' de > ,
1719{
2022 Ok ( value)
2123}
2224
25+ /// Deserialize a value from a given [`io::Read`].
26+ pub fn from_reader < ' de , T > ( reader : impl io:: Read ) -> crate :: Result < T >
27+ where
28+ T : serde:: de:: Deserialize < ' de > ,
29+ {
30+ let mut de = Amf0Decoder :: from_reader ( reader) ;
31+ let value = T :: deserialize ( & mut de) ?;
32+ Ok ( value)
33+ }
34+
35+ /// Deserialize a value from a given byte slice.
36+ pub fn from_slice < ' de , T > ( bytes : & ' de [ u8 ] ) -> crate :: Result < T >
37+ where
38+ T : serde:: de:: Deserialize < ' de > ,
39+ {
40+ let mut de = Amf0Decoder :: from_slice ( bytes) ;
41+ let value = T :: deserialize ( & mut de) ?;
42+ Ok ( value)
43+ }
44+
2345impl < ' de , R > serde:: de:: Deserializer < ' de > for & mut Amf0Decoder < R >
2446where
2547 R : ZeroCopyReader < ' de > ,
@@ -465,7 +487,7 @@ mod tests {
465487
466488 use crate :: de:: MultiValue ;
467489 use crate :: decoder:: Amf0Decoder ;
468- use crate :: { Amf0Error , Amf0Marker , Amf0Object , Amf0Value , from_bytes } ;
490+ use crate :: { Amf0Error , Amf0Marker , Amf0Object , Amf0Value , from_buf } ;
469491
470492 #[ test]
471493 fn string ( ) {
@@ -476,7 +498,7 @@ mod tests {
476498 b'h' , b'e' , b'l' , b'l' , b'o' ,
477499 ] ;
478500
479- let value: String = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
501+ let value: String = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
480502 assert_eq ! ( value, "hello" ) ;
481503
482504 #[ rustfmt:: skip]
@@ -486,11 +508,11 @@ mod tests {
486508 b'h' , b'e' , b'l' , b'l' , b'o' ,
487509 ] ;
488510
489- let value: String = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
511+ let value: String = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
490512 assert_eq ! ( value, "hello" ) ;
491513
492514 let bytes = [ Amf0Marker :: Boolean as u8 ] ;
493- let err = from_bytes :: < String > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
515+ let err = from_buf :: < String > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
494516 assert ! ( matches!(
495517 err,
496518 Amf0Error :: UnexpectedType {
@@ -503,11 +525,11 @@ mod tests {
503525 #[ test]
504526 fn bool ( ) {
505527 let bytes = [ Amf0Marker :: Boolean as u8 , 1 ] ;
506- let value: bool = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
528+ let value: bool = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
507529 assert ! ( value) ;
508530
509531 let bytes = [ Amf0Marker :: String as u8 ] ;
510- let err = from_bytes :: < bool > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
532+ let err = from_buf :: < bool > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
511533 assert ! ( matches!(
512534 err,
513535 Amf0Error :: UnexpectedType {
@@ -536,7 +558,7 @@ mod tests {
536558 ]
537559 } ;
538560
539- let value: T = from_bytes ( Bytes :: from_static ( & NUMBER_ONE ) ) . unwrap ( ) ;
561+ let value: T = from_buf ( Bytes :: from_static ( & NUMBER_ONE ) ) . unwrap ( ) ;
540562 assert_eq ! ( value, one) ;
541563 }
542564
@@ -556,11 +578,11 @@ mod tests {
556578 let mut bytes = vec ! [ Amf0Marker :: Date as u8 ] ;
557579 bytes. extend_from_slice ( & f64:: consts:: PI . to_be_bytes ( ) ) ;
558580 bytes. extend_from_slice ( & 0u16 . to_be_bytes ( ) ) ; // timezone
559- let value: f64 = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
581+ let value: f64 = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
560582 assert_eq ! ( value, f64 :: consts:: PI ) ;
561583
562584 let bytes = [ Amf0Marker :: Boolean as u8 ] ;
563- let err = from_bytes :: < f64 > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
585+ let err = from_buf :: < f64 > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
564586 assert ! ( matches!(
565587 err,
566588 Amf0Error :: UnexpectedType {
@@ -572,21 +594,21 @@ mod tests {
572594
573595 #[ test]
574596 fn char ( ) {
575- let err = from_bytes :: < char > ( Bytes :: from_owner ( [ ] ) ) . unwrap_err ( ) ;
597+ let err = from_buf :: < char > ( Bytes :: from_owner ( [ ] ) ) . unwrap_err ( ) ;
576598 assert ! ( matches!( err, Amf0Error :: CharNotSupported ) ) ;
577599 }
578600
579601 #[ test]
580602 fn optional ( ) {
581603 let bytes = [ Amf0Marker :: Null as u8 ] ;
582- let value: Option < bool > = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
604+ let value: Option < bool > = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
583605 assert_eq ! ( value, None ) ;
584606
585607 let bytes = [ Amf0Marker :: Null as u8 ] ;
586- from_bytes :: < ( ) > ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
608+ from_buf :: < ( ) > ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
587609
588610 let bytes = [ Amf0Marker :: String as u8 ] ;
589- let err = from_bytes :: < ( ) > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
611+ let err = from_buf :: < ( ) > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
590612 assert ! ( matches!(
591613 err,
592614 Amf0Error :: UnexpectedType {
@@ -596,18 +618,18 @@ mod tests {
596618 ) ) ;
597619
598620 let bytes = [ Amf0Marker :: Undefined as u8 ] ;
599- let value: Option < bool > = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
621+ let value: Option < bool > = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
600622 assert_eq ! ( value, None ) ;
601623
602624 let bytes = [ Amf0Marker :: Boolean as u8 , 0 ] ;
603- let value: Option < bool > = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
625+ let value: Option < bool > = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
604626 assert_eq ! ( value, Some ( false ) ) ;
605627
606628 #[ derive( serde:: Deserialize , PartialEq , Debug ) ]
607629 struct Unit ;
608630
609631 let bytes = [ Amf0Marker :: Null as u8 ] ;
610- let value: Unit = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
632+ let value: Unit = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
611633 assert_eq ! ( value, Unit ) ;
612634 }
613635
@@ -622,7 +644,7 @@ mod tests {
622644 0 , 5 , // length
623645 b'h' , b'e' , b'l' , b'l' , b'o' ,
624646 ] ;
625- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
647+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
626648 assert_eq ! ( value, Test ( "hello" . to_string( ) ) ) ;
627649 }
628650
@@ -641,7 +663,7 @@ mod tests {
641663 0 , 5 , // length
642664 b'h' , b'e' , b'l' , b'l' , b'o' ,
643665 ] ;
644- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
666+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
645667 assert_eq ! ( value, Test ( true , "hello" . to_string( ) ) ) ;
646668
647669 #[ rustfmt:: skip]
@@ -651,7 +673,7 @@ mod tests {
651673 Amf0Marker :: Boolean as u8 ,
652674 1 ,
653675 ] ;
654- let err = from_bytes :: < Test > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
676+ let err = from_buf :: < Test > ( Bytes :: from_owner ( bytes) ) . unwrap_err ( ) ;
655677 assert ! ( matches!( err, Amf0Error :: WrongArrayLength { expected: 2 , got: 1 } ) ) ;
656678 }
657679
@@ -679,7 +701,7 @@ mod tests {
679701 b'h' , b'e' , b'l' , b'l' , b'o' ,
680702 0 , 0 , Amf0Marker :: ObjectEnd as u8 ,
681703 ] ;
682- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
704+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
683705 assert_eq ! (
684706 value,
685707 Test {
@@ -716,7 +738,7 @@ mod tests {
716738 ] ;
717739 bytes. extend_from_slice ( & f64:: consts:: PI . to_be_bytes ( ) ) ;
718740 bytes. extend_from_slice ( & [ 0 , 0 , Amf0Marker :: ObjectEnd as u8 ] ) ;
719- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
741+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
720742
721743 assert_eq ! (
722744 value,
@@ -746,7 +768,7 @@ mod tests {
746768 ] ;
747769 bytes. extend_from_slice ( & f64:: consts:: PI . to_be_bytes ( ) ) ;
748770 bytes. extend_from_slice ( & [ 0 , 0 , 0 ] ) ; // not object end marker
749- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
771+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
750772
751773 assert_eq ! (
752774 value,
@@ -757,7 +779,7 @@ mod tests {
757779 }
758780 ) ;
759781
760- let err = from_bytes :: < Test > ( Bytes :: from_owner ( [ Amf0Marker :: String as u8 ] ) ) . unwrap_err ( ) ;
782+ let err = from_buf :: < Test > ( Bytes :: from_owner ( [ Amf0Marker :: String as u8 ] ) ) . unwrap_err ( ) ;
761783 assert ! ( matches!(
762784 err,
763785 Amf0Error :: UnexpectedType {
@@ -781,7 +803,7 @@ mod tests {
781803 0 , 1 , // length
782804 b'A' ,
783805 ] ;
784- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
806+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
785807 assert_eq ! ( value, Test :: A ) ;
786808
787809 #[ rustfmt:: skip]
@@ -790,7 +812,7 @@ mod tests {
790812 0 , 1 , // length
791813 b'B' ,
792814 ] ;
793- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
815+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
794816 assert_eq ! ( value, Test :: B ) ;
795817 }
796818
@@ -811,7 +833,7 @@ mod tests {
811833 Amf0Marker :: Boolean as u8 ,
812834 1 ,
813835 ] ;
814- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
836+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
815837 assert_eq ! ( value, Test :: A ( true ) ) ;
816838
817839 #[ rustfmt:: skip]
@@ -832,7 +854,7 @@ mod tests {
832854 b'w' , b'o' , b'r' , b'l' , b'd' ,
833855 0 , 0 , Amf0Marker :: ObjectEnd as u8 ,
834856 ] ;
835- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
857+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
836858 assert_eq ! (
837859 value,
838860 Test :: B {
@@ -854,7 +876,7 @@ mod tests {
854876 0 , 5 , // length
855877 b'h' , b'e' , b'l' , b'l' , b'o' ,
856878 ] ;
857- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
879+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
858880 assert_eq ! ( value, Test :: C ( true , "hello" . to_string( ) ) ) ;
859881 }
860882
@@ -909,7 +931,7 @@ mod tests {
909931 other : HashMap < StringCow < ' a > , Amf0Value < ' a > > ,
910932 }
911933
912- let value: Test = from_bytes ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
934+ let value: Test = from_buf ( Bytes :: from_owner ( bytes) ) . unwrap ( ) ;
913935 assert_eq ! (
914936 value,
915937 Test {
0 commit comments