serde_semver/
lib.rs

1//! Serde-compatible version checker.
2//!
3//! The crate lets you build a version checker using [`declare_version`](declare_version) macro.
4//! It is useful to create a versioned configuration with a version number audited during deserialization.
5//!
6//! For example, declare a `MyVersion` checker with the semver requirement `^3.1.4`.
7//!
8//! ```rust
9//! # use serde_semver::SemverReq;
10//! #[derive(SemverReq)]
11//! #[version("3.1.4")]
12//! struct MyVersion;
13//! ```
14//!
15//! We can embed it in the configuration struct. In the following code, it audits the version number
16//! in the JSON text.
17//!
18//! ```rust
19//! use semver::Version;
20//! use serde::{Deserialize, Serialize};
21//! use serde_semver::SemverReq;
22//! use std::path::PathBuf;
23//!
24//! // Declare the vertion type
25//! #[derive(SemverReq)]
26//! #[version("3.1.4")]
27//! struct MyVersion;
28//!
29//! // An example configuration with version tag
30//! #[derive(Serialize, Deserialize)]
31//! struct Config {
32//!     pub version: MyVersion,
33//!     pub input_file: PathBuf,
34//!     pub output_file: PathBuf,
35//! }
36//!
37//! // The version is audited during deserialization.
38//! let config: Config = serde_json::from_str(
39//!     r#"{
40//!   "version": "3.1.4",
41//!   "input_file": "input.txt",
42//!   "output_file": "output.txt"
43//! }"#,
44//! )
45//! .unwrap();
46//!
47//! // The version is recovered after serialization.
48//! assert_eq!(
49//!     serde_json::to_string_pretty(&config).unwrap(),
50//!     r#"{
51//!   "version": "3.1.4",
52//!   "input_file": "input.txt",
53//!   "output_file": "output.txt"
54//! }"#,
55//! );
56//!
57//! // It accepts compatible version during deserialization, such as "3.1.3".
58//! let config: Config = serde_json::from_str(
59//!     r#"{
60//!   "version": "3.1.3",
61//!   "input_file": "input.txt",
62//!   "output_file": "output.txt"
63//! }"#,
64//! )
65//! .unwrap();
66//!
67//! // The version is updated after serialization.
68//! assert_eq!(
69//!     serde_json::to_string_pretty(&config).unwrap(),
70//!     r#"{
71//!   "version": "3.1.4",
72//!   "input_file": "input.txt",
73//!   "output_file": "output.txt"
74//! }"#,
75//! );
76//! ```
77
78pub use semver;
79pub use serde;
80pub use serde_semver_derive::SemverReq;