diff --git a/src/githubctl/mod.rs b/src/githubctl/mod.rs index 9a4f7de..020df61 100644 --- a/src/githubctl/mod.rs +++ b/src/githubctl/mod.rs @@ -1,4 +1,6 @@ mod github_endpoint_watcher; mod githubctl; +mod pull_request_event; pub use githubctl::GithubCtl; +pub use pull_request_event::PullRequestEvent; diff --git a/src/githubctl/pull_request_event.rs b/src/githubctl/pull_request_event.rs new file mode 100644 index 0000000..daad435 --- /dev/null +++ b/src/githubctl/pull_request_event.rs @@ -0,0 +1,21 @@ +use crate::json_util::get_json_string; + +pub struct PullRequestEvent<'a> { + original_event: &'a serde_json::Value, +} + +impl<'a> PullRequestEvent<'a> { + pub fn new(original_event: &'a serde_json::Value) -> Result> { + Ok(PullRequestEvent { original_event }) + } + + pub fn is_a(event: &'a serde_json::Value) -> Result> { + let event_type = event.get("type").map(get_json_string); + match event_type { + Some(event_type_string) if event_type_string == "PullRequestEvent" => { + return Ok(true); + } + _ => Ok(false), + } + } +} diff --git a/src/json_util.rs b/src/json_util.rs new file mode 100644 index 0000000..f4ce6ba --- /dev/null +++ b/src/json_util.rs @@ -0,0 +1,23 @@ +use serde_json::{self, Map}; + +pub fn get_json_object(obj: &serde_json::Value) -> &Map { + match obj { + serde_json::Value::Null => panic!("Unexpected json type"), + serde_json::Value::Bool(_) => panic!("Unexpected json type"), + serde_json::Value::Number(_) => panic!("Unexpected json type"), + serde_json::Value::String(_) => panic!("Unexpected json type"), + serde_json::Value::Array(_) => panic!("Unexpected json type"), + serde_json::Value::Object(event_object) => event_object, + } +} + +pub fn get_json_string(str: &serde_json::Value) -> &String { + match str { + serde_json::Value::Null => panic!("Unexpected json type"), + serde_json::Value::Bool(_) => panic!("Unexpected json type"), + serde_json::Value::Number(_) => panic!("Unexpected json type"), + serde_json::Value::String(txt) => txt, + serde_json::Value::Array(_) => panic!("Unexpected json type"), + serde_json::Value::Object(_) => panic!("Unexpected json type"), + } +} diff --git a/src/main.rs b/src/main.rs index ee158a9..9ea715b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,10 @@ extern crate log; mod githubctl; +mod json_util; -use serde_json::{self, Map}; +use githubctl::PullRequestEvent; +use serde_json; const USERNAME: &'static str = include_str!("../.username"); const TOKEN: &'static str = include_str!("../.pat"); @@ -23,35 +25,10 @@ async fn main() -> Result<(), Box> { } fn handle_event(event: serde_json::Value) -> Result<(), Box> { - let event_object = get_json_object(&event); - let event_type = event_object.get("type").map(get_json_string); - match event_type { - Some(event_type_string) if event_type_string == "PullRequestEvent" => { - println!("{}", serde_json::to_string(&event)?); - } - _ => {} + let event = if PullRequestEvent::is_a(&event)? { + Some(PullRequestEvent::new(&event)?) + } else { + None }; Ok(()) } - -fn get_json_object(obj: &serde_json::Value) -> &Map { - match obj { - serde_json::Value::Null => panic!("Unexpected json type"), - serde_json::Value::Bool(_) => panic!("Unexpected json type"), - serde_json::Value::Number(_) => panic!("Unexpected json type"), - serde_json::Value::String(_) => panic!("Unexpected json type"), - serde_json::Value::Array(_) => panic!("Unexpected json type"), - serde_json::Value::Object(event_object) => event_object, - } -} - -fn get_json_string(str: &serde_json::Value) -> &String { - match str { - serde_json::Value::Null => panic!("Unexpected json type"), - serde_json::Value::Bool(_) => panic!("Unexpected json type"), - serde_json::Value::Number(_) => panic!("Unexpected json type"), - serde_json::Value::String(txt) => txt, - serde_json::Value::Array(_) => panic!("Unexpected json type"), - serde_json::Value::Object(_) => panic!("Unexpected json type"), - } -}