tmontaigu / dbase-rs

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

[newbie question] how to retrieve the value of a FieldValue? #82

Closed sanzoghenzo closed 8 months ago

sanzoghenzo commented 8 months ago

Hi, thanks for your work on this project.

I'm new to rust, and just started to piece together a program to read dbf files created by another app and send new data to a server.

I'm struggling to understand how can I get a field value for comparisons other than equality.

My use case is to filter out all the rows that have the "SEC_1970" field (that contains the unix timestamp) older than the given one.

Here's what I've done so far:

fn upload_dbf(entry: &Path, last_upload: DateTime<Utc>) -> Result<(), dbase::Error>{
    let last_uploaded = last_upload.timestamp();
    let reader = dbase::Reader::from_path(entry)?;
    let records = reader.iter_records();
    records.skip_while(|record| 
        record.unwrap().get("SEC_1970").unwrap().to_owned() < last_uploaded
    );
    for record_result in records {
        let record = record_result?;
        // send data
    }
    Ok(())
}

This obviously errors out because binary operation "<" cannot be applied to type FieldValue

I've gone through the documentation but I couldn't find any method that could help me (to my knowledge, at least).

Am I missing something, or approaching this in a completely wrong way?

Thanks for the attention

tmontaigu commented 8 months ago

There is an example in the docs https://docs.rs/dbase/0.5.0/dbase/#examples

You have to match the enum and do things with it

If you know in advance the type of the field value, you can use try_from

dbase::Date::try_from(value).unwrap()
sanzoghenzo commented 8 months ago

If you know in advance the type of the field value, you can use try_from

dbase::Date::try_from(value).unwrap()

This is what I was looking for! Like I said, I'm really a rust newbie, and I was looking for something like .to_int or .to_value... I really need to read at least the Rust by example guide! :sweat_smile:

Thanks for the help!