braze-sync 0.14.2

GitOps CLI for managing Braze configuration as code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Placeholder extraction and resolution for `__BRAZESYNC.<type>.<key>__`.
//!
//! Syntax is fixed by RFC `feat-per-env-values.md` §2.3:
//!   - Double-underscore envelope
//!   - Dot namespace
//!   - `<type>` ∈ {`lid`, `cb_id`, `custom`, `global`}
//!   - `<key>` matches `^[a-z][a-z0-9_]*$`
//!
//! This module is intentionally *resource-shape-agnostic*: it returns the
//! `(type, key)` pairs and lets callers (Phase 2+ wiring) pick the right
//! namespace (resource-local vs global, field-scoped vs resource-scoped).

use regex_lite::Regex;
use std::collections::BTreeMap;
use std::sync::OnceLock;

/// Placeholder type. Matches RFC §2.3 enumeration exactly.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PlaceholderType {
    Lid,
    CbId,
    Custom,
    Global,
}

impl PlaceholderType {
    pub fn as_str(&self) -> &'static str {
        match self {
            PlaceholderType::Lid => "lid",
            PlaceholderType::CbId => "cb_id",
            PlaceholderType::Custom => "custom",
            PlaceholderType::Global => "global",
        }
    }

    fn parse(s: &str) -> Option<Self> {
        match s {
            "lid" => Some(Self::Lid),
            "cb_id" => Some(Self::CbId),
            "custom" => Some(Self::Custom),
            "global" => Some(Self::Global),
            _ => None,
        }
    }
}

/// One placeholder occurrence within a body string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Placeholder {
    pub ty: PlaceholderType,
    pub key: String,
    /// Byte offset where the literal `__BRAZESYNC.…__` token begins.
    pub start: usize,
    /// Byte offset (exclusive) where it ends.
    pub end: usize,
}

impl Placeholder {
    /// The textual form, useful for error messages: `__BRAZESYNC.lid.foo__`.
    pub fn literal(&self) -> String {
        format!("__BRAZESYNC.{}.{}__", self.ty.as_str(), self.key)
    }
}

const PREFIX: &str = "__BRAZESYNC.";
const CLOSE: &str = "__";

fn key_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"^[a-z][a-z0-9_]*$").expect("key regex is valid"))
}

/// Loose envelope-only regex per RFC §2.3 warning rule. Catches typos like
/// `__BRAZSYNC.…__` or unknown types like `__BRAZESYNC.url.foo__` so they
/// can be surfaced as warnings rather than silently passing through. The
/// inner classes deliberately allow `_` so typo-shaped placeholders whose
/// key contains an underscore (e.g. `spring_sale`) are still caught.
fn loose_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(r"__BRAZE?SYNC\.[A-Za-z0-9_]+\.[A-Za-z0-9_]+__")
            .expect("loose placeholder regex is valid")
    })
}

/// Extract every strict `__BRAZESYNC.<type>.<key>__` occurrence in `body`,
/// in order of appearance. Parsed by anchoring on the literal `__BRAZESYNC.`
/// prefix and the *nearest* closing `__`, so the envelope is honored even
/// when `<key>` legitimately contains underscores. A regex with a greedy
/// `[a-z0-9_]*` key class would otherwise swallow text across a `__`
/// boundary and merge two intended placeholders into one.
pub fn extract_placeholders(body: &str) -> Vec<Placeholder> {
    let mut out = Vec::new();
    let bytes = body.as_bytes();
    let mut i = 0;
    while i + PREFIX.len() <= bytes.len() {
        let Some(rel) = body[i..].find(PREFIX) else {
            break;
        };
        let start = i + rel;
        let inner_start = start + PREFIX.len();
        let Some(rel_close) = body[inner_start..].find(CLOSE) else {
            break;
        };
        let close_start = inner_start + rel_close;
        let end = close_start + CLOSE.len();
        let inner = &body[inner_start..close_start];
        if let Some((ty_str, key)) = inner.split_once('.') {
            if let (Some(ty), true) = (PlaceholderType::parse(ty_str), key_re().is_match(key)) {
                out.push(Placeholder {
                    ty,
                    key: key.to_string(),
                    start,
                    end,
                });
                i = end;
                continue;
            }
        }
        // Not a valid strict placeholder; skip past the opening `__` so the
        // remainder is still scanned (and may be surfaced by the loose pass).
        i = start + CLOSE.len();
    }
    out
}

/// Find loose envelope matches that don't satisfy the strict pattern.
/// Caller surfaces these as warnings (RFC §2.3).
pub fn find_suspicious_placeholders(body: &str) -> Vec<String> {
    let strict_spans: Vec<(usize, usize)> = extract_placeholders(body)
        .into_iter()
        .map(|p| (p.start, p.end))
        .collect();
    loose_re()
        .find_iter(body)
        .filter(|m| {
            // Overlap (not equality) — the greedy loose regex can extend past
            // a valid envelope into adjacent `__...__` text. See regression
            // tests below.
            !strict_spans
                .iter()
                .any(|&(s, e)| m.start() < e && s < m.end())
        })
        .map(|m| m.as_str().to_string())
        .collect()
}

/// What the resolver couldn't satisfy. Aggregated by the pre-flight phase
/// (RFC §2.4 / §3 Q7) so apply abort can report every failure at once.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResolutionError {
    UnknownKey {
        ty: PlaceholderType,
        key: String,
        start: usize,
    },
    /// Same `lid` key referenced more than once in a single body / field.
    /// RFC §5 edge case: lid is a per-click-context ID so re-use is
    /// conceptually wrong — abort rather than substitute the same value.
    /// `occurrences` holds the byte offsets of every reference so the
    /// failure report can point operators at the duplicates directly.
    DuplicateLidKey {
        key: String,
        occurrences: Vec<usize>,
    },
}

/// Flat key for the resolver's lookup table.
///
/// Phase 1 deliberately stays resource-shape-agnostic: callers supply a
/// flat `(type, key) -> value` map and the resolver doesn't know whether
/// it came from a resource-local namespace, a field-level namespace, or
/// the `globals.custom` scope. Phase 2+ wiring composes the table from
/// the right places per RFC §2.2.
pub type LookupKey = (PlaceholderType, String);

/// Resolve every placeholder in `body` against `lookup`. Returns the
/// resolved body on success, or every unresolved placeholder on failure
/// (errors are aggregated, never short-circuited — matches §3 Q7).
pub fn resolve_placeholders(
    body: &str,
    lookup: &BTreeMap<LookupKey, String>,
) -> Result<String, Vec<ResolutionError>> {
    let placeholders = extract_placeholders(body);
    let mut errors = Vec::new();

    let mut lid_occurrences: BTreeMap<String, Vec<usize>> = BTreeMap::new();
    for ph in &placeholders {
        if matches!(ph.ty, PlaceholderType::Lid) {
            lid_occurrences
                .entry(ph.key.clone())
                .or_default()
                .push(ph.start);
        }
    }
    for (key, occurrences) in lid_occurrences {
        if occurrences.len() > 1 {
            errors.push(ResolutionError::DuplicateLidKey { key, occurrences });
        }
    }

    for ph in &placeholders {
        let key: LookupKey = (ph.ty, ph.key.clone());
        if !lookup.contains_key(&key) {
            errors.push(ResolutionError::UnknownKey {
                ty: ph.ty,
                key: ph.key.clone(),
                start: ph.start,
            });
        }
    }

    if !errors.is_empty() {
        return Err(errors);
    }

    // Substitute back-to-front so byte offsets remain stable across edits.
    let mut out = body.to_string();
    for ph in placeholders.iter().rev() {
        let key: LookupKey = (ph.ty, ph.key.clone());
        let value = lookup
            .get(&key)
            .expect("missing key would have been caught above");
        out.replace_range(ph.start..ph.end, value);
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn lookup(pairs: &[(PlaceholderType, &str, &str)]) -> BTreeMap<LookupKey, String> {
        pairs
            .iter()
            .map(|(t, k, v)| ((*t, (*k).to_string()), (*v).to_string()))
            .collect()
    }

    #[test]
    fn extracts_strict_placeholders_in_order() {
        let body = "head __BRAZESYNC.lid.spring_sale__ mid __BRAZESYNC.cb_id.cb_hero__ tail";
        let found = extract_placeholders(body);
        assert_eq!(found.len(), 2);
        assert_eq!(found[0].ty, PlaceholderType::Lid);
        assert_eq!(found[0].key, "spring_sale");
        assert_eq!(found[1].ty, PlaceholderType::CbId);
        assert_eq!(found[1].key, "cb_hero");
        assert!(found[0].start < found[1].start);
    }

    #[test]
    fn rejects_unknown_type_in_strict_pass() {
        let body = "x __BRAZESYNC.url.foo__ y";
        assert!(extract_placeholders(body).is_empty());
    }

    #[test]
    fn rejects_uppercase_key_in_strict_pass() {
        let body = "x __BRAZESYNC.lid.Foo__ y";
        assert!(extract_placeholders(body).is_empty());
    }

    #[test]
    fn rejects_digit_leading_key_in_strict_pass() {
        let body = "x __BRAZESYNC.lid.1foo__ y";
        assert!(extract_placeholders(body).is_empty());
    }

    #[test]
    fn suspicious_picks_up_typos_and_unknown_types() {
        let body = "x __BRAZSYNC.lid.foo__ y __BRAZESYNC.url.bar__ z";
        let warns = find_suspicious_placeholders(body);
        assert_eq!(warns.len(), 2);
        assert!(warns.iter().any(|s| s.contains("BRAZSYNC")));
        assert!(warns.iter().any(|s| s.contains(".url.")));
    }

    #[test]
    fn suspicious_excludes_strict_matches() {
        let body = "__BRAZESYNC.lid.ok__";
        assert!(find_suspicious_placeholders(body).is_empty());
    }

    #[test]
    fn suspicious_ignores_trailing_double_underscore_text() {
        // Regression: greedy loose regex extends past a valid placeholder
        // into `__bold__`-style adjacent text and reports a span like
        // (0, 26) for `__BRAZESYNC.lid.foo__bar__`. That span overlaps the
        // real strict placeholder (0, 21), so it must not be surfaced.
        let body = "__BRAZESYNC.lid.foo__bar__";
        assert!(find_suspicious_placeholders(body).is_empty());
    }

    #[test]
    fn suspicious_ignores_adjacent_placeholders_sharing_underscores() {
        // Regression: with two adjacent strict placeholders joined by an
        // extra `__`, the loose regex finds a single match that overlaps
        // both strict spans. Overlap means "already covered" — no warning.
        let body = "__BRAZESYNC.lid.foo____BRAZESYNC.lid.bar__";
        assert!(find_suspicious_placeholders(body).is_empty());
    }

    #[test]
    fn resolves_when_all_keys_present() {
        let body = "before __BRAZESYNC.lid.cta__ middle __BRAZESYNC.custom.host__ end";
        let map = lookup(&[
            (PlaceholderType::Lid, "cta", "ai8kexrxcp03"),
            (PlaceholderType::Custom, "host", "api-prod.example.com"),
        ]);
        let resolved = resolve_placeholders(body, &map).unwrap();
        assert_eq!(
            resolved,
            "before ai8kexrxcp03 middle api-prod.example.com end"
        );
    }

    #[test]
    fn resolves_repeated_keys_to_same_value() {
        let body = "__BRAZESYNC.global.host__/a __BRAZESYNC.global.host__/b";
        let map = lookup(&[(PlaceholderType::Global, "host", "example.com")]);
        let resolved = resolve_placeholders(body, &map).unwrap();
        assert_eq!(resolved, "example.com/a example.com/b");
    }

    #[test]
    fn aggregates_unresolved_keys() {
        let body = "__BRAZESYNC.lid.a__ __BRAZESYNC.cb_id.b__ __BRAZESYNC.custom.c__";
        let map = lookup(&[(PlaceholderType::Lid, "a", "ai8kexrxcp03")]);
        let err = resolve_placeholders(body, &map).unwrap_err();
        assert_eq!(err.len(), 2);
        let keys: Vec<_> = err
            .iter()
            .map(|e| match e {
                ResolutionError::UnknownKey { ty, key, .. } => (*ty, key.clone()),
                ResolutionError::DuplicateLidKey { .. } => unreachable!(),
            })
            .collect();
        assert!(keys.contains(&(PlaceholderType::CbId, "b".to_string())));
        assert!(keys.contains(&(PlaceholderType::Custom, "c".to_string())));
    }

    #[test]
    fn placeholder_literal_round_trips() {
        let ph = Placeholder {
            ty: PlaceholderType::CbId,
            key: "cb_hero".into(),
            start: 0,
            end: 0,
        };
        assert_eq!(ph.literal(), "__BRAZESYNC.cb_id.cb_hero__");
    }

    #[test]
    fn duplicate_lid_aborts_with_dedicated_error() {
        let body = "<a>__BRAZESYNC.lid.cta__</a> <a>__BRAZESYNC.lid.cta__</a>";
        let map = lookup(&[(PlaceholderType::Lid, "cta", "ai8kexrxcp03")]);
        let err = resolve_placeholders(body, &map).unwrap_err();
        assert!(err.iter().any(|e| matches!(
            e,
            ResolutionError::DuplicateLidKey { key, occurrences }
                if key == "cta" && occurrences.len() == 2
        )));
    }

    #[test]
    fn duplicate_cb_id_is_not_an_error() {
        // cb_id / custom / global re-use is normal substitution per §5.
        let body = "{{cb.__BRAZESYNC.cb_id.x__}} {{cb.__BRAZESYNC.cb_id.x__}}";
        let map = lookup(&[(PlaceholderType::CbId, "x", "cb42")]);
        let out = resolve_placeholders(body, &map).unwrap();
        assert_eq!(out, "{{cb.cb42}} {{cb.cb42}}");
    }

    #[test]
    fn body_without_placeholders_passes_through() {
        let body = "no placeholders here";
        let map = BTreeMap::new();
        assert_eq!(resolve_placeholders(body, &map).unwrap(), body);
    }

    #[test]
    fn suspicious_catches_typo_with_underscore_key() {
        // Regression: loose regex used to use `[^_\s]+` which excluded
        // underscores, silently letting `__BRAZSYNC.lid.spring_sale__`
        // (missing `E`, underscored key) through without a warning.
        let body = "__BRAZSYNC.lid.spring_sale__";
        let warns = find_suspicious_placeholders(body);
        assert_eq!(warns, vec!["__BRAZSYNC.lid.spring_sale__".to_string()]);
    }

    #[test]
    fn does_not_swallow_text_across_envelope_boundary() {
        // Regression: a regex with a greedy `[a-z0-9_]*` key class merged
        // `__BRAZESYNC.lid.foo__hello__BRAZESYNC.lid.bar__` into one
        // placeholder with key=`foo__hello`. The parser must stop at the
        // nearest `__` so both placeholders are recovered.
        let body = "__BRAZESYNC.lid.foo__hello__BRAZESYNC.lid.bar__";
        let ps = extract_placeholders(body);
        assert_eq!(ps.len(), 2);
        assert_eq!(ps[0].key, "foo");
        assert_eq!(ps[1].key, "bar");
        assert_eq!(&body[ps[0].start..ps[0].end], "__BRAZESYNC.lid.foo__");
        assert_eq!(&body[ps[1].start..ps[1].end], "__BRAZESYNC.lid.bar__");
    }

    #[test]
    fn adjacent_placeholders_share_no_underscore() {
        // `____` between two placeholders: prior greedy regex captured
        // key=`foo__`. The parser should treat the inner `__` as the close.
        let body = "__BRAZESYNC.lid.foo____BRAZESYNC.lid.bar__";
        let ps = extract_placeholders(body);
        assert_eq!(ps.len(), 2);
        assert_eq!(ps[0].key, "foo");
        assert_eq!(ps[1].key, "bar");
    }

    #[test]
    fn underscored_keys_still_extract() {
        // Sanity: legitimate underscored keys (RFC §2.3 allows them) are
        // not broken by the boundary-respecting parser.
        let body = "__BRAZESYNC.lid.spring_sale__ x __BRAZESYNC.custom.api_host__";
        let ps = extract_placeholders(body);
        assert_eq!(ps.len(), 2);
        assert_eq!(ps[0].key, "spring_sale");
        assert_eq!(ps[1].key, "api_host");
    }
}