: path;
}
-uint64_t
-parse_duration(std::string_view duration)
-{
- uint64_t factor = 0;
- char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1];
-
- switch (last_ch) {
- case 'd':
- factor = 24 * 60 * 60;
- break;
- case 's':
- factor = 1;
- break;
- default:
- throw core::Error(FMT(
- "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration));
- }
-
- const auto value =
- util::parse_unsigned(duration.substr(0, duration.length() - 1));
- if (value) {
- return factor * *value;
- } else {
- throw core::Error(value.error());
- }
-}
-
std::string_view
remove_extension(std::string_view path)
{
// normalized result doesn't resolve to the same file system entry as `path`.
std::string normalize_concrete_absolute_path(const std::string& path);
-// Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix,
-// into seconds. Throws `core::Error` on error.
-uint64_t parse_duration(std::string_view duration);
-
// Return a view into `path` containing the given path without the filename
// extension as determined by `get_extension()`.
std::string_view remove_extension(std::string_view path);
}
case EVICT_OLDER_THAN: {
- evict_max_age = Util::parse_duration(arg);
+ evict_max_age = util::value_or_throw<Error>(util::parse_duration(arg));
break;
}
}
}
+nonstd::expected<uint64_t, std::string>
+parse_duration(std::string_view duration)
+{
+ uint64_t factor = 0;
+ char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1];
+
+ switch (last_ch) {
+ case 'd':
+ factor = 24 * 60 * 60;
+ break;
+ case 's':
+ factor = 1;
+ break;
+ default:
+ return nonstd::make_unexpected(FMT(
+ "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration));
+ }
+
+ auto value = util::parse_unsigned(duration.substr(0, duration.length() - 1));
+ if (!value) {
+ return value;
+ };
+ return factor * *value;
+}
+
nonstd::expected<int64_t, std::string>
parse_signed(std::string_view value,
const std::optional<int64_t> min_value,
// Returns an error string if `value` cannot be parsed as a double.
nonstd::expected<double, std::string> parse_double(const std::string& value);
+// Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix,
+// into seconds.
+nonstd::expected<uint64_t, std::string>
+parse_duration(std::string_view duration);
+
// Parse a string into a signed integer.
//
// Returns an error string if `value` cannot be parsed as an int64_t or if the
#endif
}
-TEST_CASE("Util::parse_duration")
-{
- CHECK(Util::parse_duration("0s") == 0);
- CHECK(Util::parse_duration("2s") == 2);
- CHECK(Util::parse_duration("1d") == 3600 * 24);
- CHECK(Util::parse_duration("2d") == 2 * 3600 * 24);
- CHECK_THROWS_WITH(
- Util::parse_duration("-2"),
- "invalid suffix (supported: d (day) and s (second)): \"-2\"");
- CHECK_THROWS_WITH(
- Util::parse_duration("2x"),
- "invalid suffix (supported: d (day) and s (second)): \"2x\"");
- CHECK_THROWS_WITH(
- Util::parse_duration("2"),
- "invalid suffix (supported: d (day) and s (second)): \"2\"");
-}
-
TEST_CASE("Util::remove_extension")
{
CHECK(Util::remove_extension("") == "");
CHECK(util::parse_double("x").error() == "invalid floating point: \"x\"");
}
+TEST_CASE("util::parse_duration")
+{
+ CHECK(*util::parse_duration("0s") == 0);
+ CHECK(*util::parse_duration("2s") == 2);
+ CHECK(*util::parse_duration("1d") == 3600 * 24);
+ CHECK(*util::parse_duration("2d") == 2 * 3600 * 24);
+ CHECK(util::parse_duration("-2").error()
+ == "invalid suffix (supported: d (day) and s (second)): \"-2\"");
+ CHECK(util::parse_duration("2x").error()
+ == "invalid suffix (supported: d (day) and s (second)): \"2x\"");
+ CHECK(util::parse_duration("2").error()
+ == "invalid suffix (supported: d (day) and s (second)): \"2\"");
+}
+
TEST_CASE("util::parse_signed")
{
CHECK(*util::parse_signed("0") == 0);