lab_resource_manager/domain/services/resource_usage/
errors.rs

1//! リソース使用ドメインサービスのエラー
2
3use crate::domain::aggregates::resource_usage::value_objects::UsageId;
4use crate::domain::errors::DomainError;
5use crate::domain::ports::repositories::RepositoryError;
6use std::fmt;
7
8/// リソース競合エラー
9#[derive(Debug)]
10pub struct ResourceConflictError {
11    /// 競合しているリソースの説明
12    pub resource_description: String,
13    /// 競合している既存の使用予定ID
14    pub conflicting_usage_id: UsageId,
15}
16
17impl ResourceConflictError {
18    pub fn new(resource_description: impl Into<String>, conflicting_usage_id: UsageId) -> Self {
19        Self {
20            resource_description: resource_description.into(),
21            conflicting_usage_id,
22        }
23    }
24}
25
26impl fmt::Display for ResourceConflictError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(
29            f,
30            "リソース競合: {} (競合する予約ID: {})",
31            self.resource_description,
32            self.conflicting_usage_id.as_str()
33        )
34    }
35}
36
37impl std::error::Error for ResourceConflictError {}
38
39impl DomainError for ResourceConflictError {}
40
41/// 競合チェックで発生するエラー
42#[derive(Debug)]
43pub enum ConflictCheckError {
44    /// リソース競合
45    Conflict(ResourceConflictError),
46    /// リポジトリエラー
47    Repository(RepositoryError),
48}
49
50impl fmt::Display for ConflictCheckError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            ConflictCheckError::Conflict(e) => write!(f, "{}", e),
54            ConflictCheckError::Repository(e) => write!(f, "{}", e),
55        }
56    }
57}
58
59impl std::error::Error for ConflictCheckError {
60    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61        match self {
62            ConflictCheckError::Conflict(e) => Some(e),
63            ConflictCheckError::Repository(e) => Some(e),
64        }
65    }
66}
67
68impl From<RepositoryError> for ConflictCheckError {
69    fn from(e: RepositoryError) -> Self {
70        ConflictCheckError::Repository(e)
71    }
72}
73
74impl DomainError for ConflictCheckError {}