Skip to content

Commit 894b5b5

Browse files
committed
feat(CLI): adjust to serde usage in yup-oauth
* More detailed error type for JsonTokenStorage * removed all traces of rustc_serialize * use pretty-printers everywhere to allow writing human-readable json files for secretes and for tokens Fixes #93
1 parent fac5041 commit 894b5b5

5 files changed

Lines changed: 36 additions & 38 deletions

File tree

etc/api/type-cli.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ cargo:
2727
is_executable: YES
2828
dependencies:
2929
- clap = "*"
30-
- rustc-serialize = "*"
3130
- yup-hyper-mock = "*"
3231
- serde = ">= 0.3.0"
3332
- serde_macros = "*"

src/mako/api/lib/schema.mako

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
IO_TYPES, activity_split, enclose_in, REQUEST_MARKER_TRAIT, mb_type, indent_all_but_first_by,
44
NESTED_TYPE_SUFFIX, RESPONSE_MARKER_TRAIT, split_camelcase_s, METHODS_RESOURCE, unique_type_name,
55
PART_MARKER_TRAIT, canonical_type_name, TO_PARTS_MARKER, UNUSED_TYPE_MARKER, is_schema_with_optionals)
6-
7-
default_traits = ('RustcEncodable', 'Clone', 'Default')
86
%>\
97
## Build a schema which must be an object
108
###################################################################################################################

src/mako/cli/lib/engine.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::default::Default;
3333
use std::str::FromStr;
3434
3535
use oauth2::{Authenticator, DefaultAuthenticatorDelegate};
36-
use rustc_serialize::json;
36+
use serde::json;
3737
use clap::ArgMatches;
3838
3939
struct Engine<'n, 'a> {

src/mako/cli/main.rs.mako

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
extern crate clap;
1919
extern crate yup_oauth2 as oauth2;
2020
extern crate yup_hyper_mock as mock;
21-
extern crate rustc_serialize;
2221
extern crate serde;
2322
extern crate hyper;
2423
extern crate mime;

src/rust/cli/cmn.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oauth2::{ApplicationSecret, ConsoleApplicationSecret, TokenStorage, Token};
2-
use rustc_serialize::json;
2+
use serde::json;
33
use mime::Mime;
44
use clap::{App, SubCommand};
55

@@ -208,49 +208,47 @@ impl JsonTokenStorage {
208208
}
209209

210210
impl TokenStorage for JsonTokenStorage {
211-
type Error = io::Error;
211+
type Error = json::Error;
212212

213213
// NOTE: logging might be interesting, currently we swallow all errors
214-
fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option<Token>) -> Option<io::Error> {
214+
fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option<Token>) -> Option<json::Error> {
215215
match token {
216216
None => {
217217
match fs::remove_file(self.path(scope_hash)) {
218218
Err(err) =>
219219
match err.kind() {
220220
io::ErrorKind::NotFound => None,
221-
_ => Some(err)
221+
_ => Some(json::Error::IoError(err))
222222
},
223223
Ok(_) => None
224224
}
225225
}
226226
Some(token) => {
227-
let json_token = json::encode(&token).unwrap();
228227
match fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash)) {
229228
Ok(mut f) => {
230-
match f.write(json_token.as_bytes()) {
229+
match json::to_writer_pretty(&mut f, &token) {
231230
Ok(_) => None,
232-
Err(io_err) => Some(io_err),
231+
Err(io_err) => Some(json::Error::IoError(io_err)),
233232
}
234233
},
235-
Err(io_err) => Some(io_err)
234+
Err(io_err) => Some(json::Error::IoError(io_err))
236235
}
237236
}
238237
}
239238
}
240239

241-
fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result<Option<Token>, io::Error> {
240+
fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result<Option<Token>, json::Error> {
242241
match fs::File::open(&self.path(scope_hash)) {
243242
Ok(mut f) => {
244-
let mut json_string = String::new();
245-
match f.read_to_string(&mut json_string) {
246-
Ok(_) => Ok(Some(json::decode::<Token>(&json_string).unwrap())),
247-
Err(io_err) => Err(io_err),
243+
match json::de::from_reader(f) {
244+
Ok(token) => Ok(Some(token)),
245+
Err(err) => Err(err),
248246
}
249247
},
250248
Err(io_err) => {
251249
match io_err.kind() {
252250
io::ErrorKind::NotFound => Ok(None),
253-
_ => Err(io_err)
251+
_ => Err(json::Error::IoError(io_err))
254252
}
255253
}
256254
}
@@ -260,7 +258,7 @@ impl TokenStorage for JsonTokenStorage {
260258

261259
#[derive(Debug)]
262260
pub enum ApplicationSecretError {
263-
DecoderError((String, json::DecoderError)),
261+
DecoderError((String, json::Error)),
264262
FormatError(String),
265263
}
266264

@@ -440,7 +438,7 @@ pub fn assure_config_dir_exists(dir: &str) -> Result<String, CLIError> {
440438

441439
pub fn application_secret_from_directory(dir: &str,
442440
secret_basename: &str,
443-
json_app_secret: &str)
441+
json_console_secret: &str)
444442
-> Result<ApplicationSecret, CLIError> {
445443
let secret_path = Path::new(dir).join(secret_basename);
446444
let secret_str = || secret_path.as_path().to_str().unwrap().to_string();
@@ -459,7 +457,10 @@ pub fn application_secret_from_directory(dir: &str,
459457
err = match fs::OpenOptions::new().create(true).write(true).open(&secret_path) {
460458
Err(cfe) => cfe,
461459
Ok(mut f) => {
462-
match f.write(json_app_secret.as_bytes()) {
460+
// Assure we convert 'ugly' json string into pretty one
461+
let console_secret: ConsoleApplicationSecret
462+
= json::from_str(json_console_secret).unwrap();
463+
match json::to_writer_pretty(&mut f, &console_secret) {
463464
Err(io_err) => io_err,
464465
Ok(_) => continue,
465466
}
@@ -470,23 +471,24 @@ pub fn application_secret_from_directory(dir: &str,
470471
return secret_io_error(err)
471472
},
472473
Ok(mut f) => {
473-
let mut json_encoded_secret = String::new();
474-
if let Err(io_err) = f.read_to_string(&mut json_encoded_secret) {
475-
return secret_io_error(io_err)
476-
}
477-
match json::decode::<ConsoleApplicationSecret>(&json_encoded_secret) {
478-
Err(json_decode_error) => return Err(CLIError::Configuration(
479-
ConfigurationError::Secret(ApplicationSecretError::DecoderError(
480-
(secret_str(), json_decode_error)
474+
match json::de::from_reader::<_, ConsoleApplicationSecret>(f) {
475+
Err(json::Error::IoError(err)) =>
476+
return secret_io_error(err),
477+
Err(json_err) =>
478+
return Err(CLIError::Configuration(
479+
ConfigurationError::Secret(
480+
ApplicationSecretError::DecoderError(
481+
(secret_str(), json_err)
481482
)))),
482-
Ok(console_secret) => match console_secret.installed {
483-
Some(secret) => return Ok(secret),
484-
None => return Err(
485-
CLIError::Configuration(
486-
ConfigurationError::Secret(
487-
ApplicationSecretError::FormatError(secret_str())
488-
)))
489-
},
483+
Ok(console_secret) =>
484+
match console_secret.installed {
485+
Some(secret) => return Ok(secret),
486+
None => return Err(
487+
CLIError::Configuration(
488+
ConfigurationError::Secret(
489+
ApplicationSecretError::FormatError(secret_str())
490+
)))
491+
},
490492
}
491493
}
492494
}

0 commit comments

Comments
 (0)