dbase-rs icon indicating copy to clipboard operation
dbase-rs copied to clipboard

Using Serde Derive with database containing Memo field

Open dbristow-otc opened this issue 7 months ago • 1 comments

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.

dbristow-otc avatar Jan 16 '24 22:01 dbristow-otc

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

tmontaigu avatar Jan 19 '24 12:01 tmontaigu