1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use super::base::{ErrorType, MappedErrors};

/// A factory for creation errors
pub fn creation_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::CreationError)
}

/// A factory for updating errors
pub fn updating_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::UpdatingError)
}

/// A factory for fetching errors
pub fn fetching_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::FetchingError)
}

/// A factory for deletion errors
pub fn deletion_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::DeletionError)
}

/// A factory for use case errors
pub fn use_case_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::UseCaseError)
}

/// A factory for execution errors
pub fn execution_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::ExecutionError)
}

/// A factory for invalid repository errors
pub fn invalid_repo_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg)
        .with_error_type(ErrorType::InvalidRepositoryError)
}

/// A factory for invalid argument errors
pub fn invalid_arg_err(msg: String) -> MappedErrors {
    MappedErrors::default(msg).with_error_type(ErrorType::InvalidArgumentError)
}

// * ---------------------------------------------------------------------------
// * TESTS
// * ---------------------------------------------------------------------------

#[cfg(test)]
mod test {
    use super::*;
    use crate::utils::errors::{base::ErrorType, ErrorCode};

    #[test]
    fn test_default_factories() {
        assert_eq!(
            creation_err("create".to_string()).error_type(),
            ErrorType::CreationError
        );

        assert_eq!(
            updating_err("update".to_string()).error_type(),
            ErrorType::UpdatingError
        );

        assert_eq!(
            fetching_err("fetch".to_string()).error_type(),
            ErrorType::FetchingError
        );

        assert_eq!(
            deletion_err("delete".to_string()).error_type(),
            ErrorType::DeletionError
        );

        assert_eq!(
            use_case_err("use_case".to_string()).error_type(),
            ErrorType::UseCaseError
        );

        assert_eq!(
            execution_err("execution".to_string()).error_type(),
            ErrorType::ExecutionError
        );

        assert_eq!(
            invalid_repo_err("invalid_repo".to_string()).error_type(),
            ErrorType::InvalidRepositoryError
        );

        assert_eq!(
            invalid_arg_err("invalid_arg".to_string()).error_type(),
            ErrorType::InvalidArgumentError
        );
    }

    #[test]
    fn test_creation_error_factory() {
        fn result_function() -> Result<String, MappedErrors> {
            creation_err("create".to_string())
                .with_code("ID001".to_string())
                .as_error()
        }

        let result = result_function().unwrap_err();

        assert!(result.code() == ErrorCode::Code("ID001".to_string()));
    }
}