diff --git a/backend/Makefile b/backend/Makefile index 44fa4a1..ed14531 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -4,7 +4,7 @@ MAKEFLAGS += -j16 CXX=g++ LD=g++ -CXXFLAGS = -std=c++23 -pedantic-errors -Wall -Wextra -Wconversion +CXXFLAGS = -std=c++23 -pedantic-errors -Wall -Wextra -Wconversion -ftemplate-backtrace-limit=0 LDFLAGS = libraries = libmosquitto diff --git a/backend/src/json.cpp_cant_compile b/backend/src/json.cpp similarity index 89% rename from backend/src/json.cpp_cant_compile rename to backend/src/json.cpp index 8912f47..db699cb 100644 --- a/backend/src/json.cpp_cant_compile +++ b/backend/src/json.cpp @@ -131,7 +131,7 @@ private: auto make_string_tok(Loc loc, size_t* i) -> Result { step(); - while (*i < m_len && m_text[*i] != '\"') { + while (*i < m_len && m_text[*i] != '\"' && m_text[*i] != '\n') { if (m_text[*i] == '\\') { step(); if (*i >= m_len) @@ -139,6 +139,9 @@ private: } step(); } + if (*i < m_len && m_text[*i] != '\n') [[unlikely]] { + return std::unexpected(Error { loc, "malformed string" }); + } if (*i >= m_len && m_text[*i] != '\"') [[unlikely]] { return std::unexpected(Error { loc, "malformed string" }); } @@ -570,7 +573,7 @@ auto string_to_literal(std::string_view text) -> std::string template void write_indent(Writer& writer, int depth) { for (int i = 0; i < depth; ++i) { - std::format(writer, " "); + writer << " "; } } @@ -579,70 +582,70 @@ void write(Writer& writer, const Value& node, int depth = 0) { switch (node.type()) { case Type::Null: - std::format(writer, "null"); + writer << "null"; break; case Type::False: - std::format(writer, "false"); + writer << "false"; break; case Type::True: - std::format(writer, "true"); + writer << "true"; break; case Type::I64: - std::format(writer, "{}", node.get_i64()); + writer << std::format("{}", node.get_i64()); break; case Type::F64: - std::format(writer, "{}", node.get_f64()); + writer << std::format("{}", node.get_f64()); break; case Type::String: - std::format(writer, "{}", string_to_literal(node.get_string())); + writer << string_to_literal(node.get_string()); break; case Type::Array: { - std::format(writer, "["); + writer << "["; auto first = true; for (const auto& child : node.get_underlying_array()) { if (!first) { - std::format(writer, ","); + writer << ","; } first = false; if constexpr (profile == WriteProfile::Pretty) { - std::format(writer, "\n"); + writer << "\n"; write_indent(writer, depth + 1); } - write(writer, child, depth + 1); + write(writer, *child, depth + 1); } if constexpr (profile == WriteProfile::Pretty) { - std::format(writer, "\n"); + writer << "\n"; write_indent(writer, depth); } - std::format(writer, "]"); + writer << "]"; break; } case Type::Object: { - std::format(writer, "{"); + writer << "{"; auto first = true; for (const auto& [key, child] : node.get_underlying_object()) { if (!first) { - std::format(writer, ","); + writer << ","; } first = false; if constexpr (profile == WriteProfile::Pretty) { - std::format(writer, "\n"); + writer << "\n"; write_indent(writer, depth + 1); } - write(writer, string_to_literal(key)); - write(writer, ":"); + writer << string_to_literal(key); + writer << ":"; if constexpr (profile == WriteProfile::Pretty) { - std::format(writer, " "); + writer << " "; } - write(writer, child, depth + 1); + write(writer, *child, depth + 1); } if constexpr (profile == WriteProfile::Pretty) { - std::format(writer, "\n"); + writer << "\n"; write_indent(writer, depth); } - std::format(writer, "}"); + writer << "}"; break; } } @@ -652,6 +655,40 @@ void write(Writer& writer, const Value& node, int depth = 0) namespace mst::json { +auto Value::clone() const -> std::unique_ptr +{ + switch (type()) { + case Type::Null: + return make_null(); + case Type::False: + return make_bool(false); + case Type::True: + return make_bool(true); + case Type::I64: + return make_i64(get_i64()); + case Type::F64: + return make_f64(get_f64()); + case Type::String: + return make_string(get_string()); + case Type::Array: { + auto array = Array(); + for (const auto& val : get_underlying_array()) { + array.push_back(val->clone()); + } + return std::make_unique(Type::Array, Data(std::move(array))); + } + case Type::Object: { + auto object = Object(); + for (const auto& [key, val] : get_underlying_object()) { + object[key] = val->clone(); + } + return std::make_unique( + Type::Object, Data(std::move(object))); + } + } + std::unreachable(); +} + auto Value::query(std::string_view path) & -> Result { auto parser = query::Parser(path); @@ -665,15 +702,6 @@ auto Value::query( return query::resolve(parser, *this); } -auto Value::write(std::FILE* file, WriteProfile profile) const -{ - if (profile == WriteProfile::Minified) { - stringify::write(file, *this); - } else { - stringify::write(file, *this); - } -} - auto Value::write(std::ostream& stream, WriteProfile profile) const { if (profile == WriteProfile::Minified) { diff --git a/backend/src/json.hpp b/backend/src/json.hpp index 116cc0a..6e1013b 100644 --- a/backend/src/json.hpp +++ b/backend/src/json.hpp @@ -58,35 +58,6 @@ public: Value& operator=(const Value&) = delete; Value& operator=(Value&&) = delete; - static auto make_null() -> std::unique_ptr - { - return std::make_unique(Type::Null); - } - static auto make_bool(bool value) -> std::unique_ptr - { - return std::make_unique(value ? Type::True : Type::False); - } - static auto make_i64(I64 value) -> std::unique_ptr - { - return std::make_unique(Type::I64, value); - } - static auto make_f64(F64 value) -> std::unique_ptr - { - return std::make_unique(Type::F64, value); - } - static auto make_string(std::string value) -> std::unique_ptr - { - return std::make_unique(Type::String, std::move(value)); - } - static auto make_array() -> std::unique_ptr - { - return std::make_unique(Type::Array, Array()); - } - static auto make_object() -> std::unique_ptr - { - return std::make_unique(Type::Object, Object()); - } - explicit Value(Type type) : m_type(std::move(type)) , m_data(std::monostate()) @@ -99,6 +70,35 @@ public: { } + static auto make_null() -> std::unique_ptr + { + return std::make_unique(Type::Null); + } + static auto make_bool(bool value) -> std::unique_ptr + { + return std::make_unique(value ? Type::True : Type::False); + } + static auto make_i64(I64 value) -> std::unique_ptr + { + return std::make_unique(Type::I64, Data(value)); + } + static auto make_f64(F64 value) -> std::unique_ptr + { + return std::make_unique(Type::F64, Data(value)); + } + static auto make_string(std::string value) -> std::unique_ptr + { + return std::make_unique(Type::String, Data(std::move(value))); + } + static auto make_array() -> std::unique_ptr + { + return std::make_unique(Type::Array, Data(Array())); + } + static auto make_object() -> std::unique_ptr + { + return std::make_unique(Type::Object, Data(Object())); + } + inline auto type() const -> Type { return m_type; @@ -191,11 +191,6 @@ public: return get_underlying_object().contains(key); } - inline auto clone() const -> std::unique_ptr - { - return std::make_unique(m_type, Data(m_data)); - } - inline auto push(std::unique_ptr value) { get_underlying_array().push_back(std::move(value)); @@ -264,12 +259,12 @@ public: m_data = Object(); } + auto clone() const -> std::unique_ptr; + auto query(std::string_view path) & -> Result; auto query( std::string_view path) const& -> Result; - auto write( - std::FILE* file, WriteProfile profile = WriteProfile::Minified) const; auto write(std::ostream& stream, WriteProfile profile = WriteProfile::Minified) const; auto to_string(WriteProfile profile = WriteProfile::Minified) diff --git a/backend/src/main.cpp b/backend/src/main.cpp index 1e501a7..446968e 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -1,4 +1,5 @@ #include "event_loop.hpp" +#include "json.hpp" #include "mqtt.hpp" #include "server.hpp" #include @@ -12,15 +13,28 @@ #include #include +#ifdef BACKEND_TCP_PORT +#define PORT BACKEND_TCP_PORT +#else #define PORT 8888 +#endif + +#ifndef BACKEND_MQTT_PORT +#define BACKEND_MQTT_PORT 1883 +#endif int main(void) { - auto mqtt_client = mst::mqtt::Client("localhost", 1883, "test", "1234"); + auto mqtt_client + = mst::mqtt::Client("localhost", BACKEND_MQTT_PORT, "test", "1234"); mqtt_client.subscribe("/skateboard/update", [&](std::string_view text) { // std::println("Skateboard: {}", text); + auto parsed = mst::json::parse(text).value(); + + std::println(".acceleration[0]", + parsed->query(".acceleration[0]").value()->get_i64()); }); auto mqtt_thread = std::thread([&]() {