tmontaigu / dbase-rs

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

Wrong Float and None parsing #2

Closed Farkal closed 5 years ago

Farkal commented 5 years ago

Hi and thanks for your lib. I have find that float parsing was wrong because you still need to read the field length. You just need to use the same code for Numeric and for Float but with parsing as f32 for float:

field.rs

FieldType::Float => {
        let value = read_string_of_len(&mut source, field_info.field_length)?;
        FieldValue::Float(value.trim().parse::<f32>()?)
},

I found another issue with some python lib encoding shapefile: To be able to run the script you will need to have python 3.6 and run pip install pyshp

import shapefile

writer = shapefile.Writer(shapefile.POLYGON)

writer.autoBalancer = 1

writer.field('name','C')
writer.field('money','F')

prop = {
    "name": 'myname',
    "money": None,
}

coords = [(0,0), (0,1), (1,1), (0,1), (0,0)]

writer.poly(parts=[coords])

values = list(prop.values())

writer.record(*values)

writer.save('test.shp')

If we encode dbf with None value the decoding fail. Lot of lib have the same behavior but mapshaper.org work great with this values. I think we should be able to decode this values.

If you want i can create merge request and add my tests 😉

tmontaigu commented 5 years ago

Hi thanks for reporting an error 😄

For the Float parsing yes the code is wrong and i will accept a PR correcting it.

For the second error, the issue is bigger and affects more .dbf types (if not all) (Character, bool, Numeric, Date, etc) where a field can be uninitialied and will be written with only space to fill the length of the field.

The PR #1 shows that we had a problem parsing the Date type when the field was unitialzed and written with only spaces.

So I think what we would have to do is use the Option<T> to wrap FieldValue,

Farkal commented 5 years ago

I have created the pull request. I also see you change the implementation with Option but i still get thread 'test_my_file' panicked at 'calledResult::unwrap()on anErrvalue: ParseFloatError(ParseFloatError { kind: Invalid })' when i try to parse float encoded with None.

tmontaigu commented 5 years ago

Yes, I was waiting for you to do the PR that fixes this 😁

Farkal commented 5 years ago

Sorry i was thinking you wanted to do it yourself. I just create a second PR #4 . Could you release new version after this merge ? I use a lib that depend on your's 😉

tmontaigu commented 5 years ago

Fixed by #4