diff options
author | Teddy Wing | 2018-11-07 18:16:51 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-07 18:16:51 +0100 |
commit | 747ce1c17c8bbf40a2e38521bec50d0bebc0e334 (patch) | |
tree | 22645a933a528bf811d4bc21f55b2d3554ba9ea6 | |
parent | ab9345bbfc8683f877766f2ff9d33b7d49045770 (diff) | |
download | dome-key-web-747ce1c17c8bbf40a2e38521bec50d0bebc0e334.tar.bz2 |
AquaticPrime::plist(): Remove `unwrap`s
Return an `Err` instead of unwrapping `Result`s.
* Add `std::string::FromUtf8Error` to `foreign_links` to support
`String::from_utf8` error result
* Add `InvalidLicenseData` when unwrapping the dictionary from
`plist_data`. The `as_dictionary()` method returns an `Option`, and if
we get `None` from that, our `plist()` function should consider that
an error because without this input data, it can't proceed.
-rw-r--r-- | license-generator/aquatic-prime/src/lib.rs | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/license-generator/aquatic-prime/src/lib.rs b/license-generator/aquatic-prime/src/lib.rs index 8c734fd..319d2dc 100644 --- a/license-generator/aquatic-prime/src/lib.rs +++ b/license-generator/aquatic-prime/src/lib.rs @@ -10,12 +10,24 @@ extern crate serde; extern crate serde_derive; mod errors { + use plist; + error_chain! { + foreign_links { + FromUtf8(::std::string::FromUtf8Error); + + Plist(plist::Error); + } + errors { PublicKeyIncorrectNumBits(bits: i32) { description("public key has incorrect bit size") display("public key has incorrect bit size: '{}'", bits) } + + InvalidLicenseData { + display("license data must be a dictionary") + } } } } @@ -91,18 +103,26 @@ impl<'a> AquaticPrime<'a> { // Get input as `Plist` let mut xml_for_plist = Vec::with_capacity(600); - plist::serde::serialize_to_xml(&mut xml_for_plist, &input_data).unwrap(); + plist::serde::serialize_to_xml(&mut xml_for_plist, &input_data)?; let xml_for_hash_map = xml_for_plist.clone(); - let plist_data = Plist::read(Cursor::new(&xml_for_plist)).unwrap(); + let plist_data = Plist::read(Cursor::new(&xml_for_plist))?; - let mut plist_dict = plist_data.as_dictionary().unwrap().to_owned(); + let mut plist_dict = plist_data + .as_dictionary() + .ok_or(ErrorKind::InvalidLicenseData)? + .to_owned(); // Get input as HashMap to send to `sign()` - let data: HashMap<String, String> = plist::serde::deserialize(Cursor::new(&xml_for_hash_map)).unwrap(); - - let signature = self.sign(data).unwrap(); - plist_dict.insert("Signature".to_owned(), Plist::Data(signature.into_bytes())); + let data: HashMap<String, String> = plist::serde::deserialize( + Cursor::new(&xml_for_hash_map) + )?; + + let signature = self.sign(data)?; + plist_dict.insert( + "Signature".to_owned(), + Plist::Data(signature.into_bytes()) + ); // Generate plist XML string let mut plist_xml = Cursor::new(Vec::with_capacity(600)); @@ -111,11 +131,11 @@ impl<'a> AquaticPrime<'a> { let mut writer = plist::xml::EventWriter::new(&mut plist_xml); for item in Plist::Dictionary(plist_dict).into_events() { - writer.write(&item).unwrap(); + writer.write(&item)?; } } - Ok(String::from_utf8(plist_xml.into_inner()).unwrap()) + Ok(String::from_utf8(plist_xml.into_inner())?) } } |