11use oauth2:: { ApplicationSecret , ConsoleApplicationSecret , TokenStorage , Token } ;
2- use rustc_serialize :: json;
2+ use serde :: json;
33use mime:: Mime ;
44use clap:: { App , SubCommand } ;
55
@@ -208,49 +208,47 @@ impl JsonTokenStorage {
208208}
209209
210210impl 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 ) ]
262260pub 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
441439pub 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