#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum CloseReason {
Expired,
Dismissed,
CloseAction,
Other(u32),
}
impl From<u32> for CloseReason {
fn from(raw_reason: u32) -> Self {
match raw_reason {
1 => CloseReason::Expired,
2 => CloseReason::Dismissed,
3 => CloseReason::CloseAction,
other => CloseReason::Other(other),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum NotificationResponse {
Default,
Action(String),
Reply(String),
Closed(CloseReason),
}
impl NotificationResponse {
pub fn is_default_action(&self) -> bool {
matches!(self, NotificationResponse::Default)
}
}
impl From<String> for NotificationResponse {
fn from(key: String) -> Self {
Self::Action(key)
}
}
impl From<&str> for NotificationResponse {
fn from(key: &str) -> Self {
Self::Action(key.to_owned())
}
}
#[derive(Clone, Debug)]
pub enum ActionResponse<'a> {
Custom(&'a str),
Closed(CloseReason),
}
impl<'a> From<&'a str> for ActionResponse<'a> {
fn from(raw: &'a str) -> Self {
Self::Custom(raw)
}
}
pub trait ResponseHandler {
fn call(self, response: &NotificationResponse);
}
impl<F> ResponseHandler for F
where
F: FnOnce(&NotificationResponse),
{
fn call(self, response: &NotificationResponse) {
(self)(response);
}
}
pub trait CloseHandler<T> {
fn call(&self, reason: CloseReason);
}
impl<F> CloseHandler<CloseReason> for F
where
F: Fn(CloseReason),
{
fn call(&self, reason: CloseReason) {
self(reason);
}
}
impl<F> CloseHandler<()> for F
where
F: Fn(),
{
fn call(&self, _reason: CloseReason) {
self();
}
}