3737//!
3838//! ```
3939
40+ use crate :: error:: { Error , SkyhashError } ;
4041use crate :: types:: Array ;
4142use crate :: types:: FlatElement ;
4243use crate :: types:: SnapshotResult ;
@@ -47,54 +48,34 @@ use crate::IntoSkyhashAction;
4748use crate :: IntoSkyhashBytes ;
4849use crate :: Query ;
4950use crate :: RespCode ;
50- use crate :: Response ;
51+ use crate :: SkyResult ;
5152
5253cfg_async ! (
5354 use core:: { future:: Future , pin:: Pin } ;
5455) ;
55- use std:: io:: ErrorKind ;
5656
5757/// The error string returned when the snapshot engine is busy
5858pub const ERR_SNAPSHOT_BUSY : & str = "err-snapshot-busy" ;
5959/// The error string returned when periodic snapshots are busy
6060pub const ERR_SNAPSHOT_DISABLED : & str = "err-snapshot-disabled" ;
6161
62- /// Errors while running actions
63- #[ derive( Debug ) ]
64- #[ non_exhaustive]
65- pub enum ActionError {
66- /// The server sent data but we failed to parse it
67- ParseError ,
68- /// The server sent an unexpected data type for this action
69- UnexpectedDataType ,
70- /// The server sent an unknown data type that we cannot parse
71- UnknownDataType ,
72- /// The server sent an invalid response
73- InvalidResponse ,
74- /// An I/O error occurred while running this action
75- IoError ( ErrorKind ) ,
76- /// The server returned a response code **other than the one that should have been returned
77- /// for this action** (if any)
78- Code ( RespCode ) ,
79- }
80-
8162cfg_async ! (
8263 /// A special result that is returned when running actions (async)
8364 pub type AsyncResult <' s, T > = Pin <Box <dyn Future <Output = T > + Send + Sync + ' s>>;
8465 #[ doc( hidden) ]
8566 pub trait AsyncSocket : Send + Sync {
86- fn run( & mut self , q: Query ) -> AsyncResult <std :: io :: Result < Response > >;
67+ fn run( & mut self , q: Query ) -> AsyncResult <SkyResult >;
8768 }
8869 impl <T > AsyncActions for T where T : AsyncSocket { }
8970) ;
9071
9172/// A special result that is returned when running actions
92- pub type ActionResult < T > = Result < T , ActionError > ;
73+ pub type ActionResult < T > = Result < T , Error > ;
9374
9475cfg_sync ! (
9576 #[ doc( hidden) ]
9677 pub trait SyncSocket {
97- fn run( & mut self , q: Query ) -> std :: io :: Result < Response > ;
78+ fn run( & mut self , q: Query ) -> SkyResult ;
9879 }
9980 impl <T > Actions for T where T : SyncSocket { }
10081) ;
@@ -103,12 +84,8 @@ macro_rules! gen_match {
10384 ( $ret: expr, $( $( $mtch: pat) + $( if $exp: expr) * , $expect: expr) ,* ) => {
10485 match $ret {
10586 $( $( Ok ( $mtch) ) |* $( if $exp: expr) * => Ok ( $expect) , ) *
106- Ok ( Response :: InvalidResponse ) => Err ( ActionError :: InvalidResponse ) ,
107- Ok ( Response :: ParseError ) => Err ( ActionError :: ParseError ) ,
108- Ok ( Response :: UnsupportedDataType ) => Err ( ActionError :: UnknownDataType ) ,
109- Ok ( Response :: Item ( Element :: RespCode ( code) ) ) => Err ( ActionError :: Code ( code) ) ,
110- Ok ( Response :: Item ( _) ) => Err ( ActionError :: UnexpectedDataType ) ,
111- Err ( e) => Err ( ActionError :: IoError ( e. kind( ) ) ) ,
87+ Ok ( _) => Err ( SkyhashError :: UnexpectedDataType . into( ) ) ,
88+ Err ( e) => Err ( e) ,
11289 }
11390 } ;
11491}
@@ -155,52 +132,52 @@ implement_actions!(
155132 /// Get the number of keys present in the database
156133 fn dbsize( ) -> usize {
157134 { Query :: from( "dbsize" ) }
158- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
135+ Element :: UnsignedInt ( int) => int as usize
159136 }
160137 /// Deletes a single or a number of keys
161138 ///
162139 /// This will return the number of keys that were deleted
163140 fn del( key: impl IntoSkyhashAction + ' s) -> usize {
164141 { Query :: from( "del" ) . arg( key) }
165- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
142+ Element :: UnsignedInt ( int) => int as usize
166143 }
167144 /// Checks if a key (or keys) exist(s)
168145 ///
169146 /// This will return the number of keys that do exist
170147 fn exists( key: impl IntoSkyhashAction + ' s) -> usize {
171148 { Query :: from( "exists" ) . arg( key) }
172- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
149+ Element :: UnsignedInt ( int) => int as usize
173150 }
174151 /// Removes all the keys present in the database
175152 fn flushdb( ) -> ( ) {
176153 { Query :: from( "flushdb" ) }
177- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => { }
154+ Element :: RespCode ( RespCode :: Okay ) => { }
178155 }
179156 /// Get the value of a key
180157 fn get( key: impl IntoSkyhashBytes + ' s) -> String {
181158 { Query :: from( "get" ) . arg( key) }
182- Response :: Item ( Element :: Str ( st) ) => st
159+ Element :: String ( st) => st
183160 }
184161 /// Get the length of a key
185162 fn keylen( key: impl IntoSkyhashBytes + ' s) -> usize {
186163 { Query :: from( "keylen" ) . arg( key) }
187- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
164+ Element :: UnsignedInt ( int) => int as usize
188165 }
189166 /// Returns a vector of keys
190167 ///
191168 /// Do note that the order might be completely meaningless
192169 fn lskeys( count: usize ) -> Vec <FlatElement > {
193170 { Query :: from( "lskeys" ) . arg( count) }
194- Response :: Item ( Element :: Array ( Array :: Flat ( arr) ) ) => arr
171+ Element :: Array ( Array :: Flat ( arr) ) => arr
195172 }
196173 /// Get multiple keys
197174 ///
198175 /// This returns a vector of [`Element`]s which either contain the values
199176 /// as strings or contains `Not Found (Code: 1)` response codes
200177 fn mget( keys: impl IntoSkyhashAction + ' s) -> Array {
201178 { Query :: from( "mget" ) . arg( keys) }
202- Response :: Item ( Element :: Array ( Array :: Bin ( brr) ) ) => Array :: Bin ( brr) ,
203- Response :: Item ( Element :: Array ( Array :: Str ( srr) ) ) => Array :: Str ( srr)
179+ Element :: Array ( Array :: Bin ( brr) ) => Array :: Bin ( brr) ,
180+ Element :: Array ( Array :: Str ( srr) ) => Array :: Str ( srr)
204181 }
205182 /// Creates a snapshot
206183 ///
@@ -209,15 +186,16 @@ implement_actions!(
209186 /// which is normal behavior and _not an inherent error_
210187 fn mksnap( ) -> SnapshotResult {
211188 { Query :: from( "mksnap" ) }
212- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => SnapshotResult :: Okay ,
213- Response :: Item ( Element :: RespCode ( RespCode :: ErrorString ( er) ) ) => {
189+ Element :: RespCode ( RespCode :: Okay ) => SnapshotResult :: Okay ,
190+ Element :: RespCode ( RespCode :: ErrorString ( er) ) => {
214191 match er. as_str( ) {
215192 ERR_SNAPSHOT_BUSY => SnapshotResult :: Busy ,
216193 ERR_SNAPSHOT_DISABLED => SnapshotResult :: Disabled ,
217- _ => return Err ( ActionError :: InvalidResponse )
194+ _ => return Err ( SkyhashError :: InvalidResponse . into ( ) )
218195 }
219196 }
220197 }
198+
221199 /// Sets the value of multiple keys and values and returns the number of keys that were set
222200 ///
223201 /// ## Panics
@@ -231,7 +209,7 @@ implement_actions!(
231209 assert!( keys. incr_len_by( ) == values. incr_len_by( ) , "The number of keys and values for mset must be equal" ) ;
232210 Query :: from( "mset" ) . _push_alt_iter( keys, values)
233211 }
234- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
212+ Element :: UnsignedInt ( int) => int as usize
235213 }
236214 /// Updates the value of multiple keys and values and returns the number of keys that were updated
237215 ///
@@ -246,34 +224,34 @@ implement_actions!(
246224 assert!( keys. incr_len_by( ) == values. incr_len_by( ) , "The number of keys and values for mupdate must be equal" ) ;
247225 Query :: from( "mset" ) . _push_alt_iter( keys, values)
248226 }
249- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
227+ Element :: UnsignedInt ( int) => int as usize
250228 }
251229 /// Consumes a key if it exists
252230 ///
253231 /// This should return either the corresponding values of the provided keys or `Not Found`
254232 /// error codes
255233 fn pop( keys: impl IntoSkyhashBytes + ' s) -> Str {
256234 { Query :: from( "POP" ) . arg( keys) }
257- Response :: Item ( Element :: Str ( st) ) => Str :: Unicode ( st) ,
258- Response :: Item ( Element :: Binstr ( bstr) ) => Str :: Binary ( bstr)
235+ Element :: String ( st) => Str :: Unicode ( st) ,
236+ Element :: Binstr ( bstr) => Str :: Binary ( bstr)
259237 }
260238 /// Consumes the provided keys if they exist
261239 fn mpop( keys: impl IntoSkyhashAction + ' s) -> Array {
262240 { Query :: from( "mpop" ) . arg( keys) }
263- Response :: Item ( Element :: Array ( Array :: Bin ( brr) ) ) => Array :: Bin ( brr) ,
264- Response :: Item ( Element :: Array ( Array :: Str ( srr) ) ) => Array :: Str ( srr)
241+ Element :: Array ( Array :: Bin ( brr) ) => Array :: Bin ( brr) ,
242+ Element :: Array ( Array :: Str ( srr) ) => Array :: Str ( srr)
265243 }
266244 /// Deletes all the provided keys if they exist or doesn't do anything at all. This method
267245 /// will return true if all the provided keys were deleted, else it will return false
268246 fn sdel( keys: impl IntoSkyhashAction + ' s) -> bool {
269247 { Query :: from( "sdel" ) . arg( keys) }
270- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => true ,
271- Response :: Item ( Element :: RespCode ( RespCode :: NotFound ) ) => false
248+ Element :: RespCode ( RespCode :: Okay ) => true ,
249+ Element :: RespCode ( RespCode :: NotFound ) => false
272250 }
273251 /// Set the value of a key
274252 fn set( key: impl IntoSkyhashBytes + ' s, value: impl IntoSkyhashBytes + ' s) -> ( ) {
275253 { Query :: from( "set" ) . arg( key) . arg( value) }
276- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => { }
254+ Element :: RespCode ( RespCode :: Okay ) => { }
277255 }
278256 /// Sets the value of all the provided keys or does nothing. This method will return true if all the keys
279257 /// were set or will return false if none were set
@@ -292,8 +270,8 @@ implement_actions!(
292270 ) ;
293271 Query :: from( "sset" ) . _push_alt_iter( keys, values)
294272 }
295- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => true ,
296- Response :: Item ( Element :: RespCode ( RespCode :: OverwriteError ) ) => false
273+ Element :: RespCode ( RespCode :: Okay ) => true ,
274+ Element :: RespCode ( RespCode :: OverwriteError ) => false
297275 }
298276 /// Updates the value of all the provided keys or does nothing. This method will return true if all the keys
299277 /// were updated or will return false if none were updated
@@ -312,13 +290,13 @@ implement_actions!(
312290 ) ;
313291 Query :: from( "supdate" ) . _push_alt_iter( keys, values)
314292 }
315- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => true ,
316- Response :: Item ( Element :: RespCode ( RespCode :: NotFound ) ) => false
293+ Element :: RespCode ( RespCode :: Okay ) => true ,
294+ Element :: RespCode ( RespCode :: NotFound ) => false
317295 }
318296 /// Update the value of a key
319297 fn update( key: impl IntoSkyhashBytes + ' s, value: impl IntoSkyhashBytes + ' s) -> ( ) {
320298 { Query :: from( "update" ) . arg( key) . arg( value) }
321- Response :: Item ( Element :: RespCode ( RespCode :: Okay ) ) => { }
299+ Element :: RespCode ( RespCode :: Okay ) => { }
322300 }
323301 /// Updates or sets all the provided keys and returns the number of keys that were set
324302 ///
@@ -336,6 +314,6 @@ implement_actions!(
336314 ) ;
337315 Query :: from( "uset" ) . _push_alt_iter( keys, values)
338316 }
339- Response :: Item ( Element :: UnsignedInt ( int) ) => int as usize
317+ Element :: UnsignedInt ( int) => int as usize
340318 }
341319) ;
0 commit comments