Skip to content

Comments

Add fastnum implementation#502

Open
GreeFine wants to merge 1 commit intoGREsau:masterfrom
GreeFine:master
Open

Add fastnum implementation#502
GreeFine wants to merge 1 commit intoGREsau:masterfrom
GreeFine:master

Conversation

@GreeFine
Copy link

@GreeFine GreeFine commented Jan 12, 2026

I added the implementation for Fastnum.

It's another crate for decimal numbers.
I could have done a more precise job on the De/Serialize regex for Unsigned values, but want to keep the changes to the minimum.

I did test it on my project, using aide, it matched expectations.

Workaround I used if it helps someone in the same situation
use std::borrow::Cow;

use schemars::{Schema, SchemaGenerator, generate::Contract};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct JsonUDecimal(fastnum::decimal::UnsignedDecimal<4>);

impl schemars::JsonSchema for JsonUDecimal {
    fn schema_name() -> Cow<'static, str> {
        "UDecimal".into()
    }

    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
        let (ty, pattern) = match generator.contract() {
            Contract::Deserialize => ("string".into(), r"^\d+(\.\d+)?([eE]\d+)?$".into()),
            Contract::Serialize => ("string".into(), r"^\d+(\.\d+)?$".into()),
            // Contract is marked as non_exhaustive
            _ => unimplemented!("New version of Schemars introduced a new contract type"),
        };

        let mut result = Schema::default();
        result.insert("type".to_owned(), ty);
        result.insert("pattern".to_owned(), pattern);
        result
    }
}

impl From<fastnum::decimal::UnsignedDecimal<4>> for JsonUDecimal {
    fn from(value: fastnum::decimal::UnsignedDecimal<4>) -> Self {
        Self(value)
    }
}

@GreeFine
Copy link
Author

GreeFine commented Jan 12, 2026

Looks like Fastnum is requiring a Rust version that is quite new.
Sadly, dropping a few versions won't help, the 0.3 still requires Rust v1.87.0.

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.

1 participant