-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathmanager.rs
112 lines (96 loc) · 3.85 KB
/
manager.rs
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
/*
* Copyright Redis Ltd. 2016 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
use crate::jsonpath::select_value::SelectValue;
use serde_json::Number;
use redis_module::raw::RedisModuleKey;
use redis_module::rediserror::RedisError;
use redis_module::{Context, RedisString};
use crate::Format;
use crate::error::Error;
use crate::commands::KeyValue;
pub struct SetUpdateInfo {
pub path: Vec<String>,
}
pub struct AddUpdateInfo {
pub path: Vec<String>,
pub key: String,
}
pub enum UpdateInfo {
SUI(SetUpdateInfo),
AUI(AddUpdateInfo),
}
pub trait ReadHolder<V: SelectValue> {
fn get_value(&self) -> Result<Option<&V>, RedisError>;
}
pub trait WriteHolder<O: Clone, V: SelectValue> {
fn delete(&mut self) -> Result<(), RedisError>;
fn get_value(&mut self) -> Result<Option<&mut V>, RedisError>;
fn set_value(&mut self, path: Vec<String>, v: O) -> Result<bool, RedisError>;
fn dict_add(&mut self, path: Vec<String>, key: &str, v: O) -> Result<bool, RedisError>;
fn delete_path(&mut self, path: Vec<String>) -> Result<bool, RedisError>;
fn incr_by(&mut self, path: Vec<String>, num: &str) -> Result<Number, RedisError>;
fn mult_by(&mut self, path: Vec<String>, num: &str) -> Result<Number, RedisError>;
fn pow_by(&mut self, path: Vec<String>, num: &str) -> Result<Number, RedisError>;
fn bool_toggle(&mut self, path: Vec<String>) -> Result<bool, RedisError>;
fn str_append(&mut self, path: Vec<String>, val: String) -> Result<usize, RedisError>;
fn arr_append(&mut self, path: Vec<String>, args: Vec<O>) -> Result<usize, RedisError>;
fn arr_insert(
&mut self,
path: Vec<String>,
args: &[O],
index: i64,
) -> Result<usize, RedisError>;
fn arr_pop(&mut self, path: Vec<String>, index: i64) -> Result<Option<String>, RedisError>;
fn arr_trim(&mut self, path: Vec<String>, start: i64, stop: i64) -> Result<usize, RedisError>;
fn clear(&mut self, path: Vec<String>) -> Result<usize, RedisError>;
fn apply_changes(&mut self, ctx: &Context, command: &str) -> Result<(), RedisError>;
}
pub trait Manager {
/* V - SelectValue that the json path library can work on
* O - SelectValue Holder
* Naiv implementation is that V and O are from the same type but its not
* always possible so they are seperated
*/
type V: SelectValue;
type O: Clone;
type WriteHolder: WriteHolder<Self::O, Self::V>;
type ReadHolder: ReadHolder<Self::V>;
fn open_key_read(
&self,
ctx: &Context,
key: &RedisString,
) -> Result<Self::ReadHolder, RedisError>;
fn open_key_write(
&self,
ctx: &Context,
key: RedisString,
) -> Result<Self::WriteHolder, RedisError>;
#[allow(clippy::wrong_self_convention)]
fn from_str(&self, val: &str, format: Format, limit_depth: bool) -> Result<Self::O, Error>;
fn get_memory(v: &Self::V) -> Result<usize, RedisError>;
fn is_json(&self, key: *mut RedisModuleKey) -> Result<bool, RedisError>;
}
pub(crate) fn err_json<V: SelectValue>(value: &V, expected_value: &'static str) -> Error {
Error::from(err_msg_json_expected(
expected_value,
KeyValue::value_name(value),
))
}
pub(crate) fn err_msg_json_expected(expected_value: &'static str, found: &str) -> String {
format!(
"WRONGTYPE wrong type of path value - expected {} but found {}",
expected_value, found
)
}
pub(crate) fn err_msg_json_path_doesnt_exist_with_param(path: &str) -> String {
format!("ERR Path '{}' does not exist", path)
}
pub(crate) fn err_msg_json_path_doesnt_exist() -> String {
"ERR Path does not exist".to_string()
}
pub(crate) fn err_msg_json_path_doesnt_exist_with_param_or(path: &str, or: &str) -> String {
format!("ERR Path '{}' does not exist or {}", path, or)
}