diff --git a/vlib/semver/semver.v b/vlib/semver/semver.v index c35bbfb99c..dac89c7058 100644 --- a/vlib/semver/semver.v +++ b/vlib/semver/semver.v @@ -90,6 +90,16 @@ pub fn (v1 Version) le(v2 Version) bool { return compare_le(v1, v2) } +// str returns the `string` representation of the `Version`. +pub fn (ver Version) str() string { + common_string := '${ver.major}.${ver.minor}.$ver.patch' + + prerelease_string := if ver.prerelease.len > 0 { '-$ver.prerelease' } else { '' } + metadata_string := if ver.metadata.len > 0 { '+$ver.metadata' } else { '' } + + return '$common_string$prerelease_string$metadata_string' +} + // * Utilites. // coerce converts the `input` version to a `Version` struct. // coerce will strip any contents *after* the parsed version string: diff --git a/vlib/semver/semver_test.v b/vlib/semver/semver_test.v index 77c8cd7f1e..19e1111d91 100644 --- a/vlib/semver/semver_test.v +++ b/vlib/semver/semver_test.v @@ -86,6 +86,7 @@ fn test_from() { assert ver.patch == item.patch assert ver.metadata == item.metadata assert ver.prerelease == item.prerelease + assert ver.str() == item.raw } for ver in invalid_versions_to_test { semver.from(ver) or {