paupino / rust-decimal

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

Serialization V2 - brainstorming #611

Open paupino opened 11 months ago

paupino commented 11 months ago

Problem

Serialization / Deserialization of Rust Decimals has been a pain point for developing against this library simply because there are many different behaviors that developers need to support for a decimal type. For example:

The matrix of options, as well as keeping track of what is supported and when can get confusing.

Option 1

I'd like v2 of this to be a little more policy driven - perhaps leveraging macros. One potential option for this is to make generation/utilization of transparent types a bit easier. That way, you could define the type for the API and then deref when required. e.g.

#[decimal_serde(serialize = "string", deserialize = "float_or_string")]
struct DecimalLike(Decimal);

Could generate an implementation of the Serialize, Deserialize traits as well as any other helpful patterns. It'd allow full customizability at a project by project level (as well as potentially field by field).

One downside: it could become annoying if you had multiple types you needed to generate to support different APIs potentially.

KKould commented 11 months ago

Hope to add Memory Comparable, many KV-based databases can use Memory Comparable to create naturally ordered indexes.