## tktax-date
This crate provides robust Serde-compliant serialization and deserialization for the [`NaiveDate`](https://docs.rs/chrono) type from [`chrono`](https://docs.rs/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`](https://docs.rs/serde) `serialize`/`deserialize` traits.
- Zero-boilerplate usage in typical Serde contexts.
### Installation
In your `Cargo.toml`, add:
```toml
[dependencies]
tktax-date = "0.1.0"
chrono = "0.4"
serde = "1.0"
```
### Usage
```rust
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:
```bash
cargo test
```
### License
Licensed under the MIT license. See [LICENSE](LICENSE) for details.
### Contributing
Issues and pull requests are welcome on [GitHub](https://github.com/tktax-org/tktax-date).