2 releases

0.1.1 Dec 8, 2021
0.1.0 Dec 8, 2021

#86 in #checker

Download history 41/week @ 2023-12-11 204/week @ 2023-12-18 264/week @ 2024-01-08 119/week @ 2024-01-15 39/week @ 2024-01-29 77/week @ 2024-02-05 55/week @ 2024-02-12 125/week @ 2024-02-19 42/week @ 2024-02-26 53/week @ 2024-03-04 13/week @ 2024-03-11 34/week @ 2024-03-18 4/week @ 2024-03-25

106 downloads per month
Used in 2 crates (via serde-semver)

MIT license

6KB
93 lines

serde-semver: Serde-compatible version checker

API doc

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"
}"#,
);

License

MIT license. See LICENSE.txt file.

Dependencies

~1.5MB
~40K SLoC