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:
@@ -1,4 +1,6 @@
|
||||
mod github_endpoint_watcher;
|
||||
mod githubctl;
|
||||
mod pull_request_event;
|
||||
|
||||
pub use githubctl::GithubCtl;
|
||||
pub use pull_request_event::PullRequestEvent;
|
||||
|
||||
21
src/githubctl/pull_request_event.rs
Normal file
21
src/githubctl/pull_request_event.rs
Normal 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user