From b1e858f8590a676d6df67435859a51867f6d832a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 11 Nov 2017 21:13:46 +0100 Subject: 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. --- src/pull_request.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src') 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> { + 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(), + } + ) } } -- cgit v1.2.3