use std::str::FromStr;
use bigdecimal::BigDecimal;
use sqlx::postgres::types::Oid;
use sqlx::postgres::{PgRow, PgValueFormat};
use crate::prelude::*;
fn parse_pg_money_text(text: &str) -> Option<BigDecimal> {
let cleaned: String = text.chars().filter(|c| matches!(c, '0'..='9' | '.' | '-')).collect();
BigDecimal::from_str(&cleaned).ok()
}
impl RowExt for PgRow {
fn to_json(&self) -> Value {
row_to_json(self)
}
}
impl DecodeCell for PgRow {
fn decode_cell(&self, idx: usize, type_name: &str) -> Cell {
let cell = match type_name.to_ascii_uppercase().as_str() {
"BOOL" => self.try_get(idx).map_or(Cell::Null, Cell::Bool),
"INT8" => self.try_get(idx).map_or(Cell::Null, Cell::Int),
"INT4" => self.try_get(idx).map_or(Cell::Null, |v: i32| Cell::Int(v.into())),
"OID" => self.try_get(idx).map_or(Cell::Null, |v: Oid| Cell::UInt(v.0.into())),
"INT2" => self.try_get(idx).map_or(Cell::Null, |v: i16| Cell::Int(v.into())),
"NUMERIC" => self.try_get(idx).map_or(Cell::Null, Cell::Decimal),
"MONEY" => text_value(self, idx)
.and_then(parse_pg_money_text)
.map_or(Cell::Null, Cell::Decimal),
"FLOAT4" => self.try_get(idx).map_or(Cell::Null, Cell::Float32),
"FLOAT8" => self.try_get(idx).map_or(Cell::Null, Cell::Float64),
"BYTEA" => self.try_get(idx).map_or(Cell::Null, Cell::Bytes),
"JSON" | "JSONB" => self.try_get(idx).map_or(Cell::Null, Cell::Json),
"DATE" => self.try_get(idx).map_or(Cell::Null, Cell::Date),
"TIME" => self.try_get(idx).map_or(Cell::Null, Cell::Time),
"TIMESTAMP" => self.try_get(idx).map_or(Cell::Null, Cell::DateTime),
"TIMESTAMPTZ" => self.try_get(idx).map_or(Cell::Null, Cell::DateTimeUtc),
_ => text_value(self, idx).map_or_else(
|| self.try_get(idx).map_or(Cell::Null, Cell::Text),
|text| Cell::Text(text.to_owned()),
),
};
match cell {
Cell::Null => text_value(self, idx).map_or(Cell::Null, |text| Cell::Text(text.to_owned())),
cell => cell,
}
}
}
fn text_value(row: &PgRow, idx: usize) -> Option<&str> {
row.try_get_raw(idx)
.ok()
.filter(|v| v.format() == PgValueFormat::Text)
.and_then(|v| v.as_str().ok())
}
#[cfg(test)]
mod tests {
use super::parse_pg_money_text;
use bigdecimal::BigDecimal;
use std::str::FromStr;
fn dec(s: &str) -> BigDecimal {
BigDecimal::from_str(s).expect("valid decimal literal")
}
#[test]
fn parses_plain_money() {
assert_eq!(parse_pg_money_text("$123.45"), Some(dec("123.45")));
}
#[test]
fn parses_money_with_thousand_separators() {
assert_eq!(parse_pg_money_text("$1,234.56"), Some(dec("1234.56")));
assert_eq!(parse_pg_money_text("$1,234,567.89"), Some(dec("1234567.89")));
}
#[test]
fn parses_zero_money() {
assert_eq!(parse_pg_money_text("$0.00"), Some(dec("0")));
}
#[test]
fn parses_negative_money_leading_minus_outside_symbol() {
assert_eq!(parse_pg_money_text("-$99.99"), Some(dec("-99.99")));
}
#[test]
fn parses_negative_money_with_minus_after_symbol() {
assert_eq!(parse_pg_money_text("$-99.99"), Some(dec("-99.99")));
}
#[test]
fn empty_string_returns_none() {
assert!(parse_pg_money_text("").is_none());
}
#[test]
fn unparseable_returns_none() {
assert!(parse_pg_money_text("$.").is_none());
assert!(parse_pg_money_text("abc").is_none());
}
#[test]
fn accounting_parens_misparsed_as_positive() {
assert_eq!(parse_pg_money_text("($99.99)"), Some(dec("99.99")));
}
#[test]
fn large_money_at_i64_max_cents() {
assert_eq!(
parse_pg_money_text("$92,233,720,368,547,758.07"),
Some(dec("92233720368547758.07"))
);
}
}