From be9dcee422b21d17abb245b19d9bea1d44e5e432 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 12 May 2022 08:37:15 -0400 Subject: [PATCH] Move the handling of events into a struct. I am going to start parsing fields out of the json event objects, so it makes sense to give each event its own type to avoid the verbosity of parsing the values out of the json object every time they are needed. --- src/githubctl/mod.rs | 2 ++ src/githubctl/pull_request_event.rs | 21 ++++++++++++++++ src/json_util.rs | 23 ++++++++++++++++++ src/main.rs | 37 ++++++----------------------- 4 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 src/githubctl/pull_request_event.rs create mode 100644 src/json_util.rs 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"), - } -}