This commit is contained in:
sfja 2026-03-30 09:46:02 +02:00
parent 5238b38ef0
commit 92eadec0d9
4 changed files with 107 additions and 70 deletions

View File

@ -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

View File

@ -131,7 +131,7 @@ private:
auto make_string_tok(Loc loc, size_t* i) -> Result<Tok>
{
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 <typename Writer> 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, profile>(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, profile>(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<Value>
{
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<Value>(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<Value>(
Type::Object, Data(std::move(object)));
}
}
std::unreachable();
}
auto Value::query(std::string_view path) & -> Result<Value*, std::string>
{
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<std::FILE*, WriteProfile::Minified>(file, *this);
} else {
stringify::write<std::FILE*, WriteProfile::Pretty>(file, *this);
}
}
auto Value::write(std::ostream& stream, WriteProfile profile) const
{
if (profile == WriteProfile::Minified) {

View File

@ -58,35 +58,6 @@ public:
Value& operator=(const Value&) = delete;
Value& operator=(Value&&) = delete;
static auto make_null() -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::Null);
}
static auto make_bool(bool value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(value ? Type::True : Type::False);
}
static auto make_i64(I64 value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::I64, value);
}
static auto make_f64(F64 value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::F64, value);
}
static auto make_string(std::string value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::String, std::move(value));
}
static auto make_array() -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::Array, Array());
}
static auto make_object() -> std::unique_ptr<Value>
{
return std::make_unique<Value>(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<Value>
{
return std::make_unique<Value>(Type::Null);
}
static auto make_bool(bool value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(value ? Type::True : Type::False);
}
static auto make_i64(I64 value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::I64, Data(value));
}
static auto make_f64(F64 value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::F64, Data(value));
}
static auto make_string(std::string value) -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::String, Data(std::move(value)));
}
static auto make_array() -> std::unique_ptr<Value>
{
return std::make_unique<Value>(Type::Array, Data(Array()));
}
static auto make_object() -> std::unique_ptr<Value>
{
return std::make_unique<Value>(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<Value>
{
return std::make_unique<Value>(m_type, Data(m_data));
}
inline auto push(std::unique_ptr<Value> value)
{
get_underlying_array().push_back(std::move(value));
@ -264,12 +259,12 @@ public:
m_data = Object();
}
auto clone() const -> std::unique_ptr<Value>;
auto query(std::string_view path) & -> Result<Value*, std::string>;
auto query(
std::string_view path) const& -> Result<const Value*, std::string>;
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)

View File

@ -1,4 +1,5 @@
#include "event_loop.hpp"
#include "json.hpp"
#include "mqtt.hpp"
#include "server.hpp"
#include <chrono>
@ -12,15 +13,28 @@
#include <sys/types.h>
#include <thread>
#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([&]() {