1 unstable release
Uses new Rust 2024
| 0.2.2 | Feb 1, 2025 |
|---|
#44 in #date-format
149 downloads per month
Used in 19 crates
(via tktax-transaction)
43KB
192 lines
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
-
Multiple Format Parsing
- Attempts to parse input strings using a list of known patterns.
- Returns a parsing error if no format successfully matches.
-
Consistent Serialization
- Always serializes to the canonical
"MM/DD/YYYY"format for uniform downstream processing.
- Always serializes to the canonical
-
Serde Integration
- Compatible with
serdeserialize/deserializetraits. - Zero-boilerplate usage in typical Serde contexts.
- Compatible with
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.
Dependencies
~31–48MB
~702K SLoC