| Crates.io | tktax-date |
| lib.rs | tktax-date |
| version | 0.2.2 |
| created_at | 2025-02-01 01:40:31.416758+00 |
| updated_at | 2025-02-01 01:40:31.416758+00 |
| description | Serialization/Deserialization of Chrono NaiveDate with flexible US-centric formats. |
| homepage | |
| repository | https://github.com/klebs6/tktax |
| max_upload_size | |
| id | 1537991 |
| size | 80,338 |
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.
Multiple Format Parsing
Consistent Serialization
"MM/DD/YYYY" format for uniform downstream processing.Serde Integration
serde serialize/deserialize traits.In your Cargo.toml, add:
[dependencies]
tktax-date = "0.1.0"
chrono = "0.4"
serde = "1.0"
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(())
}
Unit tests are included under the serde_naive_date_format_tests module to validate deserialization from various string formats. Run:
cargo test
Licensed under the MIT license. See LICENSE for details.
Issues and pull requests are welcome on GitHub.