1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Serde-compatible version checker.
//!
//! The crate lets you build a version checker using [`declare_version`](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.1.4`.
//!
//! ```rust
//! # use serde_semver::SemverReq;
//! #[derive(SemverReq)]
//! #[version("3.1.4")]
//! struct MyVersion;
//! ```
//!
//! 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 serde_semver::SemverReq;
//! use std::path::PathBuf;
//!
//! // Declare the vertion type
//! #[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"
//! }"#,
//! );
//!
//! // It accepts compatible version during deserialization, such as "3.1.3".
//! let config: Config = serde_json::from_str(
//!     r#"{
//!   "version": "3.1.3",
//!   "input_file": "input.txt",
//!   "output_file": "output.txt"
//! }"#,
//! )
//! .unwrap();
//!
//! // The version is updated after serialization.
//! assert_eq!(
//!     serde_json::to_string_pretty(&config).unwrap(),
//!     r#"{
//!   "version": "3.1.4",
//!   "input_file": "input.txt",
//!   "output_file": "output.txt"
//! }"#,
//! );
//! ```

pub use semver;
pub use serde;
pub use serde_semver_derive::SemverReq;