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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// 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 crate::{
    api::{cargo_lib::*, config::*, foundation_storage::*, user::*},
    errors::*,
};
use rusty_bind::binding_wrapper;
use std::sync::{Arc, Mutex};
pub use wildland_corex::{
    storage::StorageTemplate, CoreXError, CryptoError, ForestRetrievalError, LocalSecureStorage,
    LssError, LssResult,
};

type VoidType = ();

pub type UserRetrievalExc = RetrievalError<UserRetrievalError>;
pub type MnemonicCreationExc = SingleVariantError<CryptoError>;
pub type StringExc = SingleVariantError<String>; // Used for simple errors originating inside CargoLib (not in dependant modules)
pub type UserCreationExc = SingleVariantError<UserCreationError>;
pub type CargoLibCreationExc = SingleVariantError<CargoLibCreationError>;
pub type ConfigParseExc = SingleVariantError<ParseConfigError>;
pub type FsaExc = FsaError;

pub type LssOptionalBytesResult = LssResult<Option<Vec<u8>>>;
/// constructor of `LssResult<Option<Vec<u8>>>` (aka [`LssOptionalBytesResult`]) with Ok variant
pub fn new_ok_lss_optional_bytes(ok_val: OptionalBytes) -> LssOptionalBytesResult {
    Ok(ok_val)
}
/// constructor of `LssResult<Option<Vec<u8>>>` (aka [`LssOptionalBytesResult`]) with Err variant
pub fn new_err_lss_optional_bytes(err_val: String) -> LssOptionalBytesResult {
    Err(LssError(err_val))
}

pub type LssBoolResult = LssResult<bool>;
/// constructor of `LssResult<bool>` (aka [`LssBoolResult`]) with Ok variant
pub fn new_ok_lss_bool(ok_val: bool) -> LssBoolResult {
    Ok(ok_val)
}
/// constructor of `LssResult<bool>` (aka [`LssBoolResult`]) with Err variant
pub fn new_err_lss_bool(err_val: String) -> LssBoolResult {
    Err(LssError(err_val))
}

pub type OptionalBytes = Option<Vec<u8>>;
/// constructor of `Option<Vec<u8>>` (aka [`OptionalBytes`]) with Some value
pub fn new_some_bytes(bytes: Vec<u8>) -> OptionalBytes {
    Some(bytes)
}
/// constructor of `Option<Vec<u8>>` (aka [`OptionalBytes`]) with None value
pub fn new_none_bytes() -> OptionalBytes {
    None
}

pub type OptionalString = Option<String>;
/// constructor of `Option<String>` (aka [`OptionalString`]) with Some value
fn new_some_string(s: String) -> OptionalString {
    Some(s)
}
/// constructor of `Option<String>` (aka [`OptionalString`]) with None value
fn new_none_string() -> OptionalString {
    None
}

pub type LssVecOfStringsResult = LssResult<Vec<String>>;
/// constructor of `LssResult<Vec<String>>` (aka [`LssVecOfStringsResult`]) with Ok variant
fn new_ok_lss_vec_of_strings(ok_val: Vec<String>) -> LssVecOfStringsResult {
    Ok(ok_val)
}
/// constructor of `LssResult<Vec<String>>` (aka [`LssVecOfStringsResult`]) with Err variant
fn new_err_lss_vec_of_strings(err_val: String) -> LssVecOfStringsResult {
    Err(LssError(err_val))
}

pub type LssUsizeResult = LssResult<usize>;
/// constructor of `LssResult<usize>` (aka [`LssUsizeResult`]) with Ok variant
fn new_ok_lss_usize(ok_val: usize) -> LssUsizeResult {
    Ok(ok_val)
}
/// constructor of `LssResult<usize>` (aka [`LssUsizeResult`]) with Err variant
pub fn new_err_lss_usize(err_val: String) -> LssUsizeResult {
    Err(LssError(err_val))
}

#[binding_wrapper]
mod ffi_binding {
    extern "ExceptionTrait" {
        fn reason(&self) -> String;
    }
    enum UserRetrievalExc {
        NotFound(_),
        Unexpected(_),
    }
    enum MnemonicCreationExc {
        Failure(_),
    }
    enum UserCreationExc {
        Failure(_),
    }
    enum CargoLibCreationExc {
        Failure(_),
    }
    enum ConfigParseExc {
        Failure(_),
    }
    enum FsaExc {
        StorageAlreadyExists,
        EvsError(_),
        CryptoError(_),
        InvalidCredentialsFormat(_),
    }
    enum StringExc {
        Failure(_),
    }

    extern "Traits" {

        // # traits required for main configuration
        //
        fn get_evs_url(self: &dyn CargoCfgProvider) -> String;
        fn get_sc_url(self: &dyn CargoCfgProvider) -> String;

        // # traits required for logging configuration
        //
        fn get_use_logger(self: &dyn CargoCfgProvider) -> bool;
        fn get_log_level(self: &dyn CargoCfgProvider) -> String;
        fn get_log_use_ansi(self: &dyn CargoCfgProvider) -> bool;
        fn get_log_file_enabled(self: &dyn CargoCfgProvider) -> bool;
        fn get_log_file_path(self: &dyn CargoCfgProvider) -> OptionalString;
        fn get_log_file_rotate_directory(self: &dyn CargoCfgProvider) -> OptionalString;
        fn get_oslog_category(self: &dyn CargoCfgProvider) -> OptionalString;
        fn get_oslog_subsystem(self: &dyn CargoCfgProvider) -> OptionalString;

        // # traits required for lss:
        //
        fn insert(
            self: &dyn LocalSecureStorage,
            key: String,
            value: Vec<u8>,
        ) -> LssOptionalBytesResult;
        fn get(self: &dyn LocalSecureStorage, key: String) -> LssOptionalBytesResult;
        fn contains_key(self: &dyn LocalSecureStorage, key: String) -> LssBoolResult;
        fn keys(self: &dyn LocalSecureStorage) -> LssVecOfStringsResult;
        fn remove(self: &dyn LocalSecureStorage, key: String) -> LssOptionalBytesResult;
        fn len(self: &dyn LocalSecureStorage) -> LssUsizeResult;
        fn is_empty(self: &dyn LocalSecureStorage) -> LssBoolResult;
    }

    extern "Rust" {
        type VoidType;

        type LssOptionalBytesResult;
        fn new_ok_lss_optional_bytes(ok_val: OptionalBytes) -> LssOptionalBytesResult;
        fn new_err_lss_optional_bytes(err_val: String) -> LssOptionalBytesResult;
        type LssBoolResult;
        fn new_ok_lss_bool(ok_val: bool) -> LssBoolResult;
        fn new_err_lss_bool(err_val: String) -> LssBoolResult;
        type LssVecOfStringsResult;
        fn new_ok_lss_vec_of_strings(ok_val: Vec<String>) -> LssVecOfStringsResult;
        fn new_err_lss_vec_of_strings(err_val: String) -> LssVecOfStringsResult;
        type LssUsizeResult;
        fn new_ok_lss_usize(ok_val: usize) -> LssUsizeResult;
        fn new_err_lss_usize(err_val: String) -> LssUsizeResult;

        type OptionalBytes;
        fn new_some_bytes(bytes: Vec<u8>) -> OptionalBytes;
        fn new_none_bytes() -> OptionalBytes;
        type OptionalString;
        fn new_some_string(s: String) -> OptionalString;
        fn new_none_string() -> OptionalString;

        type CargoConfig;
        fn parse_config(raw_content: Vec<u8>) -> Result<CargoConfig, ConfigParseExc>;
        fn collect_config(
            config_provider: &'static dyn CargoCfgProvider,
        ) -> Result<CargoConfig, ConfigParseExc>;

        fn create_cargo_lib(
            lss: &'static dyn LocalSecureStorage,
            config: CargoConfig,
        ) -> Result<Arc<Mutex<CargoLib>>, CargoLibCreationExc>;
        fn user_api(self: &Arc<Mutex<CargoLib>>) -> UserApi;
        fn foundation_storage_api(self: &Arc<Mutex<CargoLib>>) -> FoundationStorageApi;

        fn request_free_tier_storage(
            self: &FoundationStorageApi,
            email: String,
        ) -> Result<FreeTierProcessHandle, FsaExc>;
        fn verify_email(
            self: &FoundationStorageApi,
            process_handle: &FreeTierProcessHandle,
            verification_token: String,
        ) -> Result<StorageTemplate, FsaExc>;
        type FreeTierProcessHandle;

        type StorageTemplate;

        fn generate_mnemonic(self: &UserApi) -> Result<MnemonicPayload, MnemonicCreationExc>;
        fn create_mnemonic_from_vec(
            self: &UserApi,
            words: Vec<String>,
        ) -> Result<MnemonicPayload, StringExc>;
        fn create_user_from_entropy(
            self: &UserApi,
            entropy: Vec<u8>,
            device_name: String,
        ) -> Result<CargoUser, UserCreationExc>;
        fn create_user_from_mnemonic(
            self: &UserApi,
            mnemonic: &MnemonicPayload,
            device_name: String,
        ) -> Result<CargoUser, UserCreationExc>;
        fn get_user(self: &UserApi) -> Result<CargoUser, UserRetrievalExc>;

        fn get_string(self: &MnemonicPayload) -> String;
        fn get_vec(self: &MnemonicPayload) -> Vec<String>;

        fn get_string(self: &CargoUser) -> String;
    }
}