@@ -25,8 +25,97 @@ pub mod test_utils;
25
25
pub use beacon_provider:: { BeaconProvider , BeaconProviderError , BeaconProviderImpl } ;
26
26
pub use entities:: { CardanoNetwork , MagicId } ;
27
27
28
+ use lazy_static:: lazy_static;
29
+ use semver:: { Version , VersionReq } ;
30
+
28
31
/// Mithril API protocol version
29
32
/// this is the same as the one in openapi.yml file.
30
33
/// If you want to update this version to reflect changes in the protocol,
31
34
/// please also update the entry in the openapi.yml
32
- pub const MITHRIL_API_VERSION : & str = "0.1.0" ;
35
+ pub const MITHRIL_API_VERSION : & str = "0.1.1" ;
36
+
37
+ lazy_static ! {
38
+ /// The [SemVer version requirement][semver::VersionReq] associated with the [MITHRIL_API_VERSION].
39
+ ///
40
+ /// A beta version (0.x.y) will allow all versions within the same major & minor.
41
+ /// A stable version (>=1.x.y) will allow all versions within the same major.
42
+ pub static ref MITHRIL_API_VERSION_REQUIREMENT : VersionReq =
43
+ build_requirement_from_version( & Version :: parse( MITHRIL_API_VERSION ) . unwrap( ) ) ;
44
+ }
45
+
46
+ fn build_requirement_from_version ( version : & Version ) -> VersionReq {
47
+ let mut req_version = version. clone ( ) ;
48
+ req_version. patch = 0 ;
49
+
50
+ if version. major > 0 {
51
+ req_version. minor = 0 ;
52
+ }
53
+
54
+ VersionReq :: parse ( & req_version. to_string ( ) ) . unwrap ( )
55
+ }
56
+
57
+ #[ cfg( test) ]
58
+ mod test {
59
+ use crate :: {
60
+ build_requirement_from_version, MITHRIL_API_VERSION , MITHRIL_API_VERSION_REQUIREMENT ,
61
+ } ;
62
+ use semver:: { Version , VersionReq } ;
63
+
64
+ const API_SPEC_FILE : & str = "../openapi.yaml" ;
65
+
66
+ fn assert_versions_matches ( versions : & [ & str ] , requirement : & VersionReq ) {
67
+ for string in versions {
68
+ let version = Version :: parse ( string) . unwrap ( ) ;
69
+ assert ! (
70
+ requirement. matches( & version) ,
71
+ "Version {} did not match requirement: {}" ,
72
+ & version,
73
+ requirement
74
+ ) ;
75
+ }
76
+ }
77
+
78
+ fn assert_versions_dont_matches ( versions : & [ & str ] , requirement : & VersionReq ) {
79
+ for string in versions {
80
+ let version = Version :: parse ( string) . unwrap ( ) ;
81
+ assert ! (
82
+ !requirement. matches( & version) ,
83
+ "Did not expect that version {} match requirement: {}" ,
84
+ & version,
85
+ requirement
86
+ ) ;
87
+ }
88
+ }
89
+
90
+ #[ test]
91
+ fn test_semver_requirement_matching ( ) {
92
+ let beta_requirement = build_requirement_from_version ( & Version :: parse ( "0.2.4" ) . unwrap ( ) ) ;
93
+ assert_versions_matches ( & [ "0.2.0" , "0.2.4" , "0.2.5" , "0.2.99" ] , & beta_requirement) ;
94
+ assert_versions_dont_matches ( & [ "0.1.10" , "0.3.0" , "1.0.0" ] , & beta_requirement) ;
95
+
96
+ let stable_requirement = build_requirement_from_version ( & Version :: parse ( "2.1.4" ) . unwrap ( ) ) ;
97
+ assert_versions_matches (
98
+ & [ "2.0.0" , "2.1.0" , "2.1.4" , "2.1.5" , "2.12.8" ] ,
99
+ & stable_requirement,
100
+ ) ;
101
+ assert_versions_dont_matches ( & [ "0.0.0" , "1.11.9" , "3.0.0" ] , & stable_requirement) ;
102
+ }
103
+
104
+ #[ test]
105
+ fn requirement_parsed_from_api_version_should_match_said_api_version ( ) {
106
+ let api_version = Version :: parse ( MITHRIL_API_VERSION ) . unwrap ( ) ;
107
+ assert ! ( MITHRIL_API_VERSION_REQUIREMENT . matches( & api_version) ) ;
108
+ }
109
+
110
+ #[ test]
111
+ fn api_version_constant_should_match_version_in_openapi_yaml ( ) {
112
+ let yaml_spec = std:: fs:: read_to_string ( API_SPEC_FILE ) . unwrap ( ) ;
113
+ let openapi: serde_json:: Value = serde_yaml:: from_str ( & yaml_spec) . unwrap ( ) ;
114
+ let openapi_version = openapi[ "info" ] [ "version" ] . as_str ( ) . unwrap ( ) ;
115
+
116
+ assert_eq ! (
117
+ openapi_version, MITHRIL_API_VERSION ,
118
+ "MITHRIL_API_VERSION constant should always be synced with openapi.yaml version"
119
+ ) ;
120
+ }
121
+ }
0 commit comments