]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | semver\r |
2 | ======\r | |
3 | \r | |
4 | Semantic version parsing and comparison.\r | |
5 | \r | |
6 | ![Build Status](https://github.com/steveklabnik/semver/workflows/CI/badge.svg)\r | |
7 | \r | |
8 | [Documentation](https://steveklabnik.github.io/semver)\r | |
9 | \r | |
10 | Semantic versioning (see https://semver.org/) is a set of rules for\r | |
11 | assigning version numbers.\r | |
12 | \r | |
13 | ## SemVer and the Rust ecosystem\r | |
14 | \r | |
15 | Rust itself follows the SemVer specification, as does its standard libraries. The two are\r | |
16 | not tied together.\r | |
17 | \r | |
18 | [Cargo](https://crates.io), Rust's package manager, uses SemVer to determine which versions of\r | |
19 | packages you need installed.\r | |
20 | \r | |
21 | ## Installation\r | |
22 | \r | |
23 | To use `semver`, add this to your `[dependencies]` section:\r | |
24 | \r | |
25 | ```toml\r | |
26 | semver = "0.9.0"\r | |
27 | ```\r | |
28 | \r | |
29 | And this to your crate root:\r | |
30 | \r | |
31 | ```rust\r | |
32 | extern crate semver;\r | |
33 | ```\r | |
34 | \r | |
35 | ## Versions\r | |
36 | \r | |
37 | At its simplest, the `semver` crate allows you to construct `Version` objects using the `parse`\r | |
38 | method:\r | |
39 | \r | |
40 | ```rust\r | |
41 | use semver::Version;\r | |
42 | \r | |
43 | assert!(Version::parse("1.2.3") == Ok(Version {\r | |
44 | major: 1,\r | |
45 | minor: 2,\r | |
46 | patch: 3,\r | |
47 | pre: vec!(),\r | |
48 | build: vec!(),\r | |
49 | }));\r | |
50 | ```\r | |
51 | \r | |
52 | If you have multiple `Version`s, you can use the usual comparison operators to compare them:\r | |
53 | \r | |
54 | ```rust\r | |
55 | use semver::Version;\r | |
56 | \r | |
57 | assert!(Version::parse("1.2.3-alpha") != Version::parse("1.2.3-beta"));\r | |
58 | assert!(Version::parse("1.2.3-alpha2") > Version::parse("1.2.0"));\r | |
59 | ```\r | |
60 | \r | |
61 | ## Requirements\r | |
62 | \r | |
63 | The `semver` crate also provides the ability to compare requirements, which are more complex\r | |
64 | comparisons.\r | |
65 | \r | |
66 | For example, creating a requirement that only matches versions greater than or\r | |
67 | equal to 1.0.0:\r | |
68 | \r | |
69 | ```rust\r | |
70 | use semver::Version;\r | |
71 | use semver::VersionReq;\r | |
72 | \r | |
73 | let r = VersionReq::parse(">= 1.0.0").unwrap();\r | |
74 | let v = Version::parse("1.0.0").unwrap();\r | |
75 | \r | |
76 | assert!(r.to_string() == ">= 1.0.0".to_string());\r | |
77 | assert!(r.matches(&v))\r | |
78 | ```\r | |
79 | \r | |
80 | It also allows parsing of `~x.y.z` and `^x.y.z` requirements as defined at\r | |
81 | https://www.npmjs.com/package/semver\r | |
82 | \r | |
83 | **Tilde requirements** specify a minimal version with some updates:\r | |
84 | \r | |
85 | ```notrust\r | |
86 | ~1.2.3 := >=1.2.3 <1.3.0\r | |
87 | ~1.2 := >=1.2.0 <1.3.0\r | |
88 | ~1 := >=1.0.0 <2.0.0\r | |
89 | ```\r | |
90 | \r | |
91 | **Caret requirements** allow SemVer compatible updates to a specified version,\r | |
92 | `0.x` and `0.x+1` are not considered compatible, but `1.x` and `1.x+1` are.\r | |
93 | \r | |
94 | `0.0.x` is not considered compatible with any other version.\r | |
95 | Missing minor and patch versions are desugared to `0` but allow flexibility for that value.\r | |
96 | \r | |
97 | ```notrust\r | |
98 | ^1.2.3 := >=1.2.3 <2.0.0\r | |
99 | ^0.2.3 := >=0.2.3 <0.3.0\r | |
100 | ^0.0.3 := >=0.0.3 <0.0.4\r | |
101 | ^0.0 := >=0.0.0 <0.1.0\r | |
102 | ^0 := >=0.0.0 <1.0.0\r | |
103 | ```\r |