Skip to content

Commit 473d744

Browse files
authored
Semver: implement tryParse (#1084)
1 parent ddc8c04 commit 473d744

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Manifest.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ Package::tryFromToml(const toml::value& val) noexcept {
6969
auto edition = Try(Edition::tryFromString(
7070
Try(toml::try_find<std::string>(val, "package", "edition"))
7171
));
72-
auto version =
73-
Version::parse(Try(toml::try_find<std::string>(val, "package", "version"))
74-
);
72+
auto version = Try(Version::tryParse(
73+
Try(toml::try_find<std::string>(val, "package", "version"))
74+
));
7575
return Ok(Package(std::move(name), std::move(edition), std::move(version)));
7676
}
7777

src/Semver.cc

+26
Original file line numberDiff line numberDiff line change
@@ -469,18 +469,44 @@ Prerelease::parse(const std::string_view str) {
469469
VersionParser parser(str);
470470
return parser.parsePre();
471471
}
472+
Result<Prerelease>
473+
Prerelease::tryParse(const std::string_view str) noexcept {
474+
try {
475+
return Ok(parse(str));
476+
} catch (const SemverError& e) {
477+
Bail(e.what());
478+
}
479+
}
472480

473481
BuildMetadata
474482
BuildMetadata::parse(const std::string_view str) {
475483
VersionParser parser(str);
476484
return parser.parseBuild();
477485
}
486+
Result<BuildMetadata>
487+
BuildMetadata::tryParse(const std::string_view str) noexcept {
488+
try {
489+
return Ok(parse(str));
490+
} catch (const SemverError& e) {
491+
Bail(e.what());
492+
}
493+
}
478494

479495
Version
480496
Version::parse(const std::string_view str) {
481497
VersionParser parser(str);
482498
return parser.parse();
483499
}
500+
Result<Version>
501+
Version::tryParse(const std::string_view str) noexcept {
502+
try {
503+
return Ok(parse(str));
504+
} catch (const SemverError& e) {
505+
Bail(e.what());
506+
}
507+
}
508+
509+
// FIXME: remove exceptions and use Result entirely.
484510

485511
#ifdef CABIN_TEST
486512

src/Semver.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include "Exception.hpp"
13+
#include "Rustify/Result.hpp"
1314

1415
#include <cstddef>
1516
#include <cstdint>
@@ -57,6 +58,7 @@ struct Prerelease {
5758
std::vector<VersionToken> ident;
5859

5960
static Prerelease parse(std::string_view str);
61+
static Result<Prerelease> tryParse(std::string_view str) noexcept;
6062
bool empty() const noexcept;
6163
std::string toString() const noexcept;
6264
};
@@ -71,6 +73,7 @@ struct BuildMetadata {
7173
std::vector<VersionToken> ident;
7274

7375
static BuildMetadata parse(std::string_view str);
76+
static Result<BuildMetadata> tryParse(std::string_view str) noexcept;
7477
bool empty() const noexcept;
7578
std::string toString() const noexcept;
7679
};
@@ -83,6 +86,7 @@ struct Version {
8386
BuildMetadata build;
8487

8588
static Version parse(std::string_view str);
89+
static Result<Version> tryParse(std::string_view str) noexcept;
8690
std::string toString() const noexcept;
8791
};
8892
std::ostream& operator<<(std::ostream& os, const Version& ver) noexcept;

0 commit comments

Comments
 (0)