Skip to content

Commit b7ab129

Browse files
FaultyRAMbudziq
authored andcommitted
Parse a complex version string.
1 parent 6ffdf8e commit b7ab129

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/app.md

+54
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| [Find all png files recursively][ex-glob-recursive] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
1616
| [Find all files with given pattern ignoring filename case][ex-glob-with] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
1717
| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
18+
| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
1819

1920

2021
[ex-clap-basic]: #ex-clap-basic
@@ -632,6 +633,59 @@ fn run() -> Result<()> {
632633
# quick_main!(run);
633634
```
634635

636+
[ex-semver-complex]: #ex-semver-complex
637+
<a name="ex-semver-complex"></a>
638+
## Parse a complex version string.
639+
640+
[![semver-badge]][semver] [![cat-config-badge]][cat-config]
641+
642+
Constructs a [`semver::Version`] from a complex version string using [`Version::parse`]. The string
643+
contains pre-release and build metadata as defined in the [Semantic Versioning Specification].
644+
645+
Note that, in accordance with the Specification, build metadata is parsed but not considered when
646+
comparing versions. In other words, two versions may be equal even if their build strings differ.
647+
648+
```rust
649+
# #[macro_use]
650+
# extern crate error_chain;
651+
extern crate semver;
652+
653+
use semver::{Identifier, Version};
654+
#
655+
# error_chain! {
656+
# foreign_links {
657+
# SemVer(semver::SemVerError);
658+
# }
659+
# }
660+
661+
fn run() -> Result<()> {
662+
let version_str = "1.0.49-125+g72ee7853";
663+
let parsed_version = Version::parse(version_str)?;
664+
665+
assert_eq!(
666+
parsed_version,
667+
Version {
668+
major: 1,
669+
minor: 0,
670+
patch: 49,
671+
pre: vec![Identifier::Numeric(125)],
672+
build: vec![],
673+
}
674+
);
675+
assert_eq!(
676+
parsed_version.build,
677+
vec![Identifier::AlphaNumeric(String::from("g72ee7853"))]
678+
);
679+
680+
let serialized_version = parsed_version.to_string();
681+
assert_eq!(version_str, &serialized_version);
682+
683+
Ok(())
684+
}
685+
#
686+
# quick_main!(run);
687+
```
688+
635689
{{#include links.md}}
636690

637691
<!-- API Reference -->

src/intro.md

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ community. It needs and welcomes help. For details see
107107
| [Find all png files recursively][ex-glob-recursive] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
108108
| [Find all files with given pattern ignoring filename case][ex-glob-with] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
109109
| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
110+
| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
110111

111112
## [Logging](logging.html)
112113

@@ -190,6 +191,7 @@ community. It needs and welcomes help. For details see
190191
[ex-rest-get]: net.html#ex-rest-get
191192
[ex-rest-head]: net.html#ex-rest-head
192193
[ex-rest-post]: net.html#ex-rest-post
194+
[ex-semver-complex]: app.html#ex-semver-complex
193195
[ex-semver-increment]: app.html#ex-semver-increment
194196
[ex-serialize-csv]: encoding.html#ex-serialize-csv
195197
[ex-sha-digest]: basics.html#ex-sha-digest

0 commit comments

Comments
 (0)