Skip to content

Commit

Permalink
Fix some warnings and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
PJutch committed Jul 7, 2024
1 parent aa66754 commit d04fe70
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/JutchsON/escape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace JutchsON {

inline char digitChar(ptrdiff_t digit) {
if (0 <= digit && digit <= 9) {
return '0' + digit;
return static_cast<char>('0' + digit);
} else if (10 <= digit && digit <= 35) {
return 'A' + (digit - 10);
return static_cast<char>('A' + (digit - 10));
} else {
return '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion include/JutchsON/parse/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace JutchsON {

return parseOptional(s).then([&](std::optional<StringView> v) {
if (v) {
return parse<T>(*v, std::forward<Env>(env), context).map([](const T& t) {
return parse<T>(*v, std::forward<Env>(env), quoted ? Context::LINE : context).map([](const T& t) {
return std::optional<T>{t};
});
} else {
Expand Down
10 changes: 5 additions & 5 deletions include/JutchsON/parse/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ namespace JutchsON {
Parser<T>{}(&path, std::forward<Env>(env));
}) {
return Parser<T>{}(&path, std::forward<Env>(env));
}

if (std::filesystem::is_directory(path)) {
return ParseResult<T>::makeError({}, std::format("Expected a file, got directory {}", path.generic_string()));
} else {
return parse<T>(readWholeFile(path), std::forward<Env>(env));
if (std::filesystem::is_directory(path)) {
return ParseResult<T>::makeError({}, std::format("Expected a file, got directory {}", path.generic_string()));
} else {
return parse<T>(readWholeFile(path), std::forward<Env>(env));
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions include/JutchsON/parse/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ namespace JutchsON {
template <typename... Types>
struct Parser<std::variant<Types...>> {
ParseResult<std::variant<Types...>> operator() (StringView s, auto&& env, Context context) {
if (context == Context::OBJECT) {
if (auto stripped = strip(s); stripped.empty() || stripped.front() != '<') {
return ParseResult<std::variant<Types...>>::makeError(stripped.location(), "Expected a nested variant");
}
}

return parseVariant(s).then([&](auto pair) {
auto [typeStr, valueStr] = pair;
return parse<size_t>(typeStr).then([&](size_t type) {
Expand Down
2 changes: 1 addition & 1 deletion include/JutchsON/write/dict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace JutchsON {

template <typename Key, typename Value>
struct Writer<std::unordered_map<Key, Value>> {
std::string operator() (const std::unordered_map<Key, Value>& map, Context context) {
std::string operator() (const std::unordered_map<Key, Value>& map, Context) {
std::vector<std::pair<std::string, std::string>> pairs;
pairs.reserve(std::ssize(map));
for (const auto& [key, value] : map) {
Expand Down
2 changes: 1 addition & 1 deletion include/JutchsON/write/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace JutchsON {
}

template <Tuplelike T>
constexpr bool shouldBeMultiline(const T& tuple) {
constexpr bool shouldBeMultiline(const T&) {
return std::tuple_size_v<T> > 40 || anyForcesMultiline<T, 0>();
}

Expand Down
4 changes: 2 additions & 2 deletions include/JutchsON/write/num.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace JutchsON {
}

while (intpart > 0) {
res.push_back('0' + static_cast<ptrdiff_t>(fmod(intpart, 10)));
res.push_back(static_cast<char>('0' + static_cast<ptrdiff_t>(fmod(intpart, 10))));

intpart /= 10;
modf(intpart, &intpart);
Expand All @@ -67,7 +67,7 @@ namespace JutchsON {

T digit;
fractpart = modf(fractpart, &digit);
res.push_back('0' + static_cast<ptrdiff_t>(digit));
res.push_back(static_cast<char>('0' + static_cast<ptrdiff_t>(digit)));
}

while (res.back() == '0') {
Expand Down
2 changes: 1 addition & 1 deletion include/JutchsON/write/struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace JutchsON {
template <Described T>
struct Writer<T> {
std::string operator() (T t, Context context) {
std::string operator() (T t, Context) {
std::vector<std::pair<std::string, std::string>> pairs;

boost::mp11::mp_for_each<boost::describe::describe_members<T, boost::describe::mod_public>>([&](auto d) {
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down
6 changes: 5 additions & 1 deletion tests/parse/optional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ TEST(Optional, parseStdOpitonalNestedChevronsNull) {
EXPECT_FALSE(**JutchsON::parse<std::optional<std::optional<int>>>("<null>"));
}

TEST(Optional, parseStdOpitonalChevronsContext) {
EXPECT_EQ(**JutchsON::parse<std::optional<std::vector<int>>>("<1 2 3>"), (std::vector<int>{1, 2, 3}));
}

namespace {
struct TestType {};
struct TestEnv {};
Expand All @@ -70,7 +74,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down
7 changes: 6 additions & 1 deletion tests/parse/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ TEST(Variant, parseStdVariantChevronsValueContext) {
EXPECT_EQ((JutchsON::parse<std::variant<int, bool>>("<1>")), (std::variant<int, bool>{true}));
}

TEST(Variant, parseStdVariantNestedCheck) {
EXPECT_EQ((JutchsON::parse<std::variant<int, int>>("0 123", {}, JutchsON::Context::OBJECT)),
(JutchsON::ParseResult<std::variant<int, int>>::makeError({0, 0}, "Expected a nested variant")));
}

namespace {
struct TestType {};
struct TestEnv {};
Expand All @@ -67,7 +72,7 @@ namespace {
namespace JutchsON {
template <>
struct Parser<TestType> {
ParseResult<TestType> operator() (StringView s, TestEnv, Context) {
ParseResult<TestType> operator() (StringView, TestEnv, Context) {
return {{}};
}
};
Expand Down

0 comments on commit d04fe70

Please sign in to comment.