refactor(result): add print operator to result

Fixes #6
This commit is contained in:
2026-06-03 16:06:59 +02:00
parent 1b9514c49c
commit ab6b65a70e
2 changed files with 23 additions and 0 deletions
+11
View File
@@ -359,6 +359,17 @@ struct token_error {
* @return true if the errors are not equal.
*/
bool operator!=(const token_error& rhs) const { return !this->operator==(rhs); }
/**
* @brief Prints a token error to an output stream.
*
* @param os Output stream.
* @param error Token error to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const token_error& error) {
return os << error.location << ": error: unknown";
}
};
using token_r = furlang::result<token, token_error>; /**< Alias to a token result. */
+12
View File
@@ -2,6 +2,7 @@
#define FURLANG_RESULT_HPP
#include <exception>
#include <ostream>
#include <utility>
namespace furlang {
@@ -215,6 +216,17 @@ public:
*/
bool operator!=(const value_type& rhs) const { return !this->operator==(rhs); }
/**
* @brief Prints a result to an output stream.
*
* @param os Output stream.
* @param result Result to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const result& result) {
return result.has_value() ? os << result.value() : os << result.error();
}
/**
* @brief Returns a reference to value.
*