Crates.io | serde-semver |
lib.rs | serde-semver |
version | 0.2.1 |
source | src |
created_at | 2021-06-14 06:04:05.650521 |
updated_at | 2021-12-08 17:53:50.031613 |
description | Serde-compatible version checker |
homepage | https://github.com/jerry73204/serde-semver |
repository | https://github.com/jerry73204/serde-semver.git |
max_upload_size | |
id | 409808 |
size | 7,424 |
This crate provides a derive macro SemverReq
to build a versioned marker type.
For example, it assocates the type with version "3.1.4".
#[derive(SemverReq)]
#[version("3.1.4")]
struct MyVersion;
The marker type works as a version checker during deserialization. In this example, the marker verifies whether the deserialized JSON text is is compatible with version "3.1.4". For example, "3.1.3" and "3.1.0" are valid versions, but "3.2.0" and "2.7.0" are not.
use semver::Version;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use serde_semver::SemverReq;
#[derive(SemverReq)]
#[version("3.1.4")]
struct MyVersion;
// An example configuration with version tag
#[derive(Serialize, Deserialize)]
struct Config {
pub version: MyVersion,
pub input_file: PathBuf,
pub output_file: PathBuf,
}
// The version is audited during deserialization.
let config: Config = serde_json::from_str(
r#"{
"version": "3.1.4",
"input_file": "input.txt",
"output_file": "output.txt"
}"#,
)
.unwrap();
// The version is recovered after serialization.
assert_eq!(
serde_json::to_string_pretty(&config).unwrap(),
r#"{
"version": "3.1.4",
"input_file": "input.txt",
"output_file": "output.txt"
}"#,
);
MIT license. See LICENSE.txt file.