1
1
#[ macro_use]
2
2
extern crate redismodule;
3
3
4
- use redismodule:: { Context , RedisResult , NextArg , REDIS_OK } ;
4
+ use redismodule:: { Context , RedisResult , NextArg , REDIS_OK , RedisError } ;
5
5
use redismodule:: native_types:: RedisType ;
6
6
7
7
mod redisjson;
@@ -14,7 +14,6 @@ static REDIS_JSON_TYPE: RedisType = RedisType::new("RedisJSON");
14
14
pub enum SetOptions {
15
15
NotExists ,
16
16
AlreadyExists ,
17
- None
18
17
}
19
18
20
19
fn json_set ( ctx : & Context , args : Vec < String > ) -> RedisResult {
@@ -23,32 +22,32 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
23
22
let key = args. next_string ( ) ?;
24
23
let _path = args. next_string ( ) ?; // TODO handle this path
25
24
let value = args. next_string ( ) ?;
26
- let option = match args. next ( ) {
27
- Some ( op) => {
28
- match op. as_str ( ) {
29
- "NX" => SetOptions :: NotExists ,
30
- "XX" => SetOptions :: AlreadyExists ,
31
- _ => return Err ( "ERR syntax error" . into ( ) )
25
+
26
+ let set_option = args. next ( )
27
+ . map ( |op| {
28
+ match op. to_uppercase ( ) . as_str ( ) {
29
+ "NX" => Ok ( SetOptions :: NotExists ) ,
30
+ "XX" => Ok ( SetOptions :: AlreadyExists ) ,
31
+ _ => Err ( RedisError :: Str ( "ERR syntax error" ) ) ,
32
32
}
33
- }
34
- None => {
35
- SetOptions :: None
36
- }
37
- } ;
33
+ } )
34
+ . transpose ( ) ?;
38
35
39
36
let key = ctx. open_key_writable ( & key) ;
37
+ let current = key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?;
40
38
41
- match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
42
- Some ( ref mut doc) if option != SetOptions :: NotExists => {
39
+ match ( current, set_option) {
40
+ ( Some ( _) , Some ( SetOptions :: NotExists ) ) => REDIS_OK ,
41
+ ( Some ( ref mut doc) , _) => {
43
42
doc. set_value ( & value) ?;
44
43
REDIS_OK
45
44
}
46
- None if option != SetOptions :: AlreadyExists => {
45
+ ( None , Some ( SetOptions :: AlreadyExists ) ) => REDIS_OK ,
46
+ ( None , _) => {
47
47
let doc = RedisJSON :: from_str ( & value) ?;
48
48
key. set_value ( & REDIS_JSON_TYPE , doc) ?;
49
49
REDIS_OK
50
50
}
51
- _ => Ok ( ( ) . into ( ) )
52
51
}
53
52
}
54
53
0 commit comments