#pragma once #include #include #include #include #include #include namespace Void::Result { /// `Result` is a thin alias over `std::expected`. /// /// std::expected already provides every monadic operation we need /// (`and_then`, `or_else`, `transform` (= map), `transform_error` /// (= map_err), `value_or`). The wrappers in this module add /// context-chain support (see `with_context` below) and a default /// error type (`Void::Result::Error`). template using Result = std::expected; /// Tag wrapper for failure construction. Shorthand for /// `std::unexpected`. template using Failure = std::unexpected; /// Convenience factory for a successful Result. template [[nodiscard]] constexpr Result> ok(T&& value) { return Result>{std::forward(value)}; } /// Convenience factory for a failed Result with the default Error type. [[nodiscard]] inline Failure fail(std::string message) { return Failure{Error{std::move(message)}}; } /// Zero-allocation Failure factory from a compact Status. [[nodiscard]] inline Failure fail(Status status) noexcept { return Failure{Error{status}}; } /// Failure factory carrying explicit ErrorKind, message, and optional codes. [[nodiscard]] inline Failure fail(ErrorKind kind, std::string message, ErrorCode code = {}, std::error_code sys = {}) { return Failure{ Error{kind, std::move(message), code, sys}}; } /// Convenience factory for a failed Result with an arbitrary E. /// Constrained so string-like types resolve to the `std::string` /// overload above (otherwise a literal `"..."` deduces E to a char /// array). template requires(!std::convertible_to, std::string>) [[nodiscard]] constexpr Failure> fail(E&& error) { return Failure>{std::forward(error)}; } /// Add a context layer to a failed Result. On success, the result is /// returned unchanged. Requires that `E` exposes a /// `with_context(std::string) const&` method (the bundled `Error` /// type does). template [[nodiscard]] Result with_context(Result r, std::string ctx) { if (r) { return r; } return Failure{std::move(r).error().with_context(std::move(ctx))}; } } // namespace Void::Result