Skip to content

Added runtime support for rust_decimal#470

Open
Isaac-Duarte wants to merge 6 commits intoAleph-Alpha:mainfrom
Isaac-Duarte:rust_decimal_runtime
Open

Added runtime support for rust_decimal#470
Isaac-Duarte wants to merge 6 commits intoAleph-Alpha:mainfrom
Isaac-Duarte:rust_decimal_runtime

Conversation

@Isaac-Duarte
Copy link

@Isaac-Duarte Isaac-Duarte commented Jan 24, 2026

Goal

This pull request adds initial support for the rust_decimal crate.

Note this does not support serde(with=... feature flags from rust_decimal.

As discussed with @NyxCode, we could utilize serde under the hood to detect this at runtime. Due to having to write a serializer the code is a little but of a mouth full. Another alternative would be to use serde_json, but I think I'd rather support this feature via serde as opposed to having to depend on serde_json for this feature.

Closes #272

Changes

I implemented a probe serializer that will perform a one time (via OnceLock) attempt to get the runtime serialization type of a rust_decimal::Decimal.

I had a hard time coming up with a way to test this feature for both serialization formats without adding an additional feature flag, so some input/feedback there would be appreciated!

Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Sanity test

I ran a test using this branch.

The struct:

#[derive(Debug, Serialize, TS)]
#[ts(export)]
struct TestStruct {
    decimal: Decimal,
}

I attempted to run cargo test with this feature enabled.

rust_decimal = { version = "1.40.0", features = ["serde-str"] }

I got:

// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type TestStruct = { decimal: string, };

I attempted to run cargo test with this feature enabled.

rust_decimal = { version = "1.40.0", features = ["serde-float"] }

I got:

// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type TestStruct = { decimal: number, };

@Isaac-Duarte
Copy link
Author

@NyxCode I would love your review on this when you have time! Thanks for the collaboration!

@NyxCode
Copy link
Collaborator

NyxCode commented Jan 25, 2026

Thanks for the PR. This is exactly what I had in mind, very nice.
Looking at rust-decimal a bit closer, the behavior is more complicated than I thought.

When serializing, rust-decimal always produces a string, unless serde-float is enabled. Then it's a number.

When deserializing, rust-decimal always accepts both number | string.
When serde-with-arbitrary-precision is enabled, it also accepts { "$serde_json::private::Number": string }

We could either go with your implementation, although the type (string xor number) would be more restrictive when it comes to receiving data from TS.
Alternatively, we could also always emit string | number or string | number | { "$serde_json::private::Number": string }, depending on the serde-with-arbitrary-precision feature. That would allow TS code to send anything rust-decimal can deserialize, but would require the TS code to handle more cases when receiving JSON.

I'm inclined to go with your current implementation, since that seems to me to be what most users would be expecting.
Any thoughts @gustavo-shigueo?

Copy link
Collaborator

@gustavo-shigueo gustavo-shigueo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm inclined to go with your current implementation, since that seems to me to be what most users would be expecting.
Any thoughts @gustavo-shigueo?

I don't fully understand how rust_decimal works tbh, but this implementation looks sensible

@Isaac-Duarte
Copy link
Author

Thanks! I fixed the merge conflict.

@Isaac-Duarte
Copy link
Author

Do you think this can be merged now @NyxCode ?

@Isaac-Duarte
Copy link
Author

I fixed the test issues, they run clean on my end!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: rust_decimal::Decimal TS implementation

3 participants