Skip to main content

lab_resource_manager/domain/aggregates/resource_usage/
errors.rs

1use chrono::{DateTime, Utc};
2use std::fmt;
3
4/// ResourceUsage集約のドメインエラー型
5#[derive(Debug, Clone)]
6pub enum ResourceUsageError {
7    /// 無効な時間枠(終了時刻が開始時刻より前)
8    InvalidTimePeriod {
9        /// 開始時刻
10        start: DateTime<Utc>,
11        /// 終了時刻
12        end: DateTime<Utc>,
13    },
14    /// リソース項目が空
15    NoResourceItems,
16    /// リソース使用の競合
17    UsageConflict {
18        /// 競合しているリソース名
19        resource: String,
20        /// 競合しているユーザー
21        conflicting_user: String,
22    },
23}
24
25impl fmt::Display for ResourceUsageError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            ResourceUsageError::InvalidTimePeriod { start, end } => {
29                write!(
30                    f,
31                    "無効な時間枠: 終了時刻({})は開始時刻({})よりあとである必要があります。",
32                    end.format("%Y-%m-%d %H:%M:%S"),
33                    start.format("%Y-%m-%d %H:%M:%S")
34                )
35            }
36            ResourceUsageError::NoResourceItems => {
37                write!(f, "資源項目エラー: 少なくとも1つの資源項目が必要です")
38            }
39            ResourceUsageError::UsageConflict {
40                resource,
41                conflicting_user,
42            } => {
43                write!(
44                    f,
45                    "使用予定の競合: {}が{}の使用予定と競合しています",
46                    resource, conflicting_user
47                )
48            }
49        }
50    }
51}
52
53impl std::error::Error for ResourceUsageError {}
54
55impl crate::domain::errors::DomainError for ResourceUsageError {}