Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
design_tweaks.hpp
Go to the documentation of this file.
1#pragma once
2
3// pulp::design — the self-describing parameter block an imported/generated
4// design carries so that cheap tweaks survive a reload and live in the diff.
5//
6// An imported artifact (a `ui.js`, a `.dc.html`) embeds one marker-delimited
7// block:
8//
9// /*EDITMODE-BEGIN*/{"accent":"#33aaff","radius":8}/*EDITMODE-END*/
10//
11// The block is the artifact's own parameter store. When a human nudges a knob
12// in the inspector the host rewrites *only the JSON between the markers*,
13// byte-for-byte preserving everything outside it. Because the block is anchored
14// content (fixed markers, not a line/offset), it composes with the re-import
15// safety loop: a re-import re-emits the surrounding artifact and the persisted
16// block is re-applied by key, not by position.
17//
18// This translation unit is the pure core of that mechanism: locate the block,
19// read its parameters, and produce the rewritten artifact text for a changed
20// set of parameters. The inspector panel and the send-to-agent affordance are
21// UI layers built on top of these primitives and live elsewhere.
22//
23// Everything here is deterministic and free of I/O so it is unit-testable
24// without a window or a render context.
25
26#include <optional>
27#include <string>
28#include <string_view>
29#include <vector>
30
31namespace pulp::design {
32
35inline constexpr std::string_view kEditBlockBegin = "/*EDITMODE-BEGIN*/";
36inline constexpr std::string_view kEditBlockEnd = "/*EDITMODE-END*/";
37
42struct TweakParam {
43 std::string key;
44 std::string json_value;
45};
46
51 size_t block_begin = 0;
52 size_t block_end = 0;
53 size_t payload_begin = 0;
54 size_t payload_end = 0;
55};
56
63std::optional<EditBlockSpan> find_edit_block(std::string_view text);
64
69std::optional<std::vector<TweakParam>> read_edit_block(std::string_view text);
70
76std::optional<std::string> edit_block_payload(const std::vector<TweakParam>& params);
77
81 std::string text;
82 bool outside_bytes_intact = false;
83};
84
89std::optional<RewriteResult> rewrite_edit_block(std::string_view text,
90 const std::vector<TweakParam>& params);
91
96std::optional<RewriteResult> set_edit_param(std::string_view text, std::string_view key,
97 std::string_view json_value);
98
99} // namespace pulp::design
Definition design_adherence.hpp:32
std::optional< RewriteResult > rewrite_edit_block(std::string_view text, const std::vector< TweakParam > &params)
Rewrite text's edit block so its payload encodes params, preserving every byte outside the marker spa...
constexpr std::string_view kEditBlockBegin
The exact opening and closing markers that anchor the block.
Definition design_tweaks.hpp:35
std::optional< EditBlockSpan > find_edit_block(std::string_view text)
Locate the first edit block in text.
std::optional< std::string > edit_block_payload(const std::vector< TweakParam > &params)
Serialize parameters to the canonical single-line block payload (no outer markers): {"k1":v1,...
std::optional< std::vector< TweakParam > > read_edit_block(std::string_view text)
Parse the parameters out of text's edit block.
std::optional< RewriteResult > set_edit_param(std::string_view text, std::string_view key, std::string_view json_value)
Apply a single key=value change to text's block: update the key in place if present,...
constexpr std::string_view kEditBlockEnd
Definition design_tweaks.hpp:36
Where the block sits in the host text, as byte offsets.
Definition design_tweaks.hpp:50
size_t block_end
offset one past the last byte of kEditBlockEnd
Definition design_tweaks.hpp:52
size_t payload_begin
offset of the first byte of the JSON payload
Definition design_tweaks.hpp:53
size_t payload_end
offset one past the last byte of the JSON payload
Definition design_tweaks.hpp:54
size_t block_begin
offset of the first byte of kEditBlockBegin
Definition design_tweaks.hpp:51
Result of a rewrite: the new full artifact text plus whether anything outside the block changed (it m...
Definition design_tweaks.hpp:80
std::string text
the artifact with the block's payload replaced
Definition design_tweaks.hpp:81
bool outside_bytes_intact
true iff every byte outside the block is unchanged
Definition design_tweaks.hpp:82
One parameter in the block: a key and its JSON-encoded value.
Definition design_tweaks.hpp:42
std::string key
Definition design_tweaks.hpp:43
std::string json_value
the value's JSON text, exactly as it will serialize
Definition design_tweaks.hpp:44