fauna-api 1.0.856

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
//! JavaScript's conversions, so values read from JSON come out as the TypeScript SDK reads them.

use serde_json::Value;

/// `Number(value)` in JavaScript: a missing value is NaN, null is 0.
pub(crate) fn js_number(value: Option<&Value>) -> f64 {
    match value {
        None => f64::NAN,
        Some(Value::Null) => 0.0,
        Some(Value::Bool(value)) => f64::from(u8::from(*value)),
        Some(Value::Number(number)) => number.as_f64().unwrap_or(f64::NAN),
        Some(Value::String(text)) if text.trim().is_empty() => 0.0,
        Some(Value::String(text)) => text.trim().parse().unwrap_or(f64::NAN),
        Some(_) => f64::NAN,
    }
}

/// `String(value)` in JavaScript, with a fallback for a missing or null value.
pub(crate) fn js_string(value: Option<&Value>, fallback: &str) -> String {
    match value {
        None | Some(Value::Null) => fallback.to_string(),
        Some(Value::String(text)) => text.clone(),
        Some(value) => value.to_string(),
    }
}

/// `value != null` in JavaScript: the value, unless it's missing or null.
pub(crate) fn present(value: Option<&Value>) -> Option<&Value> {
    value.filter(|value| !value.is_null())
}