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.
This commit is contained in:
Tom Alexander 2022-05-12 08:37:15 -04:00
parent f7d2a2e57d
commit be9dcee422
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 53 additions and 30 deletions

View File

@ -1,4 +1,6 @@
mod github_endpoint_watcher;
mod githubctl;
mod pull_request_event;
pub use githubctl::GithubCtl;
pub use pull_request_event::PullRequestEvent;

View File

@ -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<Self, Box<dyn std::error::Error>> {
Ok(PullRequestEvent { original_event })
}
pub fn is_a(event: &'a serde_json::Value) -> Result<bool, Box<dyn std::error::Error>> {
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),
}
}
}

23
src/json_util.rs Normal file
View File

@ -0,0 +1,23 @@
use serde_json::{self, Map};
pub fn get_json_object(obj: &serde_json::Value) -> &Map<String, serde_json::Value> {
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"),
}
}

View File

@ -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<dyn std::error::Error>> {
}
fn handle_event(event: serde_json::Value) -> Result<(), Box<dyn std::error::Error>> {
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<String, serde_json::Value> {
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"),
}
}