sutures 1.0.1

Protocol-agnostic API abstraction gateway
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
429
430
use std::borrow::Cow;
use std::collections::VecDeque;

use super::schema::{Direction, RawSutureSet};
use super::suture::{BindingTaskType, Bindings, ConstantValue, Suture, TrieNode};
use super::validate::{validate_constant, validate_key, validate_terminal};
use crate::error::Error;

// ===========================================================================
// Entry point
// ===========================================================================

/// Compile a single suture_set into a ready-to-use `Suture`.
pub(super) fn compile(set: RawSutureSet) -> Result<Suture, Error> {
    if set.name.is_empty() {
        return Err(Error::Suture("suture set name must not be empty".into()));
    }
    let (binding, constants) = compile_bindings(&set)?;

    Ok(Suture {
        id: set.id.map(Cow::Owned),
        name: Cow::Owned(set.name),
        description: set.description.map(Cow::Owned),
        version: set.version.map(Cow::Owned),
        binding,
        constants,
    })
}

type CompilationResult = (Bindings, Vec<(Cow<'static, str>, ConstantValue)>);

// ===========================================================================
// Core loop
// ===========================================================================

/// One-pass compilation of a suture set into a binding trie + constants.
///
/// Iterates the suture set exactly once. For each entry the processing
/// pipeline runs as a flat state machine:
///
/// ```text
///   Classify → Validate → Split → Build chain → Merge into trie
/// ```
fn compile_bindings(suture_set: &RawSutureSet) -> Result<CompilationResult, Error> {
    let sep = terminal_separator(&suture_set.capture_direction);

    let mut root = TrieNode {
        key: Cow::Owned(suture_set.name.clone()),
        binding: BindingTaskType::Direct,
        targets: vec![],
        children: vec![],
    };
    let mut constants: Vec<(Cow<'static, str>, ConstantValue)> = Vec::new();

    for (i, raw) in suture_set.sutures.iter().enumerate() {
        let ctx = format!("suture[{i}]");

        let obj = raw
            .as_object()
            .ok_or_else(|| Error::Suture(format!("{ctx}: must be an object")))?;

        // Work queue for flattening nested objects without recursion.
        // Uses VecDeque for FIFO ordering (process keys in declaration order).
        let mut queue: VecDeque<(String, serde_json::Value)> =
            obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect();

        while let Some((key, val)) = queue.pop_front() {
            // ── State 1: Classify ──
            if key == "_" {
                compile_constants(&val, &mut constants, &ctx, &suture_set.capture_direction)?;
                continue;
            }

            // ── State 2: Validate ──
            validate_key(&key, &suture_set.capture_direction)
                .map_err(|e| Error::Suture(format!("{ctx}: {e}")))?;

            // ── State 3: Split path ──
            // Strip exactly one leading '/' (response direction), then split
            // on the direction-dependent separator, respecting backtick regions.
            let path = key.strip_prefix('/').unwrap_or(&key);
            let segments: Vec<&str> = if path.is_empty() {
                vec![]
            } else {
                split_path(path, sep)
            };

            // ── State 4: Build chain + merge into trie ──
            match &val {
                serde_json::Value::String(target) => {
                    validate_terminal(target, &suture_set.capture_direction)
                        .map_err(|e| Error::Suture(format!("{ctx}.{key}: {e}")))?;

                    if segments.is_empty() {
                        root.targets.push(Cow::Owned(target.clone()));
                    } else {
                        let child = build_terminal_chain(&segments, target, &ctx)?;
                        merge_into(&mut root, child);
                    }
                }

                serde_json::Value::Array(arr) => {
                    if arr.is_empty() {
                        return Err(Error::Suture(format!(
                            "{ctx}.{key}: array value must not be empty"
                        )));
                    }
                    for item in arr {
                        let target = item.as_str().ok_or_else(|| {
                            Error::Suture(format!("{ctx}.{key}: array items must be strings"))
                        })?;
                        validate_terminal(target, &suture_set.capture_direction)
                            .map_err(|e| Error::Suture(format!("{ctx}.{key}: {e}")))?;

                        if segments.is_empty() {
                            root.targets.push(Cow::Owned(target.to_owned()));
                        } else {
                            let child = build_terminal_chain(&segments, target, &ctx)?;
                            merge_into(&mut root, child);
                        }
                    }
                }

                serde_json::Value::Object(obj) => {
                    for (child_key, child_val) in obj {
                        // Handle nested _ constants before path concatenation.
                        if child_key == "_" {
                            compile_constants(
                                child_val,
                                &mut constants,
                                &ctx,
                                &suture_set.capture_direction,
                            )?;
                        } else {
                            // Bracket-leading child keys (e.g. "[0].text") attach
                            // directly to the parent without an extra separator.
                            let full = if child_key.starts_with(sep) || child_key.starts_with('[') {
                                format!("{}{}", key, child_key)
                            } else {
                                format!("{}{}{}", key, sep, child_key)
                            };
                            queue.push_back((full, child_val.clone()));
                        }
                    }
                }

                _ => {
                    return Err(Error::Suture(format!(
                        "{ctx}.{key}: value must be a string, array, or object"
                    )));
                }
            }
        }
    }

    let binding = match suture_set.capture_direction {
        Direction::Request => Bindings::Request(root),
        Direction::Response => Bindings::Response(root),
    };

    Ok((binding, constants))
}

// ===========================================================================
// Helpers
// ===========================================================================

/// Validate and collect constant entries from a `_` key.
fn compile_constants(
    val: &serde_json::Value,
    constants: &mut Vec<(Cow<'static, str>, ConstantValue)>,
    ctx: &str,
    direction: &Direction,
) -> Result<(), Error> {
    let arr = val
        .as_array()
        .ok_or_else(|| Error::Suture(format!("{ctx}: '_' must be an array")))?;

    for entry in arr {
        let cobj = entry
            .as_object()
            .ok_or_else(|| Error::Suture(format!("{ctx}: constant entry must be an object")))?;
        if cobj.len() != 1 {
            return Err(Error::Suture(format!(
                "{ctx}: constant entry must have exactly one property"
            )));
        }
        let (terminal, v) = cobj.iter().next().unwrap();
        validate_terminal(terminal, direction)
            .map_err(|e| Error::Suture(format!("{ctx}: constant '{terminal}': {e}")))?;
        validate_constant(v).map_err(|e| Error::Suture(format!("{ctx}: {e}")))?;
        let cv = json_to_constant(v, ctx)?;
        constants.push((Cow::Owned(terminal.clone()), cv));
    }
    Ok(())
}

/// Convert a scalar `serde_json::Value` into a `ConstantValue`.
///
/// Called after `validate_constant`, so only scalar variants are reachable.
fn json_to_constant(val: &serde_json::Value, ctx: &str) -> Result<ConstantValue, Error> {
    match val {
        serde_json::Value::Null => Ok(ConstantValue::Null),
        serde_json::Value::Bool(b) => Ok(ConstantValue::Bool(*b)),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Ok(ConstantValue::Int(i))
            } else if let Some(f) = n.as_f64() {
                Ok(ConstantValue::Float(f))
            } else {
                Err(Error::Suture(format!("{ctx}: unsupported number: {n}")))
            }
        }
        serde_json::Value::String(s) => Ok(ConstantValue::String(Cow::Owned(s.clone()))),
        _ => Err(Error::Suture(format!("{ctx}: constant must be a scalar"))),
    }
}

/// Build a chain of TrieNodes from path segments, bottom-up.
///
/// The leaf segment gets the RHS target. Intermediate segments get empty
/// targets — they exist only to describe traversal (Iterate, etc).
fn build_terminal_chain(segments: &[&str], target: &str, ctx: &str) -> Result<TrieNode, Error> {
    let leaf = segments
        .last()
        .ok_or_else(|| Error::Suture(format!("{ctx}: key produced no path segments")))?;

    let mut node = TrieNode {
        key: Cow::Owned(bare_ident(leaf).to_owned()),
        binding: resolve_binding(leaf),
        targets: vec![Cow::Owned(target.to_owned())],
        children: vec![],
    };

    // Walk backwards through intermediate segments, wrapping each as parent.
    for &seg in segments[..segments.len() - 1].iter().rev() {
        node = TrieNode {
            key: Cow::Owned(bare_ident(seg).to_owned()),
            binding: resolve_binding(seg),
            targets: vec![],
            children: vec![node],
        };
    }

    Ok(node)
}

/// Merge a child node into a parent's children list.
///
/// If a child with matching key and binding already exists, merge recursively.
/// Otherwise, append as a new child. Targets are accumulated (fan-out).
fn merge_into(parent: &mut TrieNode, child: TrieNode) {
    let existing = parent
        .children
        .iter_mut()
        .find(|c| c.key == child.key && c.binding == child.binding);

    if let Some(existing_node) = existing {
        existing_node.targets.extend(child.targets);
        for grandchild in child.children {
            merge_into(existing_node, grandchild);
        }
    } else {
        parent.children.push(child);
    }
}

// ===========================================================================
// Binding resolution
// ===========================================================================

/// Resolve a segment into a `BindingTaskType` by examining its syntax.
///
/// - Backtick regex `` `pat` `` → `IteratePattern`
/// - Brackets `[...]` (outside backticks) → `Iterate`
/// - Neither → `Direct`
fn resolve_binding(segment: &str) -> BindingTaskType {
    // ── Try regex pattern first ──
    if let Some(bt_start) = segment.find('`')
        && let Some(rel_end) = segment[bt_start + 1..].find('`')
    {
        let bt_end = rel_end + bt_start + 1;
        let pattern = segment[bt_start + 1..bt_end].to_owned();

        // Slice qualifier after closing backtick: `pat`[1:3]
        let after = &segment[bt_end + 1..];
        let (start, end, step) = if let Some(bs) = after.find('[') {
            if let Some(be) = after[bs..].find(']') {
                parse_range(&after[bs + 1..bs + be])
            } else {
                (None, None, None)
            }
        } else {
            (None, None, None)
        };

        return BindingTaskType::IteratePattern {
            pattern: Cow::Owned(pattern),
            start,
            end,
            step,
        };
    }

    // ── Try bracket range (skip backtick regions) ──
    let bytes = segment.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'`' {
            i += 1;
            while i < bytes.len() && bytes[i] != b'`' {
                i += 1;
            }
            if i < bytes.len() {
                i += 1;
            }
            continue;
        }
        if bytes[i] == b'[' {
            let bracket_start = i + 1;
            i += 1;
            while i < bytes.len() && bytes[i] != b']' {
                i += 1;
            }
            if i < bytes.len() {
                let (start, end, step) = parse_range(&segment[bracket_start..i]);
                return BindingTaskType::Iterate { start, end, step };
            }
        }
        i += 1;
    }

    BindingTaskType::Direct
}

// ===========================================================================
// Reusable leaf utils
// ===========================================================================

/// Split a path on `sep`, respecting backtick-delimited regex regions.
///
/// Unlike `str::split`, this will not split inside backtick pairs, so a regex
/// pattern containing the separator character is kept intact.
fn split_path(path: &str, sep: char) -> Vec<&str> {
    let mut segments = Vec::new();
    let bytes = path.as_bytes();
    let sep_byte = sep as u8;
    let mut start = 0;
    let mut i = 0;
    let mut in_backtick = false;

    while i < bytes.len() {
        if bytes[i] == b'`' {
            in_backtick = !in_backtick;
            i += 1;
            continue;
        }
        if !in_backtick && bytes[i] == sep_byte {
            segments.push(&path[start..i]);
            start = i + 1;
        }
        i += 1;
    }
    segments.push(&path[start..]);
    segments
}

/// Parse `start:end:step` from bracket content. Already validated.
fn parse_range(inner: &str) -> (Option<i64>, Option<i64>, Option<i64>) {
    let parts: Vec<&str> = inner.split(':').collect();
    match parts.len() {
        1 => {
            let n = parts[0].parse::<i64>().ok();
            (n, n.and_then(|v| v.checked_add(1)), Some(1))
        }
        2 => {
            let start = if parts[0].is_empty() {
                None
            } else {
                parts[0].parse().ok()
            };
            let end = if parts[1].is_empty() {
                None
            } else {
                parts[1].parse().ok()
            };
            (start, end, None)
        }
        3 => {
            let start = if parts[0].is_empty() {
                None
            } else {
                parts[0].parse().ok()
            };
            let end = if parts[1].is_empty() {
                None
            } else {
                parts[1].parse().ok()
            };
            let step = if parts[2].is_empty() {
                None
            } else {
                parts[2].parse().ok()
            };
            (start, end, step)
        }
        _ => (None, None, None),
    }
}

/// Return the bare identifier from a segment by stripping brackets and backticks.
///
/// `messages[:]`        → `messages`
/// `items[0]`           → `items`
/// `` `content_\\d+` `` → `content_\\d+` (the regex pattern)
fn bare_ident(key: &str) -> &str {
    // Regex segment — return the pattern as the identifier.
    if let Some(rest) = key.strip_prefix('`') {
        return rest.split('`').next().unwrap_or(rest);
    }
    key.split(['[', '`']).next().unwrap_or(key)
}

/// Direction-dependent path separator.
fn terminal_separator(dir: &Direction) -> char {
    match dir {
        Direction::Request => '.',
        Direction::Response => '/',
    }
}