|
15 | 15 | | [Find all png files recursively][ex-glob-recursive] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
|
16 | 16 | | [Find all files with given pattern ignoring filename case][ex-glob-with] | [![glob-badge]][glob] | [![cat-filesystem-badge]][cat-filesystem] |
|
17 | 17 | | [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] | |
18 | 19 |
|
19 | 20 |
|
20 | 21 | [ex-clap-basic]: #ex-clap-basic
|
@@ -632,6 +633,59 @@ fn run() -> Result<()> {
|
632 | 633 | # quick_main!(run);
|
633 | 634 | ```
|
634 | 635 |
|
| 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 | + |
635 | 689 | {{#include links.md}}
|
636 | 690 |
|
637 | 691 | <!-- API Reference -->
|
|
0 commit comments