Skip to content

Commit

Permalink
fix(serde): update to latest serde/rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 29, 2015
1 parent 7754a16 commit 8dab8c0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/mako/Cargo.toml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ name = "${util.program_name()}"
hyper = ">= 0.6.8"
## Must match the one hyper uses, otherwise there are duplicate similarly named `Mime` structs
mime = "0.1.0"
serde = ">= 0.5.0"
serde_json = "*"
serde = ">= 0.6.0"
serde_json = ">= 0.6.0"
yup-oauth2 = "*"
% for dep in cargo.get('dependencies', list()):
${dep}
Expand Down
75 changes: 39 additions & 36 deletions src/rust/cli/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub struct FieldCursor(Vec<String>);

impl ToString for FieldCursor {
fn to_string(&self) -> String {
self.0.connect(".")
self.0.join(".")
}
}

Expand Down Expand Up @@ -235,15 +235,15 @@ impl FieldCursor {
}
}

pub fn set_json_value(&self, mut object: &mut Value,
pub fn set_json_value(&self, mut object: &mut Value,
value: &str, type_info: JsonTypeInfo,
err: &mut InvalidOptionsError,
err: &mut InvalidOptionsError,
orig_cursor: &FieldCursor) {
assert!(self.0.len() > 0);

for field in &self.0[..self.0.len()-1] {
let tmp = object;
object =
object =
match *tmp {
Value::Object(ref mut mapping) => {
mapping.entry(field.to_owned()).or_insert(
Expand All @@ -257,19 +257,19 @@ impl FieldCursor {
match *object {
Value::Object(ref mut mapping) => {
let field = &self.0[self.0.len()-1];
let to_jval =
|value: &str, jtype: JsonType, err: &mut InvalidOptionsError|
let to_jval =
|value: &str, jtype: JsonType, err: &mut InvalidOptionsError|
-> Value {
match jtype {
JsonType::Boolean =>
JsonType::Boolean =>
Value::Bool(arg_from_str(value, err, &field, "boolean")),
JsonType::Int =>
JsonType::Int =>
Value::I64(arg_from_str(value, err, &field, "int")),
JsonType::Uint =>
JsonType::Uint =>
Value::U64(arg_from_str(value, err, &field, "uint")),
JsonType::Float =>
JsonType::Float =>
Value::F64(arg_from_str(value, err, &field, "float")),
JsonType::String =>
JsonType::String =>
Value::String(value.to_owned()),
}
};
Expand Down Expand Up @@ -336,7 +336,7 @@ pub fn calltype_from_str(name: &str, valid_protocols: Vec<String>, err: &mut Inv
match UploadProtocol::from_str(name) {
Ok(up) => up,
Err(msg) => {
err.issues.push(CLIError::InvalidUploadProtocol(name.to_string(), valid_protocols));
err.issues.push(CLIError::InvalidUploadProtocol(name.to_string(), valid_protocols));
UploadProtocol::Simple
}
})
Expand Down Expand Up @@ -374,8 +374,8 @@ pub fn writer_from_opts(arg: Option<&str>) -> Result<Box<Write>, io::Error> {
}


pub fn arg_from_str<'a, T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'a str,
pub fn arg_from_str<'a, T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'a str,
arg_type: &'a str) -> T
where T: FromStr + Default,
<T as FromStr>::Err: fmt::Display {
Expand Down Expand Up @@ -409,7 +409,7 @@ impl TokenStorage for JsonTokenStorage {
match token {
None => {
match fs::remove_file(self.path(scope_hash)) {
Err(err) =>
Err(err) =>
match err.kind() {
io::ErrorKind::NotFound => Ok(()),
_ => Err(json::Error::IoError(err))
Expand All @@ -422,7 +422,7 @@ impl TokenStorage for JsonTokenStorage {
Ok(mut f) => {
match json::to_writer_pretty(&mut f, &token) {
Ok(_) => Ok(()),
Err(io_err) => Err(json::Error::IoError(io_err)),
Err(serde_err) => Err(serde_err),
}
},
Err(io_err) => Err(json::Error::IoError(io_err))
Expand Down Expand Up @@ -453,17 +453,17 @@ impl TokenStorage for JsonTokenStorage {
#[derive(Debug)]
pub enum ApplicationSecretError {
DecoderError((String, json::Error)),
FormatError(String),
FormatError(String),
}

impl fmt::Display for ApplicationSecretError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
ApplicationSecretError::DecoderError((ref path, ref err))
=> writeln!(f, "Could not decode file at '{}' with error: {}.",
=> writeln!(f, "Could not decode file at '{}' with error: {}.",
path, err),
ApplicationSecretError::FormatError(ref path)
=> writeln!(f, "'installed' field is unset in secret file at '{}'.",
=> writeln!(f, "'installed' field is unset in secret file at '{}'.",
path),
}
}
Expand All @@ -483,7 +483,7 @@ impl fmt::Display for ConfigurationError {
match *self {
ConfigurationError::DirectoryCreationFailed((ref dir, ref err))
=> writeln!(f, "Directory '{}' could not be created with error: {}.", dir, err),
ConfigurationError::DirectoryUnset
ConfigurationError::DirectoryUnset
=> writeln!(f, "--config-dir was unset or empty."),
ConfigurationError::HomeExpansionFailed(ref dir)
=> writeln!(f, "Couldn't find HOME directory of current user, failed to expand '{}'.", dir),
Expand Down Expand Up @@ -530,10 +530,10 @@ impl fmt::Display for FieldError {
FieldError::TrailingFieldSep(ref field)
=> writeln!(f, "'{}': Single field separator may not be last character.", field),
FieldError::Unknown(ref field, ref suggestion, ref value) => {
let suffix =
let suffix =
match *suggestion {
Some(ref s) => {
let kv =
let kv =
match *value {
Some(ref v) => format!("{}={}", s, v),
None => s.clone(),
Expand All @@ -544,7 +544,7 @@ impl fmt::Display for FieldError {
};
writeln!(f, "Field '{}' does not exist.{}", field, suffix)
},
FieldError::Duplicate(ref cursor)
FieldError::Duplicate(ref cursor)
=> writeln!(f, "Value at '{}' was already set", cursor),
FieldError::Empty
=> writeln!(f, "Field names must not be empty."),
Expand Down Expand Up @@ -572,13 +572,13 @@ impl fmt::Display for CLIError {
CLIError::Configuration(ref err) => write!(f, "Configuration -> {}", err),
CLIError::Input(ref err) => write!(f, "Input -> {}", err),
CLIError::Field(ref err) => write!(f, "Field -> {}", err),
CLIError::InvalidUploadProtocol(ref proto_name, ref valid_names)
=> writeln!(f, "'{}' is not a valid upload protocol. Choose from one of {}.", proto_name, valid_names.connect(", ")),
CLIError::ParseError(ref arg_name, ref type_name, ref value, ref err_desc)
CLIError::InvalidUploadProtocol(ref proto_name, ref valid_names)
=> writeln!(f, "'{}' is not a valid upload protocol. Choose from one of {}.", proto_name, valid_names.join(", ")),
CLIError::ParseError(ref arg_name, ref type_name, ref value, ref err_desc)
=> writeln!(f, "Failed to parse argument '{}' with value '{}' as {} with error: {}.",
arg_name, value, type_name, err_desc),
CLIError::UnknownParameter(ref param_name, ref possible_values) => {
let mut suffix =
let mut suffix =
match did_you_mean(param_name, &possible_values) {
Some(v) => format!(" Did you mean '{}' ?", v),
None => String::new(),
Expand Down Expand Up @@ -632,7 +632,7 @@ pub fn assure_config_dir_exists(dir: &str) -> Result<String, CLIError> {
return Err(CLIError::Configuration(ConfigurationError::DirectoryUnset))
}

let expanded_config_dir =
let expanded_config_dir =
if trdir.as_bytes()[0] == b'~' {
match env::var("HOME").ok().or(env::var("UserProfile").ok()) {
None => return Err(CLIError::Configuration(ConfigurationError::HomeExpansionFailed(trdir.to_string()))),
Expand All @@ -655,8 +655,8 @@ pub fn assure_config_dir_exists(dir: &str) -> Result<String, CLIError> {
Ok(expanded_config_dir)
}

pub fn application_secret_from_directory(dir: &str,
secret_basename: &str,
pub fn application_secret_from_directory(dir: &str,
secret_basename: &str,
json_console_secret: &str)
-> Result<ApplicationSecret, CLIError> {
let secret_path = Path::new(dir).join(secret_basename);
Expand All @@ -677,10 +677,13 @@ pub fn application_secret_from_directory(dir: &str,
Err(cfe) => cfe,
Ok(mut f) => {
// Assure we convert 'ugly' json string into pretty one
let console_secret: ConsoleApplicationSecret
let console_secret: ConsoleApplicationSecret
= json::from_str(json_console_secret).unwrap();
match json::to_writer_pretty(&mut f, &console_secret) {
Err(io_err) => io_err,
Err(serde_err) => match serde_err {
json::Error::IoError(err) => err,
_ => panic!("Unexpected serde error: {:#?}", serde_err)
},
Ok(_) => continue,
}
}
Expand All @@ -691,15 +694,15 @@ pub fn application_secret_from_directory(dir: &str,
},
Ok(mut f) => {
match json::de::from_reader::<_, ConsoleApplicationSecret>(f) {
Err(json::Error::IoError(err)) =>
Err(json::Error::IoError(err)) =>
return secret_io_error(err),
Err(json_err) =>
Err(json_err) =>
return Err(CLIError::Configuration(
ConfigurationError::Secret(
ApplicationSecretError::DecoderError(
(secret_str(), json_err)
)))),
Ok(console_secret) =>
Ok(console_secret) =>
match console_secret.installed {
Some(secret) => return Ok(secret),
None => return Err(
Expand All @@ -713,4 +716,4 @@ pub fn application_secret_from_directory(dir: &str,
}
}
unreachable!();
}
}

0 comments on commit 8dab8c0

Please sign in to comment.