tmontaigu / dbase-rs

Rust library to read & write dBase files.
MIT License
29 stars 30 forks source link

Using Serde Derive with database containing Memo field #81

Open dbristow-otc opened 9 months ago

dbristow-otc commented 9 months ago

I have a dbase file that contains a Memo field, however I'm not sure what type I should put in the structure, and there doesn't appear to be any way to skip over a field.

#[derive(serde::Deserialize)]
pub struct Record {
    #[serde(rename = "DESCRIPTION")]
    description: Option<String>
}

fn main() {
    let mut reader = dbase::Reader::from_path("existing_database.dbf").unwrap();
    let records = reader.read_as::<Record>();

    dbg!(records);
}

This returns the following error:

[src\main.rs:90] records = Err(
    Error {
        record_num: 0,
        field: Some(
            FieldInfo {
                name: "DESCRIPTION",
                field_type: Memo,
                // trimmed extra data
        ),
        kind: BadConversion(
            FieldTypeNotAsExpected {
                expected: Character,
                actual: Memo,
            },
        ),
    },
)

As a workaround, I implemented the unit type in de.rs and simply make the type ():

index 3a7037b..d487227 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -219,11 +219,13 @@ where
         }
     }

-    fn deserialize_unit<V>(self, _visitor: V) -> Result<<V as Visitor<'de>>::Value, Self::Error>
+    fn deserialize_unit<V>(self, visitor: V) -> Result<<V as Visitor<'de>>::Value, Self::Error>
     where
         V: Visitor<'de>,
     {
-        unimplemented!("DBase cannot deserialize unit")
+        self.skip_next_field()?;
+        visitor.visit_unit()
     }

     fn deserialize_unit_struct<V>(

Is there another type that I should be using? It would be nice to not skip over this field in the future if I end up needing it.

tmontaigu commented 9 months ago

Hum, no at the moment there is nothing that connects Memo fields with serde maybe we can do something about it