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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};

use futures::future::SharedError;

use asset::AssetSpec;

/// Error type returned when loading an asset.
/// Includes the `AssetSpec` and the error (`LoadError`).
#[derive(Debug)]
pub struct AssetError<A, F, S> {
    /// The specifier of the asset which failed to load
    pub asset: AssetSpec,
    /// The error that's been raised.
    pub error: LoadError<A, F, S>,
}

impl<A, F, S> AssetError<A, F, S> {
    pub(crate) fn new(asset: AssetSpec, error: LoadError<A, F, S>) -> Self {
        AssetError { asset, error }
    }
}

impl<A, F, S> Display for AssetError<A, F, S>
where
    A: Display,
    F: Display,
    S: Display,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(
            f,
            "Failed to load asset \"{}\" of format \"{}\" from storage with id \"{}\": {}",
            &self.asset.name,
            &self.asset.ext,
            &self.asset.store.id(),
            &self.error
        )
    }
}

/// Combined error type which is produced when loading an
/// asset. This error does not include information which asset
/// failed to load. For that, please look at `AssetError`.
#[derive(Clone, Debug)]
pub enum LoadError<A, F, S> {
    /// The conversion from data -> asset failed.
    AssetError(A),
    /// The conversion from bytes -> data failed.
    FormatError(F),
    /// The storage was unable to retrieve the requested data.
    StorageError(S),
}

impl<A, F, S> Display for LoadError<A, F, S>
where
    A: Display,
    F: Display,
    S: Display,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        match *self {
            LoadError::AssetError(ref e) => write!(f, "Failed to load asset: {}", e),
            LoadError::FormatError(ref e) => write!(f, "Failed to load data: {}", e),
            LoadError::StorageError(ref e) => write!(f, "Failed to load from storage: {}", e),
        }
    }
}

impl<A, F, S> Error for AssetError<A, F, S>
where
    A: Error,
    F: Error,
    S: Error,
{
    fn description(&self) -> &str {
        "Failed to load asset"
    }

    fn cause(&self) -> Option<&Error> {
        Some(&self.error)
    }
}

impl<A, F, S> Error for LoadError<A, F, S>
where
    A: Error,
    F: Error,
    S: Error,
{
    fn cause(&self) -> Option<&Error> {
        let cause: &Error = match *self {
            LoadError::AssetError(ref e) => e,
            LoadError::FormatError(ref e) => e,
            LoadError::StorageError(ref e) => e,
        };

        Some(cause)
    }

    fn description(&self) -> &str {
        match *self {
            LoadError::AssetError(_) => "Failed to load asset",
            LoadError::FormatError(_) => "Failed to load data",
            LoadError::StorageError(_) => "Failed to load from storage",
        }
    }
}

/// An error type which cannot be instantiated.
/// Used as a placeholder for associated error types if
/// something cannot fail.
#[derive(Debug)]
pub enum NoError {}

impl Display for NoError {
    fn fmt(&self, _: &mut Formatter) -> FmtResult {
        match *self {}
    }
}

impl Error for NoError {
    fn description(&self) -> &str {
        match *self {}
    }
}


/// Shared version of error
pub struct SharedAssetError<E>(SharedError<E>);

impl<E> AsRef<E> for SharedAssetError<E> {
    fn as_ref(&self) -> &E {
        &*self.0
    }
}

impl<E> Error for SharedAssetError<E>
where
    E: Error,
{
    fn description(&self) -> &str {
        self.as_ref().description()
    }

    fn cause(&self) -> Option<&Error> {
        self.as_ref().cause()
    }
}

impl<E> Debug for SharedAssetError<E>
where
    E: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.as_ref().fmt(f)
    }
}

impl<E> Display for SharedAssetError<E>
where
    E: Display,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.as_ref().fmt(f)
    }
}

impl<E> From<SharedError<E>> for SharedAssetError<E> {
    fn from(err: SharedError<E>) -> Self {
        SharedAssetError(err)
    }
}