Bläddra i källkod

Replace num_enum crate with simple reimplementation.

Kestrel 2 år sedan
förälder
incheckning
a3fa9a4e90
6 ändrade filer med 19 tillägg och 67 borttagningar
  1. 0 61
      Cargo.lock
  2. 17 1
      microrm-macros/src/lib.rs
  3. 0 1
      microrm/Cargo.toml
  4. 0 1
      microrm/src/lib.rs
  5. 1 2
      microrm/src/model.rs
  6. 1 1
      microrm/src/model/create.rs

+ 0 - 61
Cargo.lock

@@ -160,7 +160,6 @@ version = "0.1.0"
 dependencies = [
  "base64",
  "microrm-macros",
- "num_enum",
  "rusqlite",
  "serde",
  "serde_bytes",
@@ -178,27 +177,6 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "num_enum"
-version = "0.5.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9"
-dependencies = [
- "num_enum_derive",
-]
-
-[[package]]
-name = "num_enum_derive"
-version = "0.5.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce"
-dependencies = [
- "proc-macro-crate",
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "once_cell"
 version = "1.10.0"
@@ -211,16 +189,6 @@ version = "0.3.25"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
 
-[[package]]
-name = "proc-macro-crate"
-version = "1.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a"
-dependencies = [
- "thiserror",
- "toml",
-]
-
 [[package]]
 name = "proc-macro2"
 version = "1.0.38"
@@ -328,35 +296,6 @@ dependencies = [
  "unicode-xid",
 ]
 
-[[package]]
-name = "thiserror"
-version = "1.0.31"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
-dependencies = [
- "thiserror-impl",
-]
-
-[[package]]
-name = "thiserror-impl"
-version = "1.0.31"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "toml"
-version = "0.5.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
-dependencies = [
- "serde",
-]
-
 [[package]]
 name = "typenum"
 version = "1.15.0"

+ 17 - 1
microrm-macros/src/lib.rs

@@ -68,6 +68,7 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
 
     let mut variants = Vec::new();
     let mut field_names = Vec::new();
+    let mut field_numbers = Vec::new();
     let mut value_references = Vec::new();
 
     let mut foreign_keys = Vec::new();
@@ -83,6 +84,9 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
         let field_name_str = format!("{}", field_name);
         field_names.push(quote! { Self::Column::#converted_case => #field_name_str });
 
+        let nn = field_numbers.len() + 1;
+        field_numbers.push(quote! { #nn => Self::#converted_case, });
+
         if parse_fk(&name.attrs) {
             let fk_struct_name = format_ident!("{}{}ForeignKey", struct_name, converted_case);
             let ty = &name.ty;
@@ -112,7 +116,7 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
 
     quote!{
         // Related types for #struct_name
-        #[derive(Clone,Copy,PartialEq,#microrm_ref::re_export::num_enum::TryFromPrimitive)]
+        #[derive(Clone,Copy,PartialEq)]
         #[allow(unused)]
         #[repr(usize)]
         pub enum #enum_name {
@@ -129,6 +133,18 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
             type Entity = #struct_name;
         }
 
+        impl std::convert::From<usize> for #enum_name {
+            fn from(i: usize) -> Self {
+                match i {
+                    0 => Self::ID,
+                    #(#field_numbers)*
+                    _ => {
+                        panic!("Given invalid usize to convert to column")
+                    },
+                }
+            }
+        }
+
         impl #microrm_ref::model::EntityID for #id_name {
             type Entity = #struct_name;
             fn from_raw_id(raw: i64) -> Self { Self(raw) }

+ 0 - 1
microrm/Cargo.toml

@@ -12,6 +12,5 @@ rusqlite = "0.27"
 serde = { version = "1.0", features = ["derive"] }
 serde_bytes = { version = "0.11.6" }
 serde_json = { version = "1.0" }
-num_enum = { version = "0.5.7" }
 
 microrm-macros = { path = "../microrm-macros" }

+ 0 - 1
microrm/src/lib.rs

@@ -47,7 +47,6 @@ pub use microrm_macros::{Entity, Modelable};
 // no need to show the re-exports in the documentation
 #[doc(hidden)]
 pub mod re_export {
-    pub use num_enum;
     pub use rusqlite;
     pub use serde;
     pub use serde_json;

+ 1 - 2
microrm/src/model.rs

@@ -65,8 +65,7 @@ pub trait Entity: for<'de> serde::Deserialize<'de> + serde::Serialize {
 }
 
 /// Trait representing the columns of a database entity
-pub trait EntityColumns:
-    PartialEq + crate::re_export::num_enum::TryFromPrimitive<Primitive = usize>
+pub trait EntityColumns: PartialEq + From<usize>
 {
     type Entity: Entity;
 }

+ 1 - 1
microrm/src/model/create.rs

@@ -152,7 +152,7 @@ pub fn sql_for<T: crate::model::Entity>() -> (String, String) {
     columns.push("id integer primary key".to_owned());
 
     for i in 1..T::column_count() {
-        let col = <T::Column as num_enum::TryFromPrimitive>::try_from_primitive(i).unwrap();
+        let col = <T::Column as std::convert::TryFrom<usize>>::try_from(i).unwrap();
         println!("{}", T::name(col));
 
         let fk = T::foreign_keys()