@@ -137,6 +137,28 @@ impl Context {
137137 }
138138 }
139139
140+ pub fn str_as_legal_resp_string ( s : & str ) -> CString {
141+ CString :: new (
142+ s. chars ( )
143+ . map ( |c| match c {
144+ '\r' | '\n' => ' ' as u8 ,
145+ _ => c as u8 ,
146+ } )
147+ . collect :: < Vec < _ > > ( ) ,
148+ )
149+ . unwrap ( )
150+ }
151+
152+ pub fn reply_simple_string ( & self , s : & str ) -> raw:: Status {
153+ let msg = Context :: str_as_legal_resp_string ( s) ;
154+ unsafe { raw:: RedisModule_ReplyWithSimpleString . unwrap ( ) ( self . ctx , msg. as_ptr ( ) ) . into ( ) }
155+ }
156+
157+ pub fn reply_error_string ( & self , s : & str ) -> raw:: Status {
158+ let msg = Context :: str_as_legal_resp_string ( s) ;
159+ unsafe { raw:: RedisModule_ReplyWithError . unwrap ( ) ( self . ctx , msg. as_ptr ( ) ) . into ( ) }
160+ }
161+
140162 /// # Panics
141163 ///
142164 /// Will panic if methods used are missing in redismodule.h
@@ -211,20 +233,13 @@ impl Context {
211233 }
212234 } ,
213235
214- Err ( RedisError :: WrongType ) => unsafe {
215- let msg = CString :: new ( RedisError :: WrongType . to_string ( ) ) . unwrap ( ) ;
216- raw:: RedisModule_ReplyWithError . unwrap ( ) ( self . ctx , msg. as_ptr ( ) ) . into ( )
217- } ,
236+ Err ( RedisError :: WrongType ) => {
237+ self . reply_error_string ( RedisError :: WrongType . to_string ( ) . as_str ( ) )
238+ }
218239
219- Err ( RedisError :: String ( s) ) => unsafe {
220- let msg = CString :: new ( s) . unwrap ( ) ;
221- raw:: RedisModule_ReplyWithError . unwrap ( ) ( self . ctx , msg. as_ptr ( ) ) . into ( )
222- } ,
240+ Err ( RedisError :: String ( s) ) => self . reply_error_string ( s. as_str ( ) ) ,
223241
224- Err ( RedisError :: Str ( s) ) => unsafe {
225- let msg = CString :: new ( s) . unwrap ( ) ;
226- raw:: RedisModule_ReplyWithError . unwrap ( ) ( self . ctx , msg. as_ptr ( ) ) . into ( )
227- } ,
242+ Err ( RedisError :: Str ( s) ) => self . reply_error_string ( s) ,
228243 }
229244 }
230245
0 commit comments