Parcourir la source

Replace an .expect() with an Err().

Kestrel il y a 1 an
Parent
commit
030bfda161
2 fichiers modifiés avec 27 ajouts et 33 suppressions
  1. 11 17
      microrm/src/query.rs
  2. 16 16
      microrm/src/query/resolve.rs

+ 11 - 17
microrm/src/query.rs

@@ -99,8 +99,8 @@ impl<'l> QueryInterface<'l> {
     fn expect_one_result<T>(
         &self,
         stmt: &mut sqlite::Statement,
-        with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Result<T, crate::Error>,
-    ) -> Result<Option<T>, crate::Error> {
+        with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Result<T, Error>,
+    ) -> Result<Option<T>, Error> {
         let state = stmt.next()?;
         if state != sqlite::State::Row {
             return Ok(None);
@@ -111,10 +111,10 @@ impl<'l> QueryInterface<'l> {
 
     /// Helper function to process an expected zero results
     /// Note that this errors out if there is any result
-    fn expect_no_result(&self, stmt: &mut sqlite::Statement) -> Result<(), crate::Error> {
+    fn expect_no_result(&self, stmt: &mut sqlite::Statement) -> Result<(), Error> {
         let state = stmt.next()?;
         if state != sqlite::State::Done {
-            return Err(crate::Error::ExecFailure);
+            return Err(Error::ExecFailure);
         }
 
         Ok(())
@@ -131,30 +131,24 @@ impl<'l> QueryInterface<'l> {
         hash: u64,
         create: Create,
         mut with: With,
-    ) -> Return
+    ) -> Result<Return, Error>
 where {
         let mut cache = self.cache.borrow_mut();
         let query = cache.entry(hash).or_insert_with(create);
         let res = with(query);
-        query.reset().expect("Couldn't reset query");
-        res
+        query.reset()?;
+        Ok(res)
     }
 }
 
 impl<'l> QueryInterface<'l> {
     /// Add an entity to its table in the database. Alias for `add`.
-    pub fn insert<T: Entity + serde::Serialize>(
-        &self,
-        m: &T,
-    ) -> Result<<T as Entity>::ID, crate::Error> {
+    pub fn insert<T: Entity + serde::Serialize>(&self, m: &T) -> Result<<T as Entity>::ID, Error> {
         self.add(m)
     }
 
     /// Add an entity to its table in the database.
-    pub fn add<T: Entity + serde::Serialize>(
-        &self,
-        m: &T,
-    ) -> Result<<T as Entity>::ID, crate::Error> {
+    pub fn add<T: Entity + serde::Serialize>(&self, m: &T) -> Result<<T as Entity>::ID, Error> {
         let mut hasher = std::collections::hash_map::DefaultHasher::new();
         "add".hash(&mut hasher);
         std::any::TypeId::of::<T>().hash(&mut hasher);
@@ -179,12 +173,12 @@ impl<'l> QueryInterface<'l> {
                 crate::model::store::serialize_into(stmt, m)?;
 
                 let rowid = self.expect_one_result(stmt, &mut |stmt| {
-                    stmt.read::<i64>(0).map_err(crate::Error::DBError)
+                    stmt.read::<i64>(0).map_err(Error::DBError)
                 })?;
 
                 Ok(<T as Entity>::ID::from_raw_id(rowid.unwrap()))
             },
-        )
+        )?
     }
 }
 

+ 16 - 16
microrm/src/query/resolve.rs

@@ -1,4 +1,4 @@
-use crate::{entity::EntityID, Entity, QueryInterface, WithID};
+use crate::{entity::EntityID, Entity, Error, QueryInterface, WithID};
 use std::hash::Hasher;
 
 /// Any query that can be completed/executed
@@ -10,34 +10,34 @@ where
 
     fn qi(&self) -> &'r QueryInterface<'q>;
 
-    fn exec(self) -> Result<(), crate::Error>
+    fn exec(self) -> Result<(), Error>
     where
         Self: Sized,
     {
         self.no_result()
     }
-    fn one(self) -> Result<Option<WithID<Self::Output>>, crate::Error>
+    fn one(self) -> Result<Option<WithID<Self::Output>>, Error>
     where
         Self: Sized,
         Self::Output: Entity,
     {
         self.result()
     }
-    fn one_id(self) -> Result<Option<Self::Output>, crate::Error>
+    fn one_id(self) -> Result<Option<Self::Output>, Error>
     where
         Self: Sized,
         Self::Output: EntityID,
     {
         self.id_result()
     }
-    fn all(self) -> Result<Vec<WithID<Self::Output>>, crate::Error>
+    fn all(self) -> Result<Vec<WithID<Self::Output>>, Error>
     where
         Self: Sized,
         Self::Output: Entity,
     {
         self.results()
     }
-    fn all_ids(self) -> Result<Vec<Self::Output>, crate::Error>
+    fn all_ids(self) -> Result<Vec<Self::Output>, Error>
     where
         Self: Sized,
         Self::Output: EntityID,
@@ -45,7 +45,7 @@ where
         self.id_results()
     }
 
-    fn no_result(self) -> Result<(), crate::Error>
+    fn no_result(self) -> Result<(), Error>
     where
         Self: Sized,
     {
@@ -60,10 +60,10 @@ where
 
                 self.qi().expect_no_result(stmt)
             },
-        )
+        )?
     }
 
-    fn result(self) -> Result<Option<WithID<Self::Output>>, crate::Error>
+    fn result(self) -> Result<Option<WithID<Self::Output>>, Error>
     where
         Self: Sized,
         Self::Output: Entity,
@@ -84,10 +84,10 @@ where
                     Ok(WithID::wrap(Self::Output::build_from(stmt)?, id))
                 })
             },
-        )
+        )?
     }
 
-    fn id_result(self) -> Result<Option<Self::Output>, crate::Error>
+    fn id_result(self) -> Result<Option<Self::Output>, Error>
     where
         Self: Sized,
         Self::Output: EntityID,
@@ -108,10 +108,10 @@ where
                     Ok(Self::Output::from_raw_id(id))
                 })
             },
-        )
+        )?
     }
 
-    fn results(self) -> Result<Vec<WithID<Self::Output>>, crate::Error>
+    fn results(self) -> Result<Vec<WithID<Self::Output>>, Error>
     where
         Self: Sized,
         Self::Output: Entity,
@@ -137,10 +137,10 @@ where
 
                 Ok(res)
             },
-        )
+        )?
     }
 
-    fn id_results(self) -> Result<Vec<Self::Output>, crate::Error>
+    fn id_results(self) -> Result<Vec<Self::Output>, Error>
     where
         Self: Sized,
         Self::Output: EntityID,
@@ -166,6 +166,6 @@ where
 
                 Ok(res)
             },
-        )
+        )?
     }
 }