@@ -27,7 +27,7 @@ This crate is fully compatible with Cargo. Just add it to your `Cargo.toml`:
27
27
``` toml
28
28
[dependencies ]
29
29
docopt = " 0.7"
30
- rustc-serialize = " 0.3 " # if you're using `derive(RustcDecodable )`
30
+ serde_derive = " 1.0 " # if you're using `derive(Deserialize )`
31
31
```
32
32
33
33
If you want to use the macro, then add ` docopt_macros = "0.7" ` instead.
@@ -42,7 +42,8 @@ of the named values in the Docopt usage string. Values will be automatically
42
42
converted to those types (or an error will be reported).
43
43
44
44
``` rust
45
- extern crate rustc_serialize;
45
+ #[macro_use]
46
+ extern crate serde_derive;
46
47
extern crate docopt;
47
48
48
49
use docopt :: Docopt ;
@@ -66,7 +67,7 @@ Options:
66
67
--drifting Drifting mine.
67
68
" ;
68
69
69
- #[derive(Debug , RustcDecodable )]
70
+ #[derive(Debug , Deserialize )]
70
71
struct Args {
71
72
flag_speed : isize ,
72
73
flag_drifting : bool ,
@@ -79,7 +80,7 @@ struct Args {
79
80
80
81
fn main () {
81
82
let args : Args = Docopt :: new (USAGE )
82
- . and_then (| d | d . decode ())
83
+ . and_then (| d | d . deserialize ())
83
84
. unwrap_or_else (| e | e . exit ());
84
85
println! (" {:?}" , args );
85
86
}
@@ -93,7 +94,8 @@ works on a **nightly Rust compiler**:
93
94
#![feature(plugin)]
94
95
#![plugin(docopt_macros)]
95
96
96
- extern crate rustc_serialize;
97
+ #[macro_use]
98
+ extern crate serde_derive;
97
99
extern crate docopt;
98
100
99
101
use docopt :: Docopt ;
@@ -118,7 +120,7 @@ Options:
118
120
" );
119
121
120
122
fn main () {
121
- let args : Args = Args :: docopt (). decode (). unwrap_or_else (| e | e . exit ());
123
+ let args : Args = Args :: docopt (). deserialize (). unwrap_or_else (| e | e . exit ());
122
124
println! (" {:?}" , args );
123
125
}
124
126
```
@@ -150,14 +152,15 @@ Here's another example that shows how to specify the types of your arguments:
150
152
#![feature(plugin)]
151
153
#![plugin(docopt_macros)]
152
154
153
- extern crate rustc_serialize;
155
+ #[macro_use]
156
+ extern crate serde_derive;
154
157
155
158
extern crate docopt;
156
159
157
160
docopt! (Args , " Usage: add <x> <y>" , arg_x : i32 , arg_y : i32 );
158
161
159
162
fn main () {
160
- let args : Args = Args :: docopt (). decode (). unwrap_or_else (| e | e . exit ());
163
+ let args : Args = Args :: docopt (). deserialize (). unwrap_or_else (| e | e . exit ());
161
164
println! (" x: {}, y: {}" , args . arg_x, args . arg_y);
162
165
}
163
166
```
@@ -185,10 +188,15 @@ Docopt features.
185
188
#![feature(plugin)]
186
189
#![plugin(docopt_macros)]
187
190
188
- extern crate rustc_serialize;
191
+ #[macro_use]
192
+ extern crate serde_derive;
193
+ extern crate serde;
189
194
190
195
extern crate docopt;
191
196
197
+ use serde :: de;
198
+ use std :: fmt;
199
+
192
200
docopt! (Args derive Debug , "
193
201
Usage: rustc [options] [--cfg SPEC... -L PATH...] INPUT
194
202
rustc (--help | --version)
@@ -203,27 +211,48 @@ Options:
203
211
--opt-level LEVEL Optimize with possible levels 0-3.
204
212
" , flag_opt_level : Option <OptLevel >, flag_emit : Option <Emit >);
205
213
206
- #[derive(RustcDecodable , Debug )]
214
+ #[derive(Deserialize , Debug )]
207
215
enum Emit { Asm , Ir , Bc , Obj , Link }
208
216
209
217
#[derive(Debug )]
210
218
enum OptLevel { Zero , One , Two , Three }
211
219
212
- impl rustc_serialize :: Decodable for OptLevel {
213
- fn decode <D : rustc_serialize :: Decoder >(d : & mut D ) -> Result <OptLevel , D :: Error > {
214
- Ok (match try ! (d . read_usize ()) {
215
- 0 => OptLevel :: Zero , 1 => OptLevel :: One ,
216
- 2 => OptLevel :: Two , 3 => OptLevel :: Three ,
220
+ struct OptLevelVisitor ;
221
+
222
+ impl <'de > de :: Visitor <'de > for OptLevelVisitor {
223
+ type Value = OptLevel ;
224
+
225
+ fn expecting (& self , formatter : & mut fmt :: Formatter ) -> fmt :: Result {
226
+ formatter . write_str (" an integer between 0 and 3" )
227
+ }
228
+
229
+ fn visit_u8 <E >(self , value : u8 ) -> Result <Self :: Value , E >
230
+ where E : de :: Error
231
+ {
232
+ let level = match value {
233
+ 0 => OptLevel :: Zero ,
234
+ 1 => OptLevel :: One ,
235
+ 2 => OptLevel :: Two ,
236
+ 3 => OptLevel :: Three ,
217
237
n => {
218
- let err = format! (" Could not decode '{}' as opt-level." , n );
219
- return Err (d . error ( & * err ));
238
+ let err = format! (" Could not deserialize '{}' as opt-level." , n );
239
+ return Err (E :: custom ( err ));
220
240
}
221
- })
241
+ };
242
+ Ok (level )
243
+ }
244
+ }
245
+
246
+ impl <'de > de :: Deserialize <'de > for OptLevel {
247
+ fn deserialize <D >(deserializer : D ) -> Result <OptLevel , D :: Error >
248
+ where D : de :: Deserializer <'de >
249
+ {
250
+ deserializer . deserialize_u8 (OptLevelVisitor )
222
251
}
223
252
}
224
253
225
254
fn main () {
226
- let args : Args = Args :: docopt (). decode (). unwrap_or_else (| e | e . exit ());
255
+ let args : Args = Args :: docopt (). deserialize (). unwrap_or_else (| e | e . exit ());
227
256
println! (" {:?}" , args );
228
257
}
229
258
```
0 commit comments