11#[ macro_use]
22extern crate redis_module;
33
4- use redis_module:: { parse_integer , Context , RedisError , RedisResult } ;
4+ use redis_module:: { Context , RedisError , RedisResult , RedisString } ;
55
6- fn hello_mul ( _: & Context , args : Vec < String > ) -> RedisResult {
6+ fn hello_mul ( _: & Context , args : Vec < RedisString > ) -> RedisResult {
77 if args. len ( ) < 2 {
88 return Err ( RedisError :: WrongArity ) ;
99 }
1010
1111 let nums = args
1212 . into_iter ( )
1313 . skip ( 1 )
14- . map ( |s| parse_integer ( & s ) )
14+ . map ( |s| s . parse_integer ( ) )
1515 . collect :: < Result < Vec < i64 > , RedisError > > ( ) ?;
1616
1717 let product = nums. iter ( ) . product ( ) ;
@@ -32,48 +32,3 @@ redis_module! {
3232 [ "hello.mul" , hello_mul, "" , 0 , 0 , 0 ] ,
3333 ] ,
3434}
35-
36- //////////////////////////////////////////////////////
37-
38- #[ cfg( test) ]
39- mod tests {
40- use super :: * ;
41- use redis_module:: RedisValue ;
42-
43- fn run_hello_mul ( args : & [ & str ] ) -> RedisResult {
44- hello_mul (
45- & Context :: dummy ( ) ,
46- args. iter ( ) . map ( |v| String :: from ( * v) ) . collect ( ) ,
47- )
48- }
49-
50- #[ test]
51- fn hello_mul_valid_integer_args ( ) {
52- let result = run_hello_mul ( & vec ! [ "hello.mul" , "10" , "20" , "30" ] ) ;
53-
54- match result {
55- Ok ( RedisValue :: Array ( v) ) => {
56- assert_eq ! (
57- v,
58- vec![ 10 , 20 , 30 , 6000 ]
59- . into_iter( )
60- . map( RedisValue :: Integer )
61- . collect:: <Vec <_>>( )
62- ) ;
63- }
64- _ => assert ! ( false , "Bad result: {:?}" , result) ,
65- }
66- }
67-
68- #[ test]
69- fn hello_mul_bad_integer_args ( ) {
70- let result = run_hello_mul ( & vec ! [ "hello.mul" , "10" , "xx" , "30" ] ) ;
71-
72- match result {
73- Err ( RedisError :: String ( s) ) => {
74- assert_eq ! ( s, "Couldn't parse as integer: xx" ) ;
75- }
76- _ => assert ! ( false , "Bad result: {:?}" , result) ,
77- }
78- }
79- }
0 commit comments