Rename the Result success/error methods to be consistent with the coding style

This commit is contained in:
Sameer Rahmani 2021-04-20 19:33:25 +01:00
parent 4fde47311e
commit 751eb49538
9 changed files with 17 additions and 71 deletions

View File

@ -39,8 +39,8 @@ namespace serene {
/// factory functions `Success` and `Error`. For example:
///
/// \code
/// auto successfulResult = Result<int>::Success(3);
/// auto notOkResult = Result<int>::Error(SomeLLVMError());
/// auto successfulResult = Result<int>::success(3);
/// auto notOkResult = Result<int>::error(SomeLLVMError());
// \endcode
///
/// In order check for a value being errorful or successful checkout the `ok`
@ -57,13 +57,13 @@ template <typename T, typename E = llvm::Error> class Result {
public:
/// Create a succesfull result with the given value of type `T`.
static Result Success(T v) {
static Result success(T v) {
return Result(std::in_place_index_t<0>(), std::move(v));
}
/// Create an errorful result with the given value of type `E` (default
/// `llvm::Error`).
static Result Error(E e) {
static Result error(E e) {
return Result(std::in_place_index_t<1>(), std::move(e));
}
@ -91,59 +91,5 @@ public:
operator bool() const { return ok(); }
};
/// A similar type to Rust's Result data structure. It either holds a value of
/// type `T` successfully or holds a value of type `E` errorfully. It is
/// designed to be used in situations which the return value of a function might
/// contains some errors. The official way to use this type is to use the
/// factory functions `Success` and `Error`. For example:
///
/// \code
/// auto successfulResult = Result<int>::Success(3);
/// auto notOkResult = Result<int>::Error(SomeLLVMError());
// \endcode
///
/// In order check for a value being errorful or successful checkout the `ok`
/// method or simply use the value as a conditiona.
// template <typename T, typename E = llvm::Error> class SharedResult {
// // The actual data container
// std::variant<std::shared_ptr<T>, E> contents;
// /// The main constructor which we made private to avoid ambiguousness in
// /// input type. `Success` and `Error` call this ctor.
// template <typename InPlace, typename Content>
// SharedResult(InPlace i, Content &&c) : contents(i,
// std::forward<Content>(c)){};
// public:
// /// Create a succesfull result with the given value of type `T`.
// static SharedResult Success(std::shared_ptr<T> v) {
// return Result(std::in_place_index_t<0>(), v);
// }
// /// Create an errorful result with the given value of type `E` (default
// /// `llvm::Error`).
// static SharedResult Error(E e) {
// return Result(std::in_place_index_t<1>(), std::move(e));
// }
// // using std::get, it'll throw if contents doesn't contain what you ask for
// /// Return the value if it's successful otherwise throw an error
// T &getValue() { return std::get<0>(contents); };
// /// Return the error value if it's errorful otherwise throw an error
// E &getError() { return std::get<1>(contents); };
// const T &getValue() const { return std::get<0>(contents); }
// const E &getError() const { return std::get<1>(contents); }
// /// Return the a boolean value indicating whether the value is succesful
// /// or errorful.
// bool ok() const { return std::holds_alternative<T>(contents); };
// operator bool() const { return ok(); }
// };
} // namespace serene
#endif

View File

@ -39,7 +39,7 @@ std::string Error::toString() const {
}
serene::exprs::maybe_node Error::analyze(reader::SemanticContext &ctx) {
return Result<serene::exprs::node>::Success(nullptr);
return Result<serene::exprs::node>::success(nullptr);
};
bool Error::classof(const serene::exprs::Expression *e) {

View File

@ -38,7 +38,7 @@ std::string Def::toString() const {
}
maybe_node Def::analyze(reader::SemanticContext &ctx) {
return Result<node>::Success(nullptr);
return Result<node>::success(nullptr);
};
bool Def::classof(const Expression *e) {

View File

@ -65,7 +65,7 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
if (sym->name == "def") {
if (auto err = Def::isValid(this)) {
// Not a valid `def` form
return Result<node>::Error(std::move(err));
return Result<node>::error(std::move(err));
}
Symbol *binding = llvm::dyn_cast<Symbol>(elements[1].get());
@ -75,7 +75,7 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
}
node def = make<Def>(location, binding->name, elements[2]);
return Result<node>::Success(def);
return Result<node>::success(def);
}
}
@ -84,7 +84,7 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
}
}
return Result<node>::Success(nullptr);
return Result<node>::success(nullptr);
};
bool List::classof(const Expression *e) {

View File

@ -37,7 +37,7 @@ std::string Number::toString() const {
}
maybe_node Number::analyze(reader::SemanticContext &ctx) {
return Result<node>::Success(nullptr);
return Result<node>::success(nullptr);
};
bool Number::classof(const Expression *e) {

View File

@ -37,7 +37,7 @@ std::string Symbol::toString() const {
}
maybe_node Symbol::analyze(reader::SemanticContext &ctx) {
return Result<node>::Success(nullptr);
return Result<node>::success(nullptr);
};
bool Symbol::classof(const Expression *e) {

View File

@ -270,7 +270,7 @@ exprs::maybe_ast Reader::read() {
c = getChar(true);
}
return Result<exprs::ast>::Success(std::move(this->ast));
return Result<exprs::ast>::success(std::move(this->ast));
};
/// Reads the input into an AST and prints it out as string again.
@ -303,7 +303,7 @@ exprs::maybe_ast FileReader::read() {
llvm::errs() << "Could not open input file: " << EC.message() << "\n";
llvm::errs() << llvm::formatv("File: '{0}'\n", file);
llvm::errs() << "Use absolute path for now\n";
return Result<exprs::ast>::Error(llvm::make_error<MissingFileError>(file));
return Result<exprs::ast>::error(llvm::make_error<MissingFileError>(file));
}
reader->setInput(fileOrErr.get()->getBuffer().str());

View File

@ -44,9 +44,9 @@ exprs::maybe_ast Semantics::analyze(exprs::ast &inputAst) {
ast.push_back(element);
}
} else {
Result<exprs::ast>::Error(std::move(maybeNode.getError()));
Result<exprs::ast>::error(std::move(maybeNode.getError()));
}
}
return Result<exprs::ast>::Success(std::move(ast));
return Result<exprs::ast>::success(std::move(ast));
};
}; // namespace serene::reader

View File

@ -26,12 +26,12 @@
namespace serene {
TEST_CASE("Result Type", "[utils]") {
auto r = Result<int>::Success(4);
auto r = Result<int>::success(4);
REQUIRE(r == true);
CHECK(r.getValue() == 4);
auto r1 = Result<int, char>::Error('c');
auto r1 = Result<int, char>::error('c');
REQUIRE_FALSE(r1);
CHECK(r1.getError() == 'c');