Browse Source

Properly support loading enums.

Kestrel 2 years ago
parent
commit
bfc7f18643
3 changed files with 14 additions and 3 deletions
  1. 1 1
      Cargo.lock
  2. 9 2
      microrm/src/model/load.rs
  3. 4 0
      microrm/src/query.rs

+ 1 - 1
Cargo.lock

@@ -338,7 +338,7 @@ dependencies = [
 
 [[package]]
 name = "microrm"
-version = "0.2.2"
+version = "0.2.3"
 dependencies = [
  "base64",
  "criterion",

+ 9 - 2
microrm/src/model/load.rs

@@ -1,5 +1,5 @@
 use super::Modelable;
-use serde::de::Visitor;
+use serde::de::{Visitor, IntoDeserializer};
 
 pub struct RowDeserializer<'de, 'l> {
     row: &'de sqlite::Statement<'l>,
@@ -108,6 +108,13 @@ impl<'de, 'a, 'l> serde::de::Deserializer<'de> for &'a mut RowDeserializer<'de,
         res
     }
 
+    fn deserialize_enum<V: Visitor<'de>>(self, _name: &'static str, _variants: &'static [&'static str], v: V) -> Result<V::Value, Self::Error> {
+        let built : (String, _) = Modelable::build_from(self.row, self.col_index)
+            .map_err(|e| Self::Error::LoadError(e.to_string()))?;
+
+        v.visit_enum(built.0.into_deserializer())
+    }
+
     fn deserialize_struct<V: Visitor<'de>>(
         self,
         _name: &'static str,
@@ -128,7 +135,7 @@ impl<'de, 'a, 'l> serde::de::Deserializer<'de> for &'a mut RowDeserializer<'de,
     serde::forward_to_deserialize_any! {
         i128 u128 f32 f64 char str
         bytes option unit unit_struct seq tuple
-        tuple_struct map enum identifier ignored_any
+        tuple_struct map identifier ignored_any
     }
 }
 

+ 4 - 0
microrm/src/query.rs

@@ -13,6 +13,10 @@ pub struct WithID<T: Entity> {
 }
 
 impl<T: Entity> WithID<T> {
+    pub fn new(wrap: T, id: <T as Entity>::ID) -> Self {
+        Self { wrap, id }
+    }
+
     fn wrap(what: T, raw_id: i64) -> Self {
         Self {
             wrap: what,