Skip to content

Commit

Permalink
docs: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu committed Oct 21, 2023
1 parent dc96869 commit 648b45c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use nodejs_semver::{Version, VersionDiff};

fn main() {
let v1 = Version::parse("1.2.3-rc.4").unwrap();
let v2 = Version::parse("1.2.3").unwrap();

let diff = v1.diff(&v2);

assert_eq!(diff, Some(VersionDiff::Patch));
}
6 changes: 6 additions & 0 deletions examples/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use nodejs_semver::Version;

fn main() {
let v = Version::parse("1.2.3-rc.4").unwrap();
println!("{}", v);
}
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ impl Version {
}

/// Parse a semver string into a [Version].
///
/// ```rust
#[doc = include_str!("../examples/parse.rs")]
/// ```
pub fn parse<S: AsRef<str>>(input: S) -> Result<Version, SemverError> {
let input = input.as_ref();

Expand Down Expand Up @@ -335,6 +339,10 @@ impl Version {

/// difference between two [Version]s by the release type,
/// or `None` if the [Version]s are the same.
///
/// ```rust
#[doc = include_str!("../examples/diff.rs")]
/// ```
pub fn diff(&self, other: &Self) -> Option<VersionDiff> {
let cmp_result = self.cmp(other);

Expand Down

0 comments on commit 648b45c

Please sign in to comment.