Skip to main content

lab_resource_manager/domain/ports/
notifier.rs

1// NOTE: これ以上肥大化するようであればnotifierディレクトリを作成してその中に適宜分割する
2use crate::domain::{
3    aggregates::resource_usage::entity::ResourceUsage, errors::DomainError, ports::PortError,
4};
5use async_trait::async_trait;
6use std::fmt;
7
8/// 通知イベントの種類
9#[derive(Debug, Clone)]
10pub enum NotificationEvent {
11    /// リソース使用予定が作成された
12    ResourceUsageCreated(ResourceUsage),
13    /// リソース使用予定が更新された
14    ResourceUsageUpdated(ResourceUsage),
15    /// リソース使用予定が削除された
16    ResourceUsageDeleted(ResourceUsage),
17}
18
19/// 通知サービスのポート
20#[async_trait]
21pub trait Notifier: Send + Sync {
22    /// イベントを通知する
23    async fn notify(&self, event: NotificationEvent) -> Result<(), NotificationError>;
24}
25
26/// 通知エラー
27#[derive(Debug)]
28pub enum NotificationError {
29    /// 通知送信の失敗
30    SendFailure(String),
31    /// リポジトリエラー(IdentityLink取得失敗等)
32    RepositoryError(String),
33}
34
35impl fmt::Display for NotificationError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            NotificationError::SendFailure(msg) => write!(f, "通知送信エラー: {}", msg),
39            NotificationError::RepositoryError(msg) => {
40                write!(f, "通知準備中のリポジトリエラー: {}", msg)
41            }
42        }
43    }
44}
45
46impl std::error::Error for NotificationError {}
47impl DomainError for NotificationError {}
48impl PortError for NotificationError {}