ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
opts.cpp
Go to the documentation of this file.
1#include <emscripten/bind.h>
2#include "opts.h"
3#include <optional>
4#include <string>
5extern "C" {
9#include <ascii-chat/common.h>
10#include <ascii-chat/log/log.h>
11}
12
14
15int patch_options(OptionsPatch patch, double expected_revision) {
16 emscripten::val object = patch;
17 if (object.isNull() || object.isUndefined() || object.typeOf().as<std::string>() != "object" ||
18 emscripten::val::global("Array").call<bool>("isArray", object))
19 return SET_ERRNO(ERROR_INVALID_PARAM, "Options patch must be an object");
20 emscripten::val json = emscripten::val::global("JSON").call<emscripten::val>("stringify", object);
21 if (json.isUndefined())
22 return SET_ERRNO(ERROR_INVALID_PARAM, "Options patch cannot be serialized");
23 std::string text = json.as<std::string>();
24 return options_apply_json(text.c_str(), text.size(),
25 expected_revision < 0 ? OPTIONS_REVISION_ANY : static_cast<uint64_t>(expected_revision),
26 OPTIONS_ORIGIN_LOCAL, nullptr);
27}
28int initial_options(OptionsPatch patch) {
29 emscripten::val object = patch;
30 if (!object.isNull() && !object.isUndefined() && object.typeOf().as<std::string>() == "object" &&
31 !emscripten::val::global("Array").call<bool>("isArray", object) &&
32 emscripten::val::global("Object").call<emscripten::val>("keys", object)["length"].as<unsigned>() == 0)
33 return ASCIICHAT_OK;
34 int result = patch_options(patch, -1);
35 if (result == ASCIICHAT_OK) {
36 options_snapshot_t *snapshot = SAFE_MALLOC(sizeof(*snapshot), options_snapshot_t *);
37 if (options_read_snapshot(snapshot) == ASCIICHAT_OK)
39 SAFE_FREE(snapshot);
40 }
41 return result;
42}
43static OptionsSnapshot read_options() {
44 options_snapshot_t *snapshot = SAFE_MALLOC(sizeof(*snapshot), options_snapshot_t *);
45 if (options_read_snapshot(snapshot) != ASCIICHAT_OK) {
46 SAFE_FREE(snapshot);
47 SET_ERRNO(ERROR_INVALID_STATE, "Cannot read browser options");
48 return OptionsSnapshot(emscripten::val::null());
49 }
50 char *json = options_values_json(&snapshot->values, false);
51 double revision = static_cast<double>(snapshot->revision);
52 SAFE_FREE(snapshot);
53 if (!json) {
54 SET_ERRNO(ERROR_INVALID_STATE, "Cannot serialize browser options");
55 return OptionsSnapshot(emscripten::val::null());
56 }
57 std::string text(json);
58 SAFE_FREE(json);
59 emscripten::val result = emscripten::val::object();
60 result.set("revision", revision);
61 result.set("values", emscripten::val::global("JSON").call<emscripten::val>("parse", text));
62 return OptionsSnapshot(result);
63}
64static std::optional<std::string> option_help(int mode, const std::string &name) {
65 const char *text = name.empty() ? nullptr : options_get_help_text(static_cast<asciichat_mode_t>(mode), name.c_str());
66 return text ? std::optional<std::string>(text) : std::nullopt;
67}
68
69EMSCRIPTEN_BINDINGS(ascii_chat_options) {
70 emscripten::function("browser_options_revision", +[]() { return static_cast<double>(options_revision()); });
71 emscripten::register_optional<std::string>();
72 emscripten::register_type<OptionsPatch>("OptionsPatch", "Record<string, number | string | boolean>");
73 emscripten::register_type<OptionsSnapshot>(
74 "OptionsSnapshot", "{ revision: number; values: Record<string, number | string | boolean> } | null");
75 emscripten::function("patchOptions", &patch_options);
76 emscripten::function("readOptions", &read_options);
77 emscripten::function("optionHelp", &option_help);
78}
#define SET_ERRNO(code,...)
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_MALLOC(size, cast)
Definition common.h:265
unsigned long long uint64_t
Definition common.h:63
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
void log_set_level(log_level_t level)
Set the minimum log level.
Definition log.c:737
const char * options_get_help_text(asciichat_mode_t mode, const char *option_name)
Get help text for an option in a specific mode.
Definition help_api.c:27
asciichat_mode_t
Mode type for options parsing.
Definition options.h:919
📝 Logging API with multiple log levels and terminal output control
@ OPTIONS_ORIGIN_LOCAL
#define OPTIONS_REVISION_ANY
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
EMSCRIPTEN_BINDINGS(ascii_chat_options)
Definition opts.cpp:69
EMSCRIPTEN_DECLARE_VAL_TYPE(OptionsSnapshot)
int patch_options(OptionsPatch patch, double expected_revision)
Definition opts.cpp:15
int initial_options(OptionsPatch patch)
Definition opts.cpp:28
uint64_t options_revision(void)
Definition rcu.c:876
char * options_values_json(const options_t *values, bool remote_only)
Definition rcu.c:1013
asciichat_error_t options_read_snapshot(options_snapshot_t *snapshot)
Definition rcu.c:865
asciichat_error_t options_apply_json(const char *json, size_t length, uint64_t expected_revision, options_origin_t origin, options_patch_result_t *result)
Definition rcu.c:1000
log_level_t log_level
Log level threshold.
Definition options.h:1167