This library provides custom serde support for converting Rust data structures to and from TXT record format commonly used in DNS TXT records and configuration files.
- Simple key-value pairs:
key: value→key=value - Arrays:
key: ["val", "bal"]→key_0=val, key_1=bal, key_len=2 - Objects:
key: { foo: "val", bar: "bal" }→key.foo=val, key.bar=bal - Record length limits: Each
key=valuerecord can be limited to a maximum length (default: 255 characters) - Configurable separators and suffixes: Customize array separators, object separators, and array length suffixes
- All Rust primitive types: Support for strings, numbers, booleans, options, and more
This library is particularly useful for:
- DNS TXT Records: Storing structured configuration in DNS TXT records
- Configuration Files: Simple key-value configuration format
- Environment Variables: Flattening complex structures for environment variable storage
- Log Structured Data: Converting structured data to searchable key-value pairs
- API Parameters: Flattening nested objects for form-encoded or query parameters
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
Usage
You can use the to_txt_records and from_txt_records functions on a struct that has the Serialize and Deserialize traits, respectively.
use ;
use ;
You can provide a custom configuration as well, to avoid clashing names; use with to_txt_records_with_config and from_txt_records_with_config.
use ;
let config = TxtRecordConfig ;
let data = vec!;
let records = to_txt_records_with_config?;
// 0-0=item1
// 0-1=item2
// 0-2=item3
// 0.count=3
Testing
Run the test suite:
# run an example
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License.