Kestrel 2 éve
szülő
commit
50b6e1a9ef
3 módosított fájl, 26 hozzáadás és 35 törlés
  1. 10 13
      microrm-macros/src/lib.rs
  2. 12 14
      microrm/src/lib.rs
  3. 4 8
      microrm/src/model/create.rs

+ 10 - 13
microrm-macros/src/lib.rs

@@ -4,16 +4,15 @@ use syn::{parse_macro_input, DeriveInput};
 
 use convert_case::{Case, Casing};
 
-fn parse_microrm_ref(attrs: &Vec<syn::Attribute>) -> proc_macro2::TokenStream {
+fn parse_microrm_ref(attrs: &[syn::Attribute]) -> proc_macro2::TokenStream {
     for attr in attrs {
-        if attr.path.segments.len() == 0 {
+        if attr.path.segments.is_empty() {
             continue;
         }
 
-        if attr.tokens.is_empty() {
-            if attr.path.segments.last().unwrap().ident == "microrm_internal" {
-                return quote! { crate }.into();
-            }
+        if attr.tokens.is_empty() && attr.path.segments.last().unwrap().ident == "microrm_internal"
+        {
+            return quote! { crate };
         }
     }
 
@@ -40,7 +39,7 @@ fn parse_microrm_ref(attrs: &Vec<syn::Attribute>) -> proc_macro2::TokenStream {
 pub fn derive_entity(tokens: TokenStream) -> TokenStream {
     let input = parse_macro_input!(tokens as DeriveInput);
 
-    let mut microrm_ref = parse_microrm_ref(&input.attrs);
+    let microrm_ref = parse_microrm_ref(&input.attrs);
 
     let struct_name = &input.ident;
     let enum_name = format_ident!("{}Columns", &input.ident);
@@ -70,14 +69,14 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
 
         let field_name = name.ident.as_ref().unwrap().clone();
         let field_name_str = format!("{}", field_name);
-        field_names.push(quote! { Self::Column::#converted_case => #field_name_str }.into());
+        field_names.push(quote! { Self::Column::#converted_case => #field_name_str });
 
         value_references.push(quote! { &self. #field_name });
     }
 
     let field_count = fields.named.iter().count();
 
-    let ret = quote!{
+    quote!{
         // Related types for #struct_name
         #[derive(Clone,Copy)]
         #[allow(unused)]
@@ -127,9 +126,7 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
                 vec![ #value_references ]
             }
         }
-    }.into();
-
-    ret
+    }.into()
 }
 
 /// Marks a struct as able to be directly used in an Entity to correspond to a single database column.
@@ -137,7 +134,7 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
 pub fn derive_modelable(tokens: TokenStream) -> TokenStream {
     let input = parse_macro_input!(tokens as DeriveInput);
 
-    let mut microrm_ref = parse_microrm_ref(&input.attrs);
+    let microrm_ref = parse_microrm_ref(&input.attrs);
 
     let ident = input.ident;
 

+ 12 - 14
microrm/src/lib.rs

@@ -23,21 +23,19 @@
 //!     pub value: String
 //! }
 //!
-//! fn main() {
-//!     let schema = microrm::model::SchemaModel::new().add::<KVStore>();
-//!     let db = microrm::DB::new_in_memory(schema).unwrap();
-//!     
-//!     microrm::query::add(&db, &KVStore {
-//!         key: "a_key".to_string(),
-//!         value: "a_value".to_string()
-//!     });
+//! let schema = microrm::model::SchemaModel::new().add::<KVStore>();
+//! let db = microrm::DB::new_in_memory(schema).unwrap();
 //!
-//!     let qr = microrm::query::get_one_by(&db, KVStoreColumns::Key, "a_key");
+//! microrm::query::add(&db, &KVStore {
+//!     key: "a_key".to_string(),
+//!     value: "a_value".to_string()
+//! });
 //!
-//!     assert_eq!(qr.is_some(), true);
-//!     assert_eq!(qr.as_ref().unwrap().key, "a_key");
-//!     assert_eq!(qr.as_ref().unwrap().value, "a_value");
-//! }
+//! let qr = microrm::query::get_one_by(&db, KVStoreColumns::Key, "a_key");
+//!
+//! assert_eq!(qr.is_some(), true);
+//! assert_eq!(qr.as_ref().unwrap().key, "a_key");
+//! assert_eq!(qr.as_ref().unwrap().value, "a_value");
 //! ```
 
 mod meta;
@@ -182,7 +180,7 @@ impl DB {
         );
 
         let sanity_check = query::get_one_by(self, meta::MetaschemaColumns::Key, "schema_hash");
-        assert_eq!(sanity_check.is_some(), true);
+        assert!(sanity_check.is_some());
         assert_eq!(sanity_check.unwrap().value, self.schema_hash);
 
         Ok(())

+ 4 - 8
microrm/src/model/create.rs

@@ -102,21 +102,17 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut CreateDeserializer<'de> {
         v: V,
     ) -> Result<V::Value, Self::Error> {
         if self.struct_visited {
-            let elength = self.expected_length.clone();
-            let old_elength = elength.get();
-            println!("nested deserialize_struct invoked!");
-            todo!();
+            panic!("Nested structs not allowed!");
         } else {
             self.column_name_stack.extend(fields.iter().rev());
             self.expected_length.set(fields.len());
-            let ret = v.visit_seq(self);
-            ret
+            v.visit_seq(self)
         }
     }
 
     fn deserialize_newtype_struct<V: Visitor<'de>>(
         self,
-        name: &'static str,
+        _name: &'static str,
         v: V,
     ) -> Result<V::Value, Self::Error> {
         let elength = self.expected_length.clone();
@@ -148,7 +144,7 @@ impl<'de> serde::de::SeqAccess<'de> for CreateDeserializer<'de> {
 }
 
 pub fn sql_for<T: crate::model::Entity>() -> (String, String) {
-    let mut elength = Rc::new(Cell::new(0));
+    let elength = Rc::new(Cell::new(0));
 
     let mut cd = CreateDeserializer {
         struct_visited: false,