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
//
// 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::{
        config::{CargoConfig, FoundationStorageApiConfig},
        foundation_storage::FoundationStorageApi,
        user::UserApi,
    },
    errors::{SingleErrVariantResult, SingleVariantError},
    logging,
    user::UserService,
};
use std::{
    mem::MaybeUninit,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex,
    },
};
use thiserror::Error;
use wildland_corex::{LocalSecureStorage, LssService};

#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[error("CargoLib creation error: {0}")]
pub struct CargoLibCreationError(pub String);

static INITIALIZED: AtomicBool = AtomicBool::new(false);

type SharedCargoLib = Arc<Mutex<CargoLib>>;
static mut CARGO_LIB: MaybeUninit<SharedCargoLib> = MaybeUninit::uninit();

/// Structure aggregating and exposing public API of CargoLib library.
/// All functionalities are exposed to application side through this structure.
///
/// It can be created with [`create_cargo_lib`] function.
///
#[derive(Clone)]
pub struct CargoLib {
    user_api: UserApi,
    foundation_storage_api: FoundationStorageApi,
}

impl CargoLib {
    pub fn new(
        lss: &'static dyn LocalSecureStorage,
        fsa_config: FoundationStorageApiConfig,
    ) -> Self {
        let lss_service = LssService::new(lss);
        Self {
            user_api: UserApi::new(UserService::new(lss_service.clone())),
            foundation_storage_api: FoundationStorageApi::new(fsa_config, lss_service),
        }
    }

    /// Returns structure aggregating API for user management
    #[tracing::instrument(skip(self))]
    pub fn user_api(&self) -> UserApi {
        self.user_api.clone()
    }

    /// Returns structure aggregating API for Foundation Storage management
    #[tracing::instrument(skip(self))]
    pub fn foundation_storage_api(&self) -> FoundationStorageApi {
        self.foundation_storage_api.clone()
    }
}

/// [`CargoLib`] initializer which is the main part of Cargo public API.
/// All functionalities are exposed to application side through this structure.
///
/// Underlying structure is created only once, subsequent call will return handle to the same structure.
///
/// It requires the following arguments:
/// - lss: some type implementing [`LocalSecureStorage`] trait. It is usually provided by the native
/// to a target platform language. It is assumed that a lss reference should be valid for a whole
/// program execution (static lifetime).
/// - cfg: [`CargoConfig`] structure with config variables (logger, endpoints, etc.)
///
/// CargoLib expects to get references with static lifetimes so it is important not to inline
/// objects (e.g. LSS) initialization along with createCargoLib call.
///
/// ```
/// # use wildland_corex::{LocalSecureStorage, LssResult};
/// # use wildland_cargo_lib::api::{config::*, cargo_lib::create_cargo_lib};
/// # use tracing::Level;
/// #
/// struct TestLss{};
///
/// impl LocalSecureStorage for TestLss {
/// // ...implementation here
/// #    fn insert(&self, key: String, value: Vec<u8>) -> LssResult<Option<Vec<u8>>>{todo!()}
/// #    fn get(&self, key: String) -> LssResult<Option<Vec<u8>>>{todo!()}
/// #    fn contains_key(&self, key: String) -> LssResult<bool>{todo!()}
/// #    fn keys(&self) -> LssResult<Vec<String>>{todo!()}
/// #    fn remove(&self, key: String) -> LssResult<Option<Vec<u8>>>{todo!()}
/// #    fn len(&self) -> LssResult<usize>{todo!()}
/// #    fn is_empty(&self) -> LssResult<bool>{todo!()}
/// }
///
/// let lss = TestLss{};
///
/// # use std::path::PathBuf;
/// let cfg = CargoConfig{     
///     logger_config: LoggerConfig {
///         use_logger: true,
///         log_level: Level::TRACE,
///         log_use_ansi: false,
///         log_file_path: PathBuf::from("cargo_lib_log"),
///         log_file_rotate_directory: PathBuf::from(".".to_owned()),
///         log_file_enabled: true,
///         oslog_category: None,
///         oslog_subsystem: None,
///     },
///     fsa_config: FoundationStorageApiConfig {
///         evs_url: "some_url".to_owned(),
///         sc_url: "some_url".to_owned(),
///     },
/// };
///
/// let lss: &'static TestLss = unsafe { std::mem::transmute(&lss) };
/// let cargo_lib = create_cargo_lib(lss, cfg);
/// ```
pub fn create_cargo_lib(
    lss: &'static dyn LocalSecureStorage,
    cfg: CargoConfig,
) -> SingleErrVariantResult<SharedCargoLib, CargoLibCreationError> {
    if !INITIALIZED.load(Ordering::Relaxed) {
        INITIALIZED.store(true, Ordering::Relaxed);

        logging::init_subscriber(cfg.logger_config)
            .map_err(|e| SingleVariantError::Failure(CargoLibCreationError(e.to_string())))?;

        let cargo_lib = Arc::new(Mutex::new(CargoLib::new(lss, cfg.fsa_config)));

        unsafe {
            CARGO_LIB.write(cargo_lib);
        }
    }
    unsafe { Ok(CARGO_LIB.assume_init_ref().clone()) }
}