Echo Writes Code

errors.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef CRUCIBLE_BOBA_ERRORS_HPP
#define CRUCIBLE_BOBA_ERRORS_HPP

#include "crucible/core/either.inl"
#include "crucible/core/errors.hpp"
#include "crucible/parsing/errors.inl"

#include <functional>
#include <optional>
#include <string>
#include <variant>

namespace crucible::boba::errors {
  struct ParsingFailed final {
    parsing::errors::ParsingError parsing_error;
  };

  class BobaError final : public core::errors::AbstractError {
  public:
    BobaError(ParsingFailed const &variant);

    ~BobaError() override;

    template<typename T>
    [[nodiscard]]
    auto is() const -> bool {
      return std::holds_alternative<T>(my_variant);
    }

    [[nodiscard]]
    auto describe() const -> std::string override;

    [[nodiscard]]
    auto format() const -> std::string override;

    [[nodiscard]]
    auto source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> override;

  private:
    std::variant<ParsingFailed> my_variant;
  };

  template<typename T>
  using BobaResult = core::either::Either<errors::BobaError, T>;
}

#endif // CRUCIBLE_BOBA_ERRORS_HPP