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
use crate::runtime::EnvError;
use crate::types::api::APIError;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

// TODO move this to runtime::msg::Error and rename it
#[derive(Clone, PartialEq, Eq, Serialize, Debug)]
#[serde(tag = "type")]
pub enum CtxError {
    API(APIError),
    Env(EnvError),
    Other(OtherError),
}

impl From<APIError> for CtxError {
    fn from(error: APIError) -> Self {
        CtxError::API(error)
    }
}

impl From<EnvError> for CtxError {
    fn from(error: EnvError) -> Self {
        CtxError::Env(error)
    }
}

impl From<OtherError> for CtxError {
    fn from(error: OtherError) -> Self {
        CtxError::Other(error)
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum OtherError {
    UserNotLoggedIn,
    LibraryItemNotFound,
    AddonAlreadyInstalled,
    AddonNotInstalled,
    AddonIsProtected,
    AddonConfigurationRequired,
    UserAddonsAreLocked,
    UserLibraryIsMissing,
}

impl OtherError {
    pub fn message(&self) -> String {
        match &self {
            OtherError::UserNotLoggedIn => "User is not logged in".to_owned(),
            OtherError::LibraryItemNotFound => "Item is not found in library".to_owned(),
            OtherError::AddonAlreadyInstalled => "Addon is already installed".to_owned(),
            OtherError::AddonNotInstalled => "Addon is not installed".to_owned(),
            OtherError::AddonIsProtected => "Addon is protected".to_owned(),
            OtherError::AddonConfigurationRequired => "Addon requires configuration".to_owned(),
            OtherError::UserAddonsAreLocked => "Fetching Addons from the API failed and we have defaulted the addons to the officials ones until the request succeeds".to_owned(),
            OtherError::UserLibraryIsMissing => "Fetching Library from the API failed and we have defaulted to empty library until the request succeeds".to_owned(),
        }
    }
    pub fn code(&self) -> u64 {
        match &self {
            OtherError::UserNotLoggedIn => 1,
            OtherError::LibraryItemNotFound => 2,
            OtherError::AddonAlreadyInstalled => 3,
            OtherError::AddonNotInstalled => 4,
            OtherError::AddonIsProtected => 5,
            OtherError::AddonConfigurationRequired => 6,
            OtherError::UserAddonsAreLocked => 7,
            OtherError::UserLibraryIsMissing => 8,
        }
    }
}

impl Serialize for OtherError {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("OtherError", 2)?;
        state.serialize_field("code", &self.code())?;
        state.serialize_field("message", &self.message())?;
        state.end()
    }
}