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
//! APIs for interacting with EDJX KV-Store.

use crate::error::Error as ErrorCode;
use num_traits::FromPrimitive;
use std::time::Duration;
use thiserror::Error;

#[link(wasm_import_module = "kv")]
extern "C" {
    fn host_kv_get(
        key_ptr: *const u8,
        key_len: u32,
        sd_ptr: *mut u32,
        err_code_ptr: *mut i32,
    ) -> i32;
    fn host_kv_put(
        key_ptr: *const u8,
        key_len: u32,
        val_ptr: *const u8,
        val_len: u32,
        has_ttl: u32,
        ttl: u64,
        error_code_ptr: *mut i32,
    ) -> i32;
    fn host_kv_delete(key_ptr: *const u8, key_len: u32, error_code_ptr: *mut i32) -> i32;
}

//used for KV only. Not required for Streaming
#[link(wasm_import_module = "stream")]
extern "C" {
    pub fn _host_stream_type(sd: u32) -> u32;
    pub fn host_stream_read_n(sd: u32, ptr: *mut u8, len: u32) -> u32;
    pub fn _host_stream_is_bound(sd: u32) -> u32;
    pub fn host_stream_size(sd: u32) -> u32;
    pub fn _host_stream_write_n(sd: u32, ptr: *mut u8, len: u32) -> u32;
    pub fn host_stream_drop(sd: u32);
}

const ERROR_UNKNOWN: i32 = 1;
const ERROR_NOT_FOUND: i32 = 2;
const ERROR_UNAUTHORIZED: i32 = 4;

/// Enums describing the errors that correspond to kv modules.
///
/// This is returned by methods similar to
/// [`crate::kv::set()`][`crate::kv::get()`] for
/// error scenarios.
#[derive(Error, Debug)]
pub enum KVError {
    #[error("unknown")]
    /// An unknown error has ocurred while trying to perform a KV store operation.
    Unknown,
    #[error("key not found")]
    /// The value associated with the key could not be found.
    NotFound,
    #[error("Unauthorized")]
    /// This application is not authorized to access this key.
    UnAuthorized,
}

/// Returns the value associated with the provided key.
///
/// # Example
///
/// ```
/// let some_key = "some_key".to_owned();
///
/// let value = match kv::get(&some_key) {
///     Err(e) => {
///         let status = match e {
///             KVError::Unknown => StatusCode::BAD_REQUEST,
///             KVError::UnAuthorized => StatusCode::UNAUTHORIZED,
///             KVError::NotFound  => StatusCode::NOT_FOUND,
///         };
///         return HttpResponse::from(e.to_string()).set_status(status);
///     }
///     Ok(val) => val,
/// };
/// ```
pub fn get<S>(key: S) -> Result<Vec<u8>, KVError>
where
    S: AsRef<str>,
{
    let key_str = key.as_ref();
    let mut sd: u32 = 0;
    let mut err_code = 0;

    let res = unsafe {
        host_kv_get(
            key_str.as_ptr(),
            key_str.len() as u32,
            &mut sd,
            &mut err_code,
        )
    };

    if res < 0 {
        Err(KVError::from(ErrorCode::from_i32(err_code).unwrap()))
    } else {
        let size = unsafe { host_stream_size(sd) };

        let mut val = Vec::with_capacity(size as usize);
        unsafe {
            let n = host_stream_read_n(sd, val.as_mut_ptr(), size);
            val.set_len(n as usize);
            host_stream_drop(sd);
        }

        Ok(val)
    }
}

/// Inserts a key-value pair into the KV store.
///
/// # Example
///
/// ```
/// let some_key = "some_key".to_owned();
/// let some_value = "some_value".to_owned();
///
/// let operation_success : bool = match kv::put(&some_key, some_value, Some(Duration::from_secs(1000 * 5 * 60))) {
///     Err(_) => false,
///     Ok(_) => true,
/// }
///
/// ```
pub fn put<K, V>(key: K, val: V, ttl: Option<Duration>) -> Result<(), KVError>
where
    K: AsRef<str>,
    V: AsRef<[u8]>,
{
    let key_str = key.as_ref();
    let mut err = 0;
    let val_slice = val.as_ref();

    let res = unsafe {
        host_kv_put(
            key_str.as_ptr(),
            key_str.len() as u32,
            val_slice.as_ptr(),
            val_slice.len() as u32,
            ttl.is_some() as u32,
            match ttl {
                Some(d) => d.as_secs(),
                None => 0,
            },
            &mut err,
        )
    };

    if res < 0 {
        Err(KVError::from(ErrorCode::from_i32(err).unwrap()))
    } else {
        Ok(())
    }
}

/// Removes an entry from the KV store.
pub fn delete<K>(key: K) -> Result<(), KVError>
where
    K: AsRef<str>,
{
    let key_str = key.as_ref();
    let mut err = 0;

    let res = unsafe { host_kv_delete(key_str.as_ptr(), key_str.len() as u32, &mut err) };

    if res < 0 {
        Err(KVError::from(ErrorCode::from_i32(err).unwrap()))
    } else {
        Ok(())
    }
}