Browse Source

Fix clippy nits and rustfmt pass.

Kestrel 2 years ago
parent
commit
64022e7e85
4 changed files with 50 additions and 33 deletions
  1. 2 2
      microrm/src/lib.rs
  2. 6 5
      microrm/src/model/create.rs
  3. 41 25
      microrm/src/model/modelable.rs
  4. 1 1
      microrm/src/query.rs

+ 2 - 2
microrm/src/lib.rs

@@ -20,10 +20,10 @@ macro_rules! value_list {
 // no need to show the re-exports in the documentation
 #[doc(hidden)]
 pub mod re_export {
+    pub use lazy_static;
     pub use serde;
     pub use serde_json;
     pub use sqlite;
-    pub use lazy_static;
 }
 
 #[derive(Debug)]
@@ -213,7 +213,7 @@ pub struct DBPool<'a> {
 impl<'a> DBPool<'a> {
     pub fn new(db: &'a DB) -> Self {
         Self {
-            db: db,
+            db,
             qi: std::sync::RwLock::new(Vec::new()),
         }
     }

+ 6 - 5
microrm/src/model/create.rs

@@ -3,7 +3,8 @@ pub fn sql_for_table<T: crate::model::Entity>() -> (String, String) {
 
     let mut columns = vec!["id integer primary key".to_owned()];
 
-    for i in 1..T::column_count() {
+    // skip the id column type
+    for (i, ty) in types.iter().enumerate().take(T::column_count()).skip(1) {
         let col = <T::Column as std::convert::TryFrom<usize>>::try_from(i).unwrap();
 
         let fk = T::foreign_keys()
@@ -22,7 +23,7 @@ pub fn sql_for_table<T: crate::model::Entity>() -> (String, String) {
         columns.push(format!(
             "\"{}\" {}{}",
             T::name(col),
-            types[i],
+            ty,
             fk.last().unwrap_or_else(|| "".to_string())
         ));
     }
@@ -196,7 +197,7 @@ mod test {
     #[microrm_internal]
     pub struct VecTest {
         e: u64,
-        test: Vec<usize>
+        test: Vec<usize>,
     }
 
     #[test]
@@ -212,7 +213,7 @@ mod test {
     pub enum TestEnum {
         A,
         B,
-        C
+        C,
     }
 
     #[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
@@ -220,7 +221,7 @@ mod test {
     pub struct EnumContainer {
         before: usize,
         e: TestEnum,
-        after: usize
+        after: usize,
     }
 
     #[test]

+ 41 - 25
microrm/src/model/modelable.rs

@@ -17,7 +17,10 @@ macro_rules! integral {
                 stmt.read::<i64>(col_offset).map(|x| (x as Self, 1))
             }
 
-            fn column_type() -> &'static str where Self: Sized {
+            fn column_type() -> &'static str
+            where
+                Self: Sized,
+            {
                 "integer"
             }
         }
@@ -44,7 +47,10 @@ impl Modelable for f64 {
     {
         stmt.read(col_offset).map(|x| (x, 1))
     }
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         "numeric"
     }
 }
@@ -60,7 +66,10 @@ impl Modelable for bool {
     {
         unreachable!("sqlite only gives Strings back, not &strs!");
     }
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         "integer"
     }
 }
@@ -76,7 +85,10 @@ impl<'a> Modelable for &'a str {
         unreachable!("sqlite only gives Strings back, not &strs!");
     }
 
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         "text"
     }
 }
@@ -91,7 +103,10 @@ impl Modelable for std::string::String {
     {
         stmt.read(col_offset).map(|x| (x, 1))
     }
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         "text"
     }
 }
@@ -106,22 +121,13 @@ impl<'a> Modelable for &'a [u8] {
     {
         unreachable!("sqlite only gives Vec<u8> back, not &[u8]!");
     }
-    fn column_type() -> &'static str where Self: Sized {
-        "blob"
-    }
-}
-
-/*impl Modelable for Vec<u8> {
-    fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
-        self.bind(stmt, col)
-    }
-    fn build_from(stmt: &sqlite::Statement, col_offset: usize) -> sqlite::Result<(Self, usize)>
+    fn column_type() -> &'static str
     where
         Self: Sized,
     {
-        stmt.read(col_offset).map(|x| (x, 1))
+        "blob"
     }
-}*/
+}
 
 impl<'a, T: Modelable> Modelable for &'a T {
     fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
@@ -133,7 +139,10 @@ impl<'a, T: Modelable> Modelable for &'a T {
     {
         unreachable!();
     }
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         unreachable!();
     }
 }
@@ -141,10 +150,13 @@ impl<'a, T: Modelable> Modelable for &'a T {
 impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Modelable for Vec<T> {
     fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
         // We serialize Vec<u8> types directly as a blob
-        if std::mem::size_of::<T>() == 1 && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>() {
+        if std::mem::size_of::<T>() == 1
+            && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>()
+        {
             // this bit is unsafe, but is perfectly reasonable...
-            let byte_slice = unsafe { std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
-            return byte_slice.bind_to(stmt, col)
+            let byte_slice =
+                unsafe { std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
+            return byte_slice.bind_to(stmt, col);
         }
         serde_json::to_string(self).unwrap().bind_to(stmt, col)
     }
@@ -153,14 +165,15 @@ impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Mo
         Self: Sized,
     {
         // Deserialize one-byte types directly from the blob
-        if std::mem::size_of::<T>() == 1 && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>() {
+        if std::mem::size_of::<T>() == 1
+            && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>()
+        {
             let blob: Vec<u8> = stmt.read(col_offset)?;
 
             // we know the return value is a u8 because the typeid matches, so while normally this
             // is hilariously unsafe, right now it's perfectly okay.
             Ok((unsafe { std::mem::transmute::<Vec<u8>, Vec<T>>(blob) }, 1))
-        }
-        else {
+        } else {
             let s = String::build_from(stmt, col_offset)?;
             Ok((
                 serde_json::from_str::<Vec<T>>(s.0.as_str()).map_err(|e| sqlite::Error {
@@ -171,7 +184,10 @@ impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Mo
             ))
         }
     }
-    fn column_type() -> &'static str where Self: Sized {
+    fn column_type() -> &'static str
+    where
+        Self: Sized,
+    {
         "blob"
     }
 }

+ 1 - 1
microrm/src/query.rs

@@ -190,7 +190,7 @@ impl<'l> QueryInterface<'l> {
         self.cached_query_column(
             "get_one_by",
             std::any::TypeId::of::<T>(),
-            &c,
+            c,
             &|| {
                 self.db
                     .conn