diff options
author | Teddy Wing | 2017-11-11 21:13:46 +0100 |
---|---|---|
committer | Teddy Wing | 2017-11-11 21:13:46 +0100 |
commit | b1e858f8590a676d6df67435859a51867f6d832a (patch) | |
tree | b8c58bba74271441fc57a59a2577fcb125e00410 /src | |
parent | e4d6aaec0bebc7d3df0b9c03902cc2e283b6bbb2 (diff) | |
download | kipper-b1e858f8590a676d6df67435859a51867f6d832a.tar.bz2 |
CommitRef::new(): Return `Result`
Eliminate `.unwrap()` by returning a `Result`. The
`JsonValue::take_string`s return `Options`, so if they're `None`, just
fill the struct with the default for `String`, which is `""` empty
string.
Diffstat (limited to 'src')
-rw-r--r-- | src/pull_request.rs | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/pull_request.rs b/src/pull_request.rs index fc886c7..df4cd2a 100644 --- a/src/pull_request.rs +++ b/src/pull_request.rs @@ -1,5 +1,6 @@ extern crate json; +use std::error::Error; #[derive(Debug)] pub struct CommitRef { @@ -10,15 +11,17 @@ pub struct CommitRef { } impl CommitRef { - pub fn new(json_str: &str) -> CommitRef { - let mut github_push_event = json::parse(json_str).unwrap(); + pub fn new(json_str: &str) -> Result<CommitRef, Box<Error>> { + let mut github_push_event = json::parse(json_str)?; - CommitRef { - owner: github_push_event["pull_request"]["head"]["repo"]["owner"]["login"].take_string().unwrap(), - repo: github_push_event["pull_request"]["head"]["repo"]["name"].take_string().unwrap(), - sha: github_push_event["pull_request"]["head"]["sha"].take_string().unwrap(), - branch: github_push_event["pull_request"]["head"]["ref"].take_string().unwrap(), - } + Ok( + CommitRef { + owner: github_push_event["pull_request"]["head"]["repo"]["owner"]["login"].take_string().unwrap_or_default(), + repo: github_push_event["pull_request"]["head"]["repo"]["name"].take_string().unwrap_or_default(), + sha: github_push_event["pull_request"]["head"]["sha"].take_string().unwrap_or_default(), + branch: github_push_event["pull_request"]["head"]["ref"].take_string().unwrap_or_default(), + } + ) } } |