//! Conversion utilities for working with JSON values and decimal types.
//!
//! Provides functions to convert `serde_json::Value` types into `Decimal` values,
//! handling numeric, string, and other JSON types appropriately.

use rust_decimal::Decimal;
use rust_decimal::prelude::FromPrimitive;
use serde_json::Value;
use std::str::FromStr;

/// Convert a JSON value to a Decimal.
///
/// Attempts to convert the provided JSON value into a `Decimal` type. Supports conversion from:
/// - JSON numbers (integers, unsigned integers, and floats)
/// - JSON strings (parsed as decimal strings)
///
/// # Arguments
///
/// * `value` - A reference to a `serde_json::Value` to convert
///
/// # Returns
///
/// Returns `Some(Decimal)` if the conversion succeeds, or `None` if the value type is
/// unsupported or the conversion fails (e.g., invalid decimal string format).
///
/// # Example
///
/// ```ignore
/// use serde_json::json;
/// use rust_decimal::Decimal;
///
/// let num_value = json!(42);
/// assert_eq!(value_to_decimal(&num_value), Some(Decimal::from(42)));
///
/// let str_value = json!("123.45");
/// assert_eq!(value_to_decimal(&str_value), Some(Decimal::from_str("123.45").unwrap()));
///
/// let null_value = json!(null);
/// assert_eq!(value_to_decimal(&null_value), None);
/// ```
pub fn value_to_decimal(value: &Value) -> Option<Decimal> {
    match value {
        Value::Number(num) => {
            if let Some(i) = num.as_i64() {
                Some(Decimal::from(i))
            } else if let Some(u) = num.as_u64() {
                Some(Decimal::from(u))
            } else if let Some(f) = num.as_f64() {
                Decimal::from_f64(f)
            } else {
                None
            }
        }
        Value::String(text) => Decimal::from_str(text).ok(),
        _ => None,
    }
}
