Change to ? operator

This commit is contained in:
Thomas Gideon 2017-02-17 12:39:28 -05:00
parent 94b83d12aa
commit 3c95e1b163
4 changed files with 20 additions and 20 deletions

View file

@ -11,9 +11,9 @@ use medallion::{
};
fn load_key(keypath: &str) -> Result<String, Error> {
let mut key_file = try!(File::open(keypath));
let mut key_file = File::open(keypath)?;
let mut key = String::new();
try!(key_file.read_to_string(&mut key));
key_file.read_to_string(&mut key)?;
Ok(key)
}

View file

@ -40,10 +40,10 @@ impl<T: Serialize + Deserialize> Component for Claims<T> {
/// This implementation simply parses the base64 data twice, each time applying it to the
/// registered and private claims.
fn from_base64(raw: &str) -> Result<Claims<T>> {
let data = try!(decode_config(raw, URL_SAFE));
let reg_claims: Registered = try!(serde_json::from_slice(&data));
let data = decode_config(raw, URL_SAFE)?;
let reg_claims: Registered = serde_json::from_slice(&data)?;
let pri_claims: T = try!(serde_json::from_slice(&data));
let pri_claims: T = serde_json::from_slice(&data)?;
Ok(Claims {
@ -58,7 +58,7 @@ impl<T: Serialize + Deserialize> Component for Claims<T> {
if let Value::Object(mut reg_map) = serde_json::to_value(&self.reg)? {
if let Value::Object(pri_map) = serde_json::to_value(&self.private)? {
reg_map.extend(pri_map);
let s = try!(serde_json::to_string(&reg_map));
let s = serde_json::to_string(&reg_map)?;
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
Ok(enc)
} else {

View file

@ -30,7 +30,7 @@ pub fn verify(target: &str, data: &str, key: &[u8], algorithm: &Algorithm) -> Re
}
fn sign_hmac(data: &str, key: &[u8], digest: MessageDigest) -> Result<String> {
let secret_key = try!(PKey::hmac(key));
let secret_key = PKey::hmac(key)?;
let mut signer = Signer::new(digest, &secret_key)?;
signer.update(data.as_bytes())?;
@ -67,7 +67,7 @@ fn verify_rsa(signature: &str, data: &str, key: &[u8], digest: MessageDigest) ->
let pkey = PKey::from_rsa(public_key)?;
let mut verifier = Verifier::new(digest, &pkey)?;
verifier.update(data.as_bytes())?;
Ok(try!(verifier.finish(&signature_bytes)))
Ok(verifier.finish(&signature_bytes)?)
}
#[cfg(test)]
@ -128,9 +128,9 @@ mod tests {
}
fn load_key(keypath: &str) -> Result<String, Error> {
let mut key_file = try!(File::open(keypath));
let mut key_file = File::open(keypath)?;
let mut key = String::new();
try!(key_file.read_to_string(&mut key));
key_file.read_to_string(&mut key)?;
Ok(key)
}
}

View file

@ -50,14 +50,14 @@ impl<T> Component for T
/// Parse from a string.
fn from_base64(raw: &str) -> Result<T> {
let data = try!(decode_config(raw, URL_SAFE));
let s = try!(String::from_utf8(data));
Ok(try!(serde_json::from_str(&*s)))
let data = decode_config(raw, URL_SAFE)?;
let s = String::from_utf8(data)?;
Ok(serde_json::from_str(&*s)?)
}
/// Encode to a string.
fn to_base64(&self) -> Result<String> {
let s = try!(serde_json::to_string(&self));
let s = serde_json::to_string(&self)?;
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
Ok(enc)
}
@ -80,8 +80,8 @@ impl<H, C> Token<H, C>
Ok(Token {
raw: Some(raw.into()),
header: try!(Component::from_base64(pieces[0])),
claims: try!(Component::from_base64(pieces[1])),
header: Component::from_base64(pieces[0])?,
claims: Component::from_base64(pieces[1])?,
})
}
@ -102,8 +102,8 @@ impl<H, C> Token<H, C>
/// Generate the signed token from a key with the specific algorithm as a url-safe, base64
/// string.
pub fn signed(&self, key: &[u8]) -> Result<String> {
let header = try!(Component::to_base64(&self.header));
let claims = try!(self.claims.to_base64());
let header = Component::to_base64(&self.header)?;
let claims = self.claims.to_base64()?;
let data = format!("{}.{}", header, claims);
let sig = crypt::sign(&*data, key, &self.header.alg())?;
@ -172,9 +172,9 @@ mod tests {
}
fn load_key(keypath: &str) -> Result<String, Error> {
let mut key_file = try!(File::open(keypath));
let mut key_file = File::open(keypath)?;
let mut key = String::new();
try!(key_file.read_to_string(&mut key));
key_file.read_to_string(&mut key)?;
Ok(key)
}
}