feat: Add serde support behind a feature flag #4

Merged
rosa merged 2 commits from feat/serde into main 2026-08-14 01:33:48 +00:00
Owner

Adds a serde feature, off by default, giving Money, MoneyBag,
Currency, IsoAlphabeticCode, IsoNumericCode, and RoundingMode a
Serialize and a Deserialize impl. The wire shapes are documented in the
README, under Features.

The impls are lucre's own end to end, down to the text an amount is written
as. rust_decimal's serde-float and serde-str features are crate-wide and
additive, so deferring to Decimal's impls would have let any crate sharing a
build change what lucre writes. rust_decimal is now depended on with its
default features off, std aside, which also stops lucre handing Decimal: Serialize to downstream crates that never asked for it.

Reading a MoneyBag totals whatever the document says rather than insisting
it already be in the shape a bag would have written: a zero balance leaves no
currency behind, and a currency named twice is summed, matching FromIterator
and +=.

IsoNumericCode gains TryFrom<u32>, without which deserialization would
have been the only way to build one from an integer.

The first commit is separate: Money::from_major now rescales to the
currency's minor digits, so the three constructors produce the same Decimal
for the same money.

Adds a `serde` feature, off by default, giving `Money`, `MoneyBag`, `Currency`, `IsoAlphabeticCode`, `IsoNumericCode`, and `RoundingMode` a `Serialize` and a `Deserialize` impl. The wire shapes are documented in the README, under Features. The impls are lucre's own end to end, down to the text an amount is written as. rust_decimal's `serde-float` and `serde-str` features are crate-wide and additive, so deferring to `Decimal`'s impls would have let any crate sharing a build change what lucre writes. `rust_decimal` is now depended on with its default features off, `std` aside, which also stops lucre handing `Decimal: Serialize` to downstream crates that never asked for it. Reading a `MoneyBag` totals whatever the document says rather than insisting it already be in the shape a bag would have written: a zero balance leaves no currency behind, and a currency named twice is summed, matching `FromIterator` and `+=`. `IsoNumericCode` gains `TryFrom<u32>`, without which deserialization would have been the only way to build one from an integer. The first commit is separate: `Money::from_major` now rescales to the currency's minor digits, so the three constructors produce the same `Decimal` for the same money.
Money::from_major built its amount at scale zero, so the Decimal it
carried differed from the one from_minor and from_decimal produce for
the same money. Rescaling to the currency's minor digits makes the three
constructors agree.
feat: Add serde support behind a feature flag
All checks were successful
ci/woodpecker/push/test Pipeline was successful
6324be1dd4
Serialize and Deserialize for Money, MoneyBag, Currency,
IsoAlphabeticCode, IsoNumericCode, and RoundingMode, gated on a serde
feature that is off by default. The impls are lucre's own down to the
amount's text, so no feature another crate enables can change what
lucre writes.

IsoNumericCode gains TryFrom<u32>, without which deserialization would
have been the only way to build one from an integer.
rosa merged commit 39692ccf4c into main 2026-08-14 01:33:48 +00:00
rosa deleted branch feat/serde 2026-08-14 01:33:48 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre!4
No description provided.