paupino / rust-decimal

Decimal number implementation written in pure Rust suitable for financial and fixed-precision calculations.
https://docs.rs/rust_decimal/
MIT License
1.02k stars 183 forks source link

Revisit serde features to make them easier to comprehend and work with #405

Open paupino opened 3 years ago

paupino commented 3 years ago

This can likely share some lessons from serde_json, without pulling in the additional dependency.

e.g. Serlialization, look into how this is done.

        serde_json::Number::from_str(&self.to_string())
            .map_err(serde::ser::Error::custom)?
            .serialize(serializer) 

e.g.

        let mut s = serializer.serialize_struct(TOKEN, 1)?;
        s.serialize_field(TOKEN, &self.n)?;
        s.end()

Deserialization would be looking into the arbitrary precision feature.

paupino commented 3 years ago

I think this ticket should encompass a bit more than optimization. We should really start to clean up the behavior and make it easier to comprehend/parse if possible. One proposal is:

  1. Deserialization supports both float (1.234) and string based ("1.234") formats
  2. Serialization format is controlled via feature flag. Default is to use the string based approach however serde-float can be used to serialize float format also.

serde-str should become redundant and serde-arbitrary-precision only necessary if wanting to leverage serde-json precision features (assuming optimization is still a part of this too - i.e. avoiding intermediary floats).