box-open-sdk 0.1.1

Generated Box API SDK for Rust (community, unofficial).
Documentation
// Code generated by box-gantry. DO NOT EDIT.

//! Serialization behavioral tests: the write/read tri-state (absent / null /
//! value) and typed date/time round-trips (VR-4).

use crate::serde_helpers::double_option;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct TriState {
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "double_option"
    )]
    field: Option<Option<String>>,
}

#[test]
fn nullable_write_tri_state() {
    // The tri-state is expressed on write (Box clear-on-update): absent omits
    // the key, null serializes null, a value serializes the value.
    let absent = serde_json::to_string(&TriState { field: None }).unwrap();
    assert_eq!(absent, "{}");

    let null = serde_json::to_string(&TriState { field: Some(None) }).unwrap();
    assert_eq!(null, r#"{"field":null}"#);

    let value = serde_json::to_string(&TriState {
        field: Some(Some("x".to_string())),
    })
    .unwrap();
    assert_eq!(value, r#"{"field":"x"}"#);
}

#[test]
fn nullable_read_tri_state() {
    // A present value reads as Some(Some(v)); a present null reads as Some(None)
    // via double_option; an absent field reads as None (serde default).
    let value: TriState = serde_json::from_str(r#"{"field":"y"}"#).unwrap();
    assert_eq!(value.field, Some(Some("y".to_string())));

    let null: TriState = serde_json::from_str(r#"{"field":null}"#).unwrap();
    assert_eq!(null.field, Some(None));

    let absent: TriState = serde_json::from_str("{}").unwrap();
    assert_eq!(absent.field, None);
}

#[test]
fn date_round_trip() {
    // A full-date `NaiveDate` serializes to exactly Box's `2020-01-31` wire form.
    let date: chrono::NaiveDate = serde_json::from_str(r#""2026-07-12""#).unwrap();
    assert_eq!(date.to_string(), "2026-07-12");
    assert_eq!(serde_json::to_string(&date).unwrap(), r#""2026-07-12""#);
}

#[test]
fn date_time_round_trip() {
    // An RFC 3339 `DateTime<Utc>` round-trips through serde without drift.
    let source = r#""2026-07-12T08:30:00Z""#;
    let dt: chrono::DateTime<chrono::Utc> = serde_json::from_str(source).unwrap();
    let encoded = serde_json::to_string(&dt).unwrap();
    let back: chrono::DateTime<chrono::Utc> = serde_json::from_str(&encoded).unwrap();
    assert_eq!(dt, back);
}