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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#[cfg(any(feature = "auth"))]
pub mod creation {
    use std::{error::Error as StdError, fmt};

    #[derive(Debug)]
    pub(crate) enum CreationErrorKind {
        #[cfg(feature = "enterprise")]
        BaseUrlWithoutProtocol,
        #[cfg(feature = "enterprise")]
        BaseUrlWithoutApiPath,
        #[cfg(feature = "auth")]
        AuthTokenNotProvided,
        #[cfg(feature = "enterprise")]
        BaseUrlNotProvided,
    }

    #[derive(Debug)]
    pub struct CreationError {
        pub(crate) kind: CreationErrorKind,
    }

    impl CreationError {
        fn new(kind: CreationErrorKind) -> Self {
            Self { kind }
        }

        #[cfg(feature = "enterprise")]
        pub(crate) fn base_url_without_protocol() -> Self {
            Self::new(CreationErrorKind::BaseUrlWithoutProtocol)
        }

        #[cfg(feature = "enterprise")]
        pub(crate) fn base_url_without_api_path() -> Self {
            Self::new(CreationErrorKind::BaseUrlWithoutApiPath)
        }

        #[cfg(feature = "auth")]
        pub(crate) fn auth_token_not_provided() -> Self {
            Self::new(CreationErrorKind::AuthTokenNotProvided)
        }

        #[cfg(feature = "enterprise")]
        pub(crate) fn base_url_not_provided() -> Self {
            Self::new(CreationErrorKind::BaseUrlNotProvided)
        }
    }

    impl StdError for CreationError {}

    impl fmt::Display for CreationError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match &self.kind {
                #[cfg(feature = "enterprise")]
                CreationErrorKind::BaseUrlWithoutProtocol => {
                    write!(f, "Base URL is without the protocol.")
                }

                #[cfg(feature = "enterprise")]
                CreationErrorKind::BaseUrlWithoutApiPath => {
                    write!(f, "Base URL is without the `/api/v3` path at the end.")
                }
                #[cfg(feature = "auth")]
                CreationErrorKind::AuthTokenNotProvided => {
                    write!(f, "Auth token not provided")
                }
                #[cfg(feature = "enterprise")]
                CreationErrorKind::BaseUrlNotProvided => {
                    write!(f, "Base URL is not provided.")
                }
            }
        }
    }
    #[cfg(test)]
    mod tests {
        use crate::CreationError;

        fn assert_sync<T: Sync>() {}
        fn assert_send<T: Send>() {}

        #[test]
        fn test_send_and_sync() {
            assert_sync::<CreationError>();
            assert_send::<CreationError>();
        }
    }
}

pub mod runtime {
    use std::{error::Error as StdError, fmt};

    #[derive(Debug)]
    pub(crate) enum RuntimeErrorKind {
        #[cfg(feature = "auth")]
        BadCredentials,
        NotFound,
    }

    #[derive(Debug)]
    pub struct RuntimeError {
        pub(crate) kind: RuntimeErrorKind,
    }

    impl RuntimeError {
        fn new(kind: RuntimeErrorKind) -> Self {
            Self { kind }
        }

        #[cfg(feature = "auth")]
        pub(crate) fn bad_credentials() -> Self {
            Self::new(RuntimeErrorKind::BadCredentials)
        }

        pub(crate) fn not_found() -> Self {
            Self::new(RuntimeErrorKind::NotFound)
        }
    }

    impl StdError for RuntimeError {}

    impl fmt::Display for RuntimeError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match &self.kind {
                #[cfg(feature = "auth")]
                RuntimeErrorKind::BadCredentials => {
                    write!(f, "Bad credentials")
                }
                RuntimeErrorKind::NotFound => {
                    write!(f, "Either the resource does not exist, or it is protected")
                }
            }
        }
    }
    #[cfg(test)]
    mod tests {
        use crate::RuntimeError;

        fn assert_sync<T: Sync>() {}
        fn assert_send<T: Send>() {}

        #[test]
        fn test_send_and_sync() {
            assert_sync::<RuntimeError>();
            assert_send::<RuntimeError>();
        }
    }
}