Skip to main content

lgui_services/notification/
contract.rs

1use std::{fmt, sync::Arc};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub struct Notification {
5    pub title: String,
6    pub body: String,
7}
8
9impl Notification {
10    pub fn new(title: impl Into<String>, body: impl Into<String>) -> Self {
11        Self {
12            title: title.into(),
13            body: body.into(),
14        }
15    }
16}
17
18pub trait NotificationService: Send + Sync + 'static {
19    type Error: std::error::Error + Send + Sync + 'static;
20
21    fn show(&self, notification: &Notification) -> Result<(), Self::Error>;
22}
23
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct NotificationError(String);
26
27impl NotificationError {
28    pub fn new(message: impl Into<String>) -> Self {
29        Self(message.into())
30    }
31}
32
33impl fmt::Display for NotificationError {
34    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35        formatter.write_str(&self.0)
36    }
37}
38
39impl std::error::Error for NotificationError {}
40
41#[derive(Clone)]
42pub struct NotificationHandle {
43    show: Arc<dyn Fn(&Notification) -> Result<(), NotificationError> + Send + Sync>,
44}
45
46impl NotificationHandle {
47    pub fn new(
48        show: impl Fn(&Notification) -> Result<(), NotificationError> + Send + Sync + 'static,
49    ) -> Self {
50        Self {
51            show: Arc::new(show),
52        }
53    }
54
55    pub fn show(&self, notification: &Notification) -> Result<(), NotificationError> {
56        (self.show)(notification)
57    }
58}