debot_utils/
lib.rs

1pub mod datetime_utils;
2pub mod kws_decrypt;
3pub mod limitied_size_map;
4pub mod math;
5pub mod retry_loop;
6
7pub use datetime_utils::*;
8pub use kws_decrypt::decrypt_data_with_kms;
9pub use limitied_size_map::DynamicLimitedSizeMap;
10pub use limitied_size_map::LimitedSizeMap;
11pub use limitied_size_map::ValuePair;
12pub use math::*;
13pub use retry_loop::*;
14use serde::Serializer;
15use std::num::ParseFloatError;
16use std::str::FromStr;
17
18use rust_decimal::prelude::ToPrimitive;
19use rust_decimal::Decimal;
20use rust_decimal::Error as DecimalParseError;
21
22#[derive(Debug)]
23pub enum ParseDecimalError {
24    FloatError(ParseFloatError),
25    DecimalError(DecimalParseError),
26}
27
28impl From<ParseFloatError> for ParseDecimalError {
29    fn from(err: ParseFloatError) -> Self {
30        ParseDecimalError::FloatError(err)
31    }
32}
33
34impl From<DecimalParseError> for ParseDecimalError {
35    fn from(err: DecimalParseError) -> Self {
36        ParseDecimalError::DecimalError(err)
37    }
38}
39
40pub fn parse_to_decimal(input: &str) -> Result<Decimal, ParseDecimalError> {
41    Decimal::from_str(input).map_err(Into::into)
42}
43
44pub fn parse_to_f64(input: &str) -> Result<f64, ParseFloatError> {
45    input.parse::<f64>()
46}
47
48pub fn serialize_decimal_as_f64<S>(decimal: &Decimal, serializer: S) -> Result<S::Ok, S::Error>
49where
50    S: Serializer,
51{
52    let f64_value = decimal.to_f64().ok_or(serde::ser::Error::custom(
53        "Decimal to f64 conversion failed",
54    ))?;
55    serializer.serialize_f64(f64_value)
56}
57
58pub trait HasId {
59    fn id(&self) -> Option<u32>;
60}