The crate lets you build a version checker using declare_version
macro.
It is useful to create a versioned configuration with a version number audited during deserialization.
For example, declare a MyVersion
checker with the semver requirement ^3.5.11
.
rust
serde_semver::declare_version!(MyVersion, 3, 5, 11);
We can embed it in the configuration struct. In the following code, it audits the version number in the JSON text.
```rust use semver::Version; use serde::{Deserialize, Serialize}; use std::path::PathBuf;
// Declare the checker type that asserts '3.5.11' serdesemver::declareversion!(MyVersion, 3, 5, 11);
// An example configuration with version tag
struct Config { pub version: MyVersion, pub inputfile: PathBuf, pub outputfile: PathBuf, }
// The version number is audited during deserialization. let config: Config = serdejson::fromstr( r#"{ "version": "3.5.12", "inputfile": "input.txt", "outputfile": "output.txt" }"#, ) .unwrap();
// The original version is recovered after serialization. asserteq!( serdejson::tostringpretty(&config).unwrap(), r#"{ "version": "3.5.12", "inputfile": "input.txt", "outputfile": "output.txt" }"#, );
// Besides deserialization, the version tag can also be created from scratch. let my_ver = MyVersion::new(Version::new(3, 5, 11)).unwrap(); ```
MIT license. See LICENSE.txt file.