Skip to content

Commit

Permalink
remove GenericError, use Result's default error type (string)
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Nov 9, 2024
1 parent 4978942 commit 4794c3a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
29 changes: 14 additions & 15 deletions include/matjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ namespace matjson {
class Value;

using Array = std::vector<Value>;
// TODO: change these?
using ParseError = std::string_view;
using GenericError = std::string_view;
// TODO: make a custom type?
using ParseError = std::string;

static constexpr int NO_INDENTATION = 0;
static constexpr int TAB_INDENTATION = -1;
Expand Down Expand Up @@ -127,22 +126,22 @@ namespace matjson {
/// Returns the value associated with the given key
/// @param key Object key
/// @return The value associated with the key, or an error if it does not exist.
geode::Result<Value&, GenericError> get(std::string_view key);
geode::Result<Value&> get(std::string_view key);

/// Returns the value associated with the given key
/// @param key Object key
/// @return The value associated with the key, or an error if it does not exist.
geode::Result<Value const&, GenericError> get(std::string_view key) const;
geode::Result<Value const&> get(std::string_view key) const;

/// Returns the value associated with the given index
/// @param index Array index
/// @return The value associated with the index, or an error if the index is out of bounds.
geode::Result<Value&, GenericError> get(size_t index);
geode::Result<Value&> get(size_t index);

/// Returns the value associated with the given index
/// @param index Array index
/// @return The value associated with the index, or an error if the index is out of bounds.
geode::Result<Value const&, GenericError> get(size_t index) const;
geode::Result<Value const&> get(size_t index) const;

/// Returns the value associated with the given key
/// @param key Object key
Expand Down Expand Up @@ -213,7 +212,7 @@ namespace matjson {
/// If the given indentation is matjson::TAB_INDENTATION, the json is indented with tabs.
/// @param indentationSize The number of spaces to use for indentation
/// @return The JSON string or an error
geode::Result<std::string, GenericError> dump(int indentationSize = 4) const;
geode::Result<std::string> dump(int indentationSize = 4) const;

Type type() const;

Expand Down Expand Up @@ -247,12 +246,12 @@ namespace matjson {
return this->type() == Type::Object;
}

geode::Result<bool, GenericError> asBool() const;
geode::Result<std::string, GenericError> asString() const;
geode::Result<std::intmax_t, GenericError> asInt() const;
geode::Result<std::uintmax_t, GenericError> asUInt() const;
geode::Result<double, GenericError> asDouble() const;
geode::Result<Array, GenericError> asArray() const;
geode::Result<bool> asBool() const;
geode::Result<std::string> asString() const;
geode::Result<std::intmax_t> asInt() const;
geode::Result<std::uintmax_t> asUInt() const;
geode::Result<double> asDouble() const;
geode::Result<Array> asArray() const;

std::optional<std::string> getKey() const;

Expand Down Expand Up @@ -289,7 +288,7 @@ namespace matjson {
return this->asString();
}
else if constexpr (std::is_same_v<T, Value>) {
return geode::Result<Value, GenericError>(geode::Ok(*this));
return geode::Result<Value>(geode::Ok(*this));
}
else {
static_assert(!std::is_same_v<T, T>, "no conversion found from matjson::Value to T");
Expand Down
2 changes: 1 addition & 1 deletion include/matjson/reflect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
template <class T>
requires(std::is_aggregate_v<T> && !std::is_array_v<T>)
struct matjson::Serialize<T> {
static geode::Result<T, std::string_view> fromJson(matjson::Value const& json) {
static geode::Result<T> fromJson(matjson::Value const& json) {
T value;
std::optional<std::string_view> error;
reflect::for_each<T>([&](auto N) {
Expand Down
14 changes: 6 additions & 8 deletions include/matjson/std.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace matjson {

template <class T>
struct Serialize<std::optional<T>> {
static geode::Result<std::optional<T>, std::string_view> fromJson(Value const& value)
static geode::Result<std::optional<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<T>(); }
{
if (!value.isNull()) {
Expand All @@ -37,7 +37,7 @@ namespace matjson {

template <class T>
struct Serialize<std::vector<T>> {
static geode::Result<std::vector<T>, std::string_view> fromJson(Value const& value)
static geode::Result<std::vector<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<T>(); }
{
if (!value.isArray()) return geode::Err("not an array");
Expand All @@ -64,7 +64,7 @@ namespace matjson {

template <class T>
struct Serialize<std::unordered_set<T>> {
static geode::Result<std::unordered_set<T>, std::string_view> fromJson(Value const& value)
static geode::Result<std::unordered_set<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<std::decay_t<T>>(); }
{
if (!value.isArray()) return geode::Err("not an array");
Expand All @@ -91,7 +91,7 @@ namespace matjson {

template <class T>
struct Serialize<std::set<T>> {
static geode::Result<std::set<T>, std::string_view> fromJson(Value const& value)
static geode::Result<std::set<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<std::decay_t<T>>(); }
{
if (!value.isArray()) return geode::Err("not an array");
Expand All @@ -117,7 +117,7 @@ namespace matjson {

template <class T>
struct Serialize<std::map<std::string, T>> {
static geode::Result<std::map<std::string, T>, std::string_view> fromJson(Value const& value)
static geode::Result<std::map<std::string, T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<std::decay_t<T>>(); }
{
if (!value.isObject()) return geode::Err("not an object");
Expand All @@ -142,9 +142,7 @@ namespace matjson {

template <class T>
struct Serialize<std::unordered_map<std::string, T>> {
static geode::Result<std::unordered_map<std::string, T>, std::string_view> fromJson(
Value const& value
)
static geode::Result<std::unordered_map<std::string, T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<std::decay_t<T>>(); }
{
if (!value.isObject()) return geode::Err("not an object");
Expand Down
6 changes: 3 additions & 3 deletions src/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void dumpJsonString(std::string_view str, std::string& out) {
out.push_back('"');
}

Result<void, GenericError> dumpJsonNumber(ValueImpl const& impl, std::string& out) {
Result<void> dumpJsonNumber(ValueImpl const& impl, std::string& out) {
if (impl.isInt()) {
out += std::to_string(impl.asNumber<intmax_t>());
}
Expand Down Expand Up @@ -72,7 +72,7 @@ Result<void, GenericError> dumpJsonNumber(ValueImpl const& impl, std::string& ou
return Ok();
}

Result<void, GenericError> dumpImpl(Value const& value, std::string& out, int indentation, int depth) {
Result<void> dumpImpl(Value const& value, std::string& out, int indentation, int depth) {
auto& impl = ValueImpl::fromValue(value);
switch (value.type()) {
case Type::Null: {
Expand Down Expand Up @@ -143,7 +143,7 @@ Result<void, GenericError> dumpImpl(Value const& value, std::string& out, int in
return Ok();
}

Result<std::string, GenericError> Value::dump(int indentationSize) const {
Result<std::string> Value::dump(int indentationSize) const {
std::string out;
GEODE_UNWRAP(dumpImpl(*this, out, indentationSize, 0));
return Ok(out);
Expand Down
20 changes: 10 additions & 10 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ static Value& asNotConst(Value const& value) {
return const_cast<Value&>(value);
}

Result<Value&, GenericError> Value::get(std::string_view key) {
Result<Value&> Value::get(std::string_view key) {
return std::as_const(*this).get(key).map(asNotConst);
}

Result<Value const&, GenericError> Value::get(std::string_view key) const {
Result<Value const&> Value::get(std::string_view key) const {
if (this->type() != Type::Object) {
return Err("not an object");
}
Expand All @@ -98,11 +98,11 @@ Result<Value const&, GenericError> Value::get(std::string_view key) const {
return Err("key not found");
}

Result<Value&, GenericError> Value::get(size_t index) {
Result<Value&> Value::get(size_t index) {
return std::as_const(*this).get(index).map(asNotConst);
}

Result<Value const&, GenericError> Value::get(size_t index) const {
Result<Value const&> Value::get(size_t index) const {
if (this->type() != Type::Array) {
return Err("not an array");
}
Expand Down Expand Up @@ -249,42 +249,42 @@ void Value::setKey_(std::string_view key) {
return m_impl->setKey(std::string(key));
}

Result<bool, GenericError> Value::asBool() const {
Result<bool> Value::asBool() const {
if (this->type() != Type::Bool) {
return Err("not a bool");
}
return Ok(m_impl->asBool());
}

Result<std::string, GenericError> Value::asString() const {
Result<std::string> Value::asString() const {
if (this->type() != Type::String) {
return Err("not a string");
}
return Ok(m_impl->asString());
}

Result<intmax_t, GenericError> Value::asInt() const {
Result<intmax_t> Value::asInt() const {
if (this->type() != Type::Number) {
return Err("not a number");
}
return Ok(m_impl->asNumber<intmax_t>());
}

Result<uintmax_t, GenericError> Value::asUInt() const {
Result<uintmax_t> Value::asUInt() const {
if (this->type() != Type::Number) {
return Err("not a number");
}
return Ok(m_impl->asNumber<uintmax_t>());
}

Result<double, GenericError> Value::asDouble() const {
Result<double> Value::asDouble() const {
if (this->type() != Type::Number) {
return Err("not a number");
}
return Ok(m_impl->asNumber<double>());
}

Result<Array, GenericError> Value::asArray() const {
Result<Array> Value::asArray() const {
if (this->type() != Type::Array) {
return Err("not an array");
}
Expand Down

0 comments on commit 4794c3a

Please sign in to comment.