tktax-date

Crates.iotktax-date
lib.rstktax-date
version0.2.2
created_at2025-02-01 01:40:31.416758+00
updated_at2025-02-01 01:40:31.416758+00
descriptionSerialization/Deserialization of Chrono NaiveDate with flexible US-centric formats.
homepage
repositoryhttps://github.com/klebs6/tktax
max_upload_size
id1537991
size80,338
(klebs6)

documentation

README

tktax-date

This crate provides robust Serde-compliant serialization and deserialization for the NaiveDate type from chrono. It accommodates various US-centric date formats (MM/DD/YYYY, MM/DD/YY, and variants allowing single-digit months/days). This is particularly helpful for systems handling multiple date-string formats in a unified manner.

Features

  1. Multiple Format Parsing

    • Attempts to parse input strings using a list of known patterns.
    • Returns a parsing error if no format successfully matches.
  2. Consistent Serialization

    • Always serializes to the canonical "MM/DD/YYYY" format for uniform downstream processing.
  3. Serde Integration

    • Compatible with serde serialize/deserialize traits.
    • Zero-boilerplate usage in typical Serde contexts.

Installation

In your Cargo.toml, add:

[dependencies]
tktax-date = "0.1.0"
chrono = "0.4"
serde = "1.0"

Usage

use serde::{Serialize, Deserialize};
use chrono::NaiveDate;
use tktax_date::naive_date_format;

#[derive(Serialize, Deserialize)]
struct ExampleRecord {
    // Date field using the tktax-date naive_date_format
    #[serde(
        serialize_with = "naive_date_format::serialize",
        deserialize_with = "naive_date_format::deserialize"
    )]
    recorded_date: NaiveDate,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Demonstration input
    let json_input = r#"{ "recorded_date": "1/2/23" }"#;

    // Deserialize
    let record: ExampleRecord = serde_json::from_str(json_input)?;
    println!("Deserialized Date: {:?}", record.recorded_date);

    // Serialize
    let json_output = serde_json::to_string(&record)?;
    println!("Serialized JSON: {}", json_output);

    Ok(())
}

Testing

Unit tests are included under the serde_naive_date_format_tests module to validate deserialization from various string formats. Run:

cargo test

License

Licensed under the MIT license. See LICENSE for details.

Contributing

Issues and pull requests are welcome on GitHub.

Commit count: 0

cargo fmt