/* * 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 json_path::select_value::SelectValue; use serde_json::Number; use redis_module::raw::RedisModuleKey; use redis_module::rediserror::RedisError; use redis_module::{Context, RedisResult, RedisString}; use crate::Format; use crate::error::Error; use crate::key_value::KeyValue; pub struct SetUpdateInfo { pub path: Vec, } pub struct AddUpdateInfo { pub path: Vec, pub key: String, } pub enum UpdateInfo { SUI(SetUpdateInfo), AUI(AddUpdateInfo), } pub trait ReadHolder { fn get_value(&self) -> Result, RedisError>; } pub trait WriteHolder { fn delete(&mut self) -> Result<(), RedisError>; fn get_value(&mut self) -> Result, RedisError>; fn set_value(&mut self, path: Vec, v: O) -> Result; fn merge_value(&mut self, path: Vec, v: O) -> Result; fn dict_add(&mut self, path: Vec, key: &str, v: O) -> Result; fn delete_path(&mut self, path: Vec) -> Result; fn incr_by(&mut self, path: Vec, num: &str) -> Result; fn mult_by(&mut self, path: Vec, num: &str) -> Result; fn pow_by(&mut self, path: Vec, num: &str) -> Result; fn bool_toggle(&mut self, path: Vec) -> Result; fn str_append(&mut self, path: Vec, val: String) -> Result; fn arr_append(&mut self, path: Vec, args: Vec) -> Result; fn arr_insert( &mut self, path: Vec, args: &[O], index: i64, ) -> Result; fn arr_pop) -> RedisResult>( &mut self, path: Vec, index: i64, serialize_callback: C, ) -> RedisResult; fn arr_trim(&mut self, path: Vec, start: i64, stop: i64) -> Result; fn clear(&mut self, path: Vec) -> Result; 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 * Naive implementation is that V and O are from the same type but its not * always possible so they are separated */ type V: SelectValue; type O: Clone; type WriteHolder: WriteHolder; type ReadHolder: ReadHolder; fn open_key_read( &self, ctx: &Context, key: &RedisString, ) -> Result; fn open_key_write( &self, ctx: &Context, key: RedisString, ) -> Result; #[allow(clippy::wrong_self_convention)] fn from_str(&self, val: &str, format: Format, limit_depth: bool) -> Result; fn get_memory(&self, v: &Self::V) -> Result; fn is_json(&self, key: *mut RedisModuleKey) -> Result; } pub(crate) fn err_json(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 {expected_value} but found {found}") } pub(crate) fn err_msg_json_path_doesnt_exist_with_param(path: &str) -> String { format!("ERR Path '{path}' does not exist") } 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 '{path}' does not exist or {or}") }