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
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::fmt::Display;

use thiserror::Error;
use wildland_corex::catlib_service::error::{CatlibError, CatlibResult};
use wildland_corex::{
    CryptoError,
    ErrContext,
    ForestIdentityCreationError,
    ForestRetrievalError,
    LssError,
};

use crate::api::cargo_user::AutomountError;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum CreateMnemonicError {
    #[error("Invalid Mnemonic words")]
    InvalidMnemonicWords,
}

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum UserCreationError {
    #[error("User already exists")]
    UserAlreadyExists,
    #[error("Could not check if user already exists: {0}")]
    UserRetrievalError(UserRetrievalError),
    #[error("{0}: Crypto error: {1}")]
    CryptoError(String, CryptoError),
    #[error("Could not create a new forest identity: {0}")]
    ForestIdentityCreationError(#[from] ForestIdentityCreationError),
    #[error("{0}: LSS error: {1}")]
    LssError(String, LssError),
    #[error("{0}: CatLib error: {1}")]
    CatlibError(String, CatlibError),
}

impl<T> ErrContext<UserCreationError, T> for Result<T, CryptoError> {
    fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
        self.map_err(|e| UserCreationError::CryptoError(ctx.to_string(), e))
    }
}

impl<T> ErrContext<UserCreationError, T> for Result<T, LssError> {
    fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
        self.map_err(|e| UserCreationError::LssError(ctx.to_string(), e))
    }
}

impl<T> ErrContext<UserCreationError, T> for CatlibResult<T> {
    fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
        self.map_err(|e| UserCreationError::CatlibError(ctx.to_string(), e))
    }
}

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum UserRetrievalError {
    #[error(transparent)]
    ForestRetrievalError(#[from] ForestRetrievalError),
    #[error("Default forest not found in LSS: {0}")]
    ForestNotFound(String),
    #[error(transparent)]
    LssError(#[from] LssError),
    #[error(transparent)]
    CatlibError(#[from] CatlibError),
    #[error("Metadata of this device has not been found in Forest")]
    DeviceMetadataNotFound,
    #[error("User not found")]
    UserNotFound,
    #[error("{0}: CargoUser inner error: {1}")]
    AutomountError(String, AutomountError),
}

impl<T> ErrContext<UserRetrievalError, T> for Result<T, AutomountError> {
    fn context(self, ctx: impl Display) -> Result<T, UserRetrievalError> {
        self.map_err(|e| UserRetrievalError::AutomountError(ctx.to_string(), e))
    }
}