@@ -6,7 +6,7 @@ use redis_module::{
66} ;
77
88pub ( crate ) fn state ( ctx : & Context , args : Vec < RedisString > ) -> RedisResult {
9- if args. len ( ) < 2 || args. len ( ) > 3 {
9+ if args. len ( ) < 2 || args. len ( ) > 3 {
1010 return Err ( RedisError :: WrongArity ) ;
1111 }
1212 let mut args = args. into_iter ( ) . skip ( 1 ) ;
@@ -21,67 +21,29 @@ pub(crate) fn state(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
2121 return Ok ( RedisValue :: Null ) ;
2222 }
2323
24- // return value.map_or_else(
25- // || Ok(RedisValue::Null),
26- // |sm| Ok(RedisValue::SimpleString(sm.current().to_string())),
27- // );
28- // }
29-
3024 let sm = value. unwrap ( ) ;
3125 let mut keys: Vec < RedisValue > = Vec :: new ( ) ;
32- if ! list. is_err ( ) {
26+ if list. is_ok ( ) {
3327 for x in sm. map ( ) . keys ( ) {
3428 keys. push ( RedisValue :: SimpleString ( x. to_string ( ) ) ) ;
3529 }
36- } else if list. unwrap ( ) . to_string ( ) . to_uppercase ( ) == "LIST" {
37- keys. push ( RedisValue :: SimpleString ( sm. current ( ) . to_string ( ) ) ) ;
3830 } else {
39- return Ok ( RedisValue :: Null ) ;
31+ keys . push ( RedisValue :: SimpleString ( sm . current ( ) . to_string ( ) ) ) ;
4032 }
4133 Ok ( RedisValue :: Array ( keys) )
42-
43- // if let Some(sm) = value {
44- // let mut keys: Vec<RedisValue> = Vec::new();
45- // for x in sm.map().keys() {
46- // keys.push(RedisValue::SimpleString(x.to_string()));
47- // }
48- // Ok(RedisValue::Array(keys))
49- // } else {
50- // Ok(RedisValue::Null)
51- // }
52-
5334}
5435
55- // pub(crate) fn states(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
56- // if args.len() != 2 {
57- // return Err(RedisError::WrongArity);
58- // }
59- // let mut args = args.into_iter().skip(1);
60- // let key = args.next_arg()?;
61-
62- // let rkey = RedisKey::open(ctx.ctx, &key);
63- // let value = rkey.get_value::<StateMachine>(&REDIS_SM_TYPE)?;
64-
65- // if let Some(sm) = value {
66- // let mut keys: Vec<RedisValue> = Vec::new();
67- // for x in sm.map().keys() {
68- // keys.push(RedisValue::SimpleString(x.to_string()));
69- // }
70- // Ok(RedisValue::Array(keys))
71- // } else {
72- // Ok(RedisValue::Null)
73- // }
74- // }
75-
7636pub ( crate ) fn mutate ( ctx : & Context , args : Vec < RedisString > ) -> RedisResult {
77- if args. len ( ) < 3 || args. len ( ) > 5 {
37+ if args. len ( ) < 3 || args. len ( ) > 6 {
7838 return Err ( RedisError :: WrongArity ) ;
7939 }
8040
8141 let mut args = args. into_iter ( ) . skip ( 1 ) ;
8242 let key = args. next_arg ( ) ?;
8343 let target = args. next_arg ( ) ?;
84- let force = args. next_arg ( ) ;
44+
45+ let maybe_reason = args. next_arg ( ) ;
46+ let maybe_options = args. next_arg ( ) ;
8547
8648 let rkey = RedisKeyWritable :: open ( ctx. ctx , & key) ;
8749 let value = rkey. get_value :: < StateMachine > ( & REDIS_SM_TYPE ) ?;
@@ -91,17 +53,23 @@ pub(crate) fn mutate(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
9153 }
9254
9355 let sm = value. unwrap ( ) ;
94- if force. is_err ( ) {
95- let res = sm. check_transition ( target. to_string ( ) ) ;
96- if !res {
97- return Ok ( RedisValue :: Null ) ;
56+ if !sm. is_valid_state ( target. to_string ( ) ) {
57+ return Err ( RedisError :: String ( "Invaild state transition" . to_string ( ) ) ) ;
58+ }
59+
60+ let res = sm. can_transition ( target. to_string ( ) ) ;
61+
62+ if let Ok ( options) = maybe_options {
63+ if options. to_string ( ) . to_uppercase ( ) != "FORCE" {
64+ return Err ( RedisError :: String ( "Invaild command option" . to_string ( ) ) ) ;
9865 }
99- sm. set_current ( target. to_string ( ) ) ;
100- } else if force. unwrap ( ) . to_string ( ) . to_uppercase ( ) == "FORCE" {
101- sm. set_current ( target. to_string ( ) ) ;
102- } else {
103- return Ok ( RedisValue :: Null ) ;
66+ } else if !res {
67+ return Err ( RedisError :: String ( "Invaild state transition" . to_string ( ) ) ) ;
10468 }
10569
70+ sm. set_current ( target. to_string ( ) ) ;
71+ if let Ok ( value) = maybe_reason {
72+ sm. set_reason ( value. to_string ( ) ) ;
73+ }
10674 REDIS_OK
10775}
0 commit comments