@@ -84,6 +84,43 @@ fn all_required() {
8484 ) ;
8585}
8686
87+ #[ test]
88+ fn decode_from_str ( ) {
89+ #[ derive( Debug , DecodeFromStr , PartialEq ) ]
90+ struct Foo ( String ) ;
91+
92+ #[ derive( Debug , Snafu ) ]
93+ #[ snafu( display( "bar error" ) ) ]
94+ struct FooError ;
95+
96+ impl FromStr for Foo {
97+ type Err = FooError ;
98+
99+ fn from_str ( s : & str ) -> Result < Self , FooError > {
100+ if s == "foo" {
101+ Ok ( Foo ( s. to_string ( ) ) )
102+ } else {
103+ Err ( FooError )
104+ }
105+ }
106+ }
107+
108+ assert_eq ! (
109+ Foo :: decode_from_slice( & [ 0x63 , 0x66 , 0x6f , 0x6f ] ) . unwrap( ) ,
110+ Foo ( "foo" . to_string( ) ) ,
111+ ) ;
112+
113+ let err = Foo :: decode_from_slice ( & [ 0x63 , 0x62 , 0x61 , 0x72 ] ) . unwrap_err ( ) ;
114+
115+ assert_matches ! (
116+ err,
117+ DecodeError :: FromStr {
118+ name: "Foo" ,
119+ ref source,
120+ } if source. to_string( ) == "bar error" ,
121+ ) ;
122+ }
123+
87124#[ test]
88125fn decode_with_optional ( ) {
89126 fn decode_offset ( decoder : & mut Decoder ) -> Result < u64 , DecodeError > {
@@ -124,6 +161,20 @@ fn decode_with_required() {
124161 ) ;
125162}
126163
164+ #[ test]
165+ fn encode_display ( ) {
166+ #[ derive( EncodeDisplay ) ]
167+ struct Foo ;
168+
169+ impl Display for Foo {
170+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
171+ write ! ( f, "foo" )
172+ }
173+ }
174+
175+ assert_eq ! ( Foo . encode_to_vec( ) , [ 0x63 , 0x66 , 0x6f , 0x6f ] ) ;
176+ }
177+
127178#[ test]
128179fn encode_with_optional ( ) {
129180 struct Foreign ( u64 ) ;
@@ -171,57 +222,6 @@ fn encode_with_required() {
171222 ) ;
172223}
173224
174- #[ test]
175- fn decode_from_str ( ) {
176- #[ derive( Debug , DecodeFromStr , PartialEq ) ]
177- struct Foo ( String ) ;
178-
179- #[ derive( Debug , Snafu ) ]
180- #[ snafu( display( "bar error" ) ) ]
181- struct FooError ;
182-
183- impl FromStr for Foo {
184- type Err = FooError ;
185-
186- fn from_str ( s : & str ) -> Result < Self , FooError > {
187- if s == "foo" {
188- Ok ( Foo ( s. to_string ( ) ) )
189- } else {
190- Err ( FooError )
191- }
192- }
193- }
194-
195- assert_eq ! (
196- Foo :: decode_from_slice( & [ 0x63 , 0x66 , 0x6f , 0x6f ] ) . unwrap( ) ,
197- Foo ( "foo" . to_string( ) ) ,
198- ) ;
199-
200- let err = Foo :: decode_from_slice ( & [ 0x63 , 0x62 , 0x61 , 0x72 ] ) . unwrap_err ( ) ;
201-
202- assert_matches ! (
203- err,
204- DecodeError :: FromStr {
205- name: "Foo" ,
206- ref source,
207- } if source. to_string( ) == "bar error" ,
208- ) ;
209- }
210-
211- #[ test]
212- fn encode_display ( ) {
213- #[ derive( EncodeDisplay ) ]
214- struct Foo ;
215-
216- impl Display for Foo {
217- fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
218- write ! ( f, "foo" )
219- }
220- }
221-
222- assert_eq ! ( Foo . encode_to_vec( ) , [ 0x63 , 0x66 , 0x6f , 0x6f ] ) ;
223- }
224-
225225#[ test]
226226fn enum_invalid_discriminant ( ) {
227227 #[ derive( Debug , Decode , FromRepr ) ]
0 commit comments