Rocket
Rocket copied to clipboard
Impl FromFormField for Decimal type
Existing Functionality
I am attempting to record monetary transactions and would like to use rust_decimal::Decimal
for this field, but it requires me to implement FromFormField to use this. I would prefer not to use an f64 type for this, as it is not recommended.
Suggested Changes
Can rust_decimal::Decimal
be included in the standard implementation?
I have been struggling to complete the implementation, as the example shows a struct with multiple key: value pairs and Decimal doesn't have this format.
Wouldn't an integer type be better for monetary values? If you store your prices in cents, you'll have the best precision and speed possible. However, if you're dealing with sub-cent values, it may indeed be better to use rust_decimal
.
If you really want to use rust_decimal
, you will have to make a newtype around Decimal
so you can implement FromFormType
yourself. Maybe something like that could work? (not tested)
struct MyDecimal(Decimal);
#[rocket::async_trait]
impl<'r> FromFormField<'r> for MyDecimal {
fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
Decimal::from_str(field.value)
.map(Self)
.map_err(|e| rocket::form::Error::custom(e).into())
}
async fn from_data(field: DataField<'r, '_>) -> Result<'v, Self> {
unimplemented!()
}
fn default() -> Option<Self> {
Self(Decimal::default())
}
]
Can I take this up ?
If you really want to use
rust_decimal
, you will have to make a newtype aroundDecimal
so you can implementFromFormType
yourself. Maybe something like that could work? (not tested)
Exactly. I don't think Rocket should be adding dependencies, optional or not, for every type we might want trait implementations for. Perhaps the other library would be willing to do, and that'd be great. Otherwise, we're unfortunately stuck with this limitation of Rust and the necessary workarounds.