use std::convert::TryInto;
use octocrab::models::{Repository, User, issues::Comment, issues::Issue};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Payload {
pub action: Action,
pub sender: User,
pub repository: Repository,
pub comment: Option<Comment>,
pub issue: Option<Issue>,
}
pub struct IssueCommentPayload {
pub action: Action,
pub sender: User,
pub issue: Issue,
pub comment: Comment,
pub repository: Repository,
}
impl TryInto<IssueCommentPayload> for Payload {
type Error = Error;
fn try_into(self) -> Result<IssueCommentPayload, Self::Error> {
let comment = self.comment.ok_or(Error::MissingCommentPayload)?;
let issue = self.issue.ok_or(Error::MissingIssuePayload)?;
Ok(IssueCommentPayload {
action: self.action,
sender: self.sender,
repository: self.repository,
comment,
issue,
})
}
}
#[derive(thiserror::Error, Clone, Debug)]
pub enum Error {
#[error("Expected field \"comment\" not found in webhook payload")]
MissingCommentPayload,
#[error("Expected field \"issue\" not found in webhook payload")]
MissingIssuePayload,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Action {
Created,
Edited,
Deleted,
}