From 8ce397e8c0a83e49e390de9deb73d588e4931ecf Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 29 Oct 2024 21:02:25 -0500 Subject: Reworking of Result. --- include/fud_format.hpp | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'include/fud_format.hpp') diff --git a/include/fud_format.hpp b/include/fud_format.hpp index e156f43..c06643d 100644 --- a/include/fud_format.hpp +++ b/include/fud_format.hpp @@ -125,10 +125,10 @@ struct FormatFill { length = 2; auto fill = data[0]; if (not Ascii::valid(fill)) { - return FudStatus::Utf8Invalid; + return RetType::error(FudStatus::Utf8Invalid); } if (fill == '{' || fill == '}') { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } return RetType::okay(FormatFill{std::move(align2).value(), data[0]}); } @@ -465,45 +465,46 @@ FormatResult vFormat(Sink& sink, FormatCharMode formatMode, FormatString fmt, co template Result getSpecField(FormatSpec& formatSpec, uint32_t index, const FormatArguments& args) { + using RetType = Result; static_assert(not std::is_signed_v); if (not formatSpec.takesPosition() || not formatSpec.takesWidth) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } if (index >= Size) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } if (std::holds_alternative(args[index])) { - return std::get(args[index]); + return RetType::okay(std::get(args[index])); } if (std::holds_alternative(args[index])) { auto value = std::get(args[index]); if (value < 0) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return static_cast(value); + return RetType::okay(static_cast(value)); } if (std::holds_alternative(args[index])) { auto value = std::get(args[index]); if (value < 0 || value > std::numeric_limits::max()) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return static_cast(value); + return RetType::okay(static_cast(value)); } if (std::holds_alternative(args[index])) { - return std::get(args[index]); + return RetType::okay(std::get(args[index])); } if (std::holds_alternative(args[index])) { auto value = std::get(args[index]); if (value > std::numeric_limits::max()) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return static_cast(value); + return RetType::okay(static_cast(value)); } - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } template @@ -514,6 +515,7 @@ Result vFormatPrepareSpec( bool& firstSpec, bool& takesPosition) { + using RetType = Result; if (firstSpec) { firstSpec = false; return vFormatPrepareFirstSpec(scanView, args, argIndex, takesPosition); @@ -528,24 +530,24 @@ Result vFormatPrepareSpec( auto formatSpec{formatSpecResult.getOkay()}; if (takesPosition != formatSpec.takesPosition() || (takesPosition && formatSpec.position >= Size)) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } if (takesPosition) { auto status = vFormatPreparePositionalSpec(formatSpec, args); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } } else { auto status = vFormatPrepareNonPositionalSpec(formatSpec, args, argIndex); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } } scanView.advanceUnsafe(specLength); - return formatSpec; + return RetType::okay(formatSpec); } template @@ -555,6 +557,7 @@ Result vFormatPrepareFirstSpec( const uint32_t& argIndex, bool& takesPosition) { + using RetType = Result; size_t specLength{0}; auto formatSpecResult{FormatSpec::parse(scanView, specLength)}; if (formatSpecResult.isError()) { @@ -567,18 +570,18 @@ Result vFormatPrepareFirstSpec( if (takesPosition) { auto status = vFormatPreparePositionalSpec(formatSpec, args); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } } else { auto status = vFormatPrepareNonPositionalSpec(formatSpec, args, argIndex); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } } scanView.advanceUnsafe(specLength); - return formatSpec; + return RetType::okay(formatSpec); } template -- cgit v1.2.3