blackline-core 0.3.2

OPC packages, XML DOM, TreeOp, formulas, RFC 5261 patch, and XQuery Update
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! RFC 5261 XML Patch — compile `add` / `replace` / `remove` to [`TreeOp`].
//!
//! This is a **mutation** module. It does not invent a new edit algebra:
//! every operation is a selector plus one of the three IETF verbs, and
//! those verbs compile to [`crate::tree::TreeOp`]. Apply is sequential
//! (each op sees the tree left by the previous one), matching RFC 5261.
//!
//! [RFC 5261](https://www.rfc-editor.org/rfc/rfc5261.html) requires a
//! unique target. Zero or many matches is an error.
//!
//! JSON (agent-first) and the RFC's XML `<diff>` document are both
//! accepted. Namespace-axis patches (`type="namespace::…"`) are out of
//! scope — this crate matches local names, like [`crate::NodePath`].

use serde::{Deserialize, Serialize};

use crate::error::CoreError;
use crate::formula::{self, Match};
use crate::tree::{self, TreeOp, TreeOpReport};
use crate::xml::XmlNode;

/// Where an `<add>` places its payload relative to `sel`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum PatchPos {
    /// Last child of the selected element (RFC 5261 default).
    #[default]
    Append,
    /// First child of the selected element.
    Prepend,
    /// Immediate preceding sibling.
    Before,
    /// Immediate following sibling.
    After,
}

/// One RFC 5261 operation, in the JSON form the CLI prefers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "lowercase")]
pub enum PatchOp {
    /// Add a child, sibling, or attribute.
    Add {
        /// RFC 5261 `sel`.
        sel: String,
        /// `pos`. Default [`PatchPos::Append`].
        #[serde(default)]
        pos: PatchPos,
        /// RFC 5261 `type`. `"@name"` adds an attribute; omit for nodes.
        #[serde(default, rename = "type")]
        type_attr: Option<String>,
        /// Element fragment(s).
        #[serde(default)]
        xml: Option<String>,
        /// Text payload (attribute value, or a text node).
        #[serde(default)]
        text: Option<String>,
    },
    /// Replace the unique target node or attribute.
    Replace {
        /// RFC 5261 `sel`.
        sel: String,
        /// Replacement element.
        #[serde(default)]
        xml: Option<String>,
        /// Replacement text / attribute value.
        #[serde(default)]
        text: Option<String>,
    },
    /// Remove the unique target node or attribute.
    Remove {
        /// RFC 5261 `sel`.
        sel: String,
    },
}

/// Parse `--ops` as a JSON array or an RFC 5261 `<diff>` document.
pub fn parse_ops(raw: &str) -> Result<Vec<PatchOp>, CoreError> {
    let trimmed = raw.trim();
    if trimmed.starts_with('<') {
        parse_rfc_xml(trimmed)
    } else {
        serde_json::from_str(trimmed)
            .map_err(|e| CoreError::patch(format!("invalid patch JSON: {e}")))
    }
}

/// Parse `<diff><add sel="…">…</add>…</diff>` (root name is ignored).
pub fn parse_rfc_xml(xml: &str) -> Result<Vec<PatchOp>, CoreError> {
    let doc = crate::xml::parse(xml.as_bytes()).map_err(|e| CoreError::patch(e.to_string()))?;
    let mut ops = Vec::new();
    for child in doc.root.children() {
        if !child.is_element() {
            continue;
        }
        let sel = child
            .get_attr("sel")
            .ok_or_else(|| CoreError::patch(format!("<{}> missing sel", child.local_name())))?
            .to_string();
        match child.local_name() {
            "add" => {
                let pos = match child.get_attr("pos") {
                    Some("prepend") => PatchPos::Prepend,
                    Some("before") => PatchPos::Before,
                    Some("after") => PatchPos::After,
                    Some("append") | None => PatchPos::Append,
                    Some(other) => {
                        return Err(CoreError::patch(format!("unknown pos '{other}'")));
                    }
                };
                let type_attr = child.get_attr("type").map(str::to_string);
                let (xml, text) = payload_from_node(child);
                ops.push(PatchOp::Add {
                    sel,
                    pos,
                    type_attr,
                    xml,
                    text,
                });
            }
            "replace" => {
                let (xml, text) = payload_from_node(child);
                ops.push(PatchOp::Replace { sel, xml, text });
            }
            "remove" => ops.push(PatchOp::Remove { sel }),
            other => {
                return Err(CoreError::patch(format!(
                    "unknown patch element <{other}>; expected add, replace, or remove"
                )));
            }
        }
    }
    Ok(ops)
}

fn payload_from_node(node: &XmlNode) -> (Option<String>, Option<String>) {
    let elements: Vec<&XmlNode> = node.children().iter().filter(|c| c.is_element()).collect();
    if !elements.is_empty() {
        let xml = elements
            .iter()
            .map(|e| e.to_xml_string())
            .collect::<Vec<_>>()
            .join("");
        return (Some(xml), None);
    }
    let text = node.text_content();
    if text.is_empty() {
        (None, None)
    } else {
        (None, Some(text))
    }
}

/// Compile and apply `ops` left to right (RFC 5261 sequential semantics).
pub fn apply(
    root: &mut XmlNode,
    ops: &[PatchOp],
    strict: bool,
) -> Result<Vec<TreeOpReport>, CoreError> {
    let planned = plan(root, ops)?;
    tree::apply_ops(root, &planned, strict)
}

/// Compile `ops` against a clone so later selectors see earlier edits.
/// The returned [`TreeOp`]s are valid when applied in order to the original.
pub fn plan(root: &XmlNode, ops: &[PatchOp]) -> Result<Vec<TreeOp>, CoreError> {
    let mut clone = root.clone();
    let mut all = Vec::new();
    for op in ops {
        let compiled = compile_one(&clone, op)?;
        tree::apply_ops(&mut clone, &compiled, true)?;
        all.extend(compiled);
    }
    Ok(all)
}

fn compile_one(root: &XmlNode, op: &PatchOp) -> Result<Vec<TreeOp>, CoreError> {
    match op {
        PatchOp::Add {
            sel,
            pos,
            type_attr,
            xml,
            text,
        } => compile_add(
            root,
            sel,
            *pos,
            type_attr.as_deref(),
            xml.as_deref(),
            text.as_deref(),
        ),
        PatchOp::Replace { sel, xml, text } => {
            compile_replace(root, sel, xml.as_deref(), text.as_deref())
        }
        PatchOp::Remove { sel } => compile_remove(root, sel),
    }
}

fn unique_match(root: &XmlNode, sel: &str) -> Result<Match, CoreError> {
    let matches = formula::select_str(root, sel)?;
    match matches.len() {
        1 => Ok(matches.into_iter().next().expect("len 1")),
        0 => Err(CoreError::patch(format!("sel '{sel}' matched nothing"))),
        n => Err(CoreError::patch(format!(
            "sel '{sel}' matched {n} nodes; RFC 5261 requires exactly one"
        ))),
    }
}

fn compile_add(
    root: &XmlNode,
    sel: &str,
    pos: PatchPos,
    type_attr: Option<&str>,
    xml: Option<&str>,
    text: Option<&str>,
) -> Result<Vec<TreeOp>, CoreError> {
    let target = unique_match(root, sel)?;
    if let Some(ty) = type_attr {
        if let Some(name) = ty.strip_prefix('@') {
            let value = text.unwrap_or("").to_string();
            return Ok(vec![TreeOp::SetAttr {
                path: target.node_path()?.to_op_path(),
                name: name.to_string(),
                value,
            }]);
        }
        if ty.starts_with("namespace:") || ty.starts_with("namespace::") {
            return Err(CoreError::patch(
                "namespace-axis add is not supported; this crate matches local names",
            ));
        }
        return Err(CoreError::patch(format!(
            "unsupported add type '{ty}'; use @attr for attributes"
        )));
    }
    if target.attr.is_some() {
        return Err(CoreError::patch(
            "add with an attribute sel needs type=\"@name\" on the parent, or use replace",
        ));
    }
    let payload = payload_xml(xml, text)?;
    insert_ops(root, &target, pos, &payload)
}

fn compile_replace(
    root: &XmlNode,
    sel: &str,
    xml: Option<&str>,
    text: Option<&str>,
) -> Result<Vec<TreeOp>, CoreError> {
    let target = unique_match(root, sel)?;
    if let Some(attr) = target.attr.as_deref() {
        let value = text
            .map(str::to_string)
            .or_else(|| xml.map(str::to_string))
            .unwrap_or_default();
        return Ok(vec![TreeOp::SetAttr {
            path: target.node_path()?.to_op_path(),
            name: attr.to_string(),
            value,
        }]);
    }
    if let Some(fragment) = xml {
        return Ok(vec![TreeOp::Replace {
            path: target.node_path()?.to_op_path(),
            xml: fragment.to_string(),
        }]);
    }
    Ok(vec![TreeOp::SetText {
        path: target.node_path()?.to_op_path(),
        text: text.unwrap_or("").to_string(),
    }])
}

fn compile_remove(root: &XmlNode, sel: &str) -> Result<Vec<TreeOp>, CoreError> {
    let target = unique_match(root, sel)?;
    if let Some(attr) = target.attr.as_deref() {
        return Ok(vec![TreeOp::RemoveAttr {
            path: target.node_path()?.to_op_path(),
            name: attr.to_string(),
        }]);
    }
    let path = target.node_path()?;
    if path.is_root() {
        return Err(CoreError::patch("cannot remove the document element"));
    }
    let index = tree::child_index(root, &path)?;
    let parent = path.parent().expect("not root");
    Ok(vec![TreeOp::RemoveChild {
        path: parent.to_op_path(),
        index,
    }])
}

fn payload_xml(xml: Option<&str>, text: Option<&str>) -> Result<String, CoreError> {
    if let Some(x) = xml {
        return Ok(x.to_string());
    }
    if let Some(t) = text {
        return Ok(t.to_string());
    }
    Err(CoreError::patch("add/replace needs xml or text"))
}

fn insert_ops(
    root: &XmlNode,
    target: &Match,
    pos: PatchPos,
    payload: &str,
) -> Result<Vec<TreeOp>, CoreError> {
    let fragments = tree::parse_fragments(payload)?;
    if fragments.is_empty() {
        return Err(CoreError::patch("add payload is empty"));
    }
    let path = target.node_path()?;
    let mut ops = Vec::new();
    match pos {
        PatchPos::Append => {
            for frag in &fragments {
                ops.push(TreeOp::InsertChild {
                    path: path.to_op_path(),
                    index: None,
                    xml: frag.to_xml_string(),
                });
            }
        }
        PatchPos::Prepend => {
            for (i, frag) in fragments.iter().enumerate() {
                ops.push(TreeOp::InsertChild {
                    path: path.to_op_path(),
                    index: Some(i),
                    xml: frag.to_xml_string(),
                });
            }
        }
        PatchPos::Before | PatchPos::After => {
            if path.is_root() {
                return Err(CoreError::patch(
                    "cannot insert before/after the document element",
                ));
            }
            let mut index = tree::child_index(root, &path)?;
            if pos == PatchPos::After {
                index += 1;
            }
            let parent = path.parent().expect("not root").to_op_path();
            for (i, frag) in fragments.iter().enumerate() {
                ops.push(TreeOp::InsertChild {
                    path: parent.clone(),
                    index: Some(index + i),
                    xml: frag.to_xml_string(),
                });
            }
        }
    }
    Ok(ops)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::formula::eval_str;
    use crate::xml;

    fn sample() -> XmlNode {
        xml::parse(b"<root><p id=\"a\">one</p><p id=\"b\">two</p></root>")
            .unwrap()
            .root
    }

    #[test]
    fn remove_unique_then_add() {
        let mut root = sample();
        apply(
            &mut root,
            &parse_ops(
                r#"[{"op":"remove","sel":"//p[@id=\"a\"]"},{"op":"add","sel":"/","xml":"<p id=\"c\">three</p>"}]"#,
            )
            .unwrap(),
            true,
        )
        .unwrap();
        assert_eq!(eval_str(&root, "count(//p)").unwrap().as_number(), Some(2));
        assert_eq!(
            eval_str(&root, r#"text(//p[@id="c"])"#).unwrap().to_text(),
            "three"
        );
    }

    #[test]
    fn rfc_xml_add_attr_and_replace() {
        let mut root = sample();
        let xml = r#"<diff>
            <add sel="/p[0]" type="@n">9</add>
            <replace sel="/p[1]"><p id="b">TWO</p></replace>
        </diff>"#;
        apply(&mut root, &parse_rfc_xml(xml).unwrap(), true).unwrap();
        assert_eq!(root.child_named("p", 0).unwrap().get_attr("n"), Some("9"));
        assert_eq!(root.child_named("p", 1).unwrap().text_content(), "TWO");
    }

    #[test]
    fn unique_sel_is_required() {
        let root = sample();
        let err = plan(
            &root,
            &parse_ops(r#"[{"op":"remove","sel":"//p"}]"#).unwrap(),
        );
        assert!(err.unwrap_err().to_string().contains("matched 2"));
    }

    #[test]
    fn insert_before_sibling() {
        let mut root = sample();
        apply(
            &mut root,
            &[PatchOp::Add {
                sel: "/p[1]".into(),
                pos: PatchPos::Before,
                type_attr: None,
                xml: Some("<p id=\"m\">mid</p>".into()),
                text: None,
            }],
            true,
        )
        .unwrap();
        assert_eq!(root.child_named("p", 1).unwrap().text_content(), "mid");
    }

    #[test]
    fn remove_attribute_via_sel() {
        let mut root = sample();
        apply(
            &mut root,
            &[PatchOp::Remove {
                sel: "/p[0]/@id".into(),
            }],
            true,
        )
        .unwrap();
        assert!(root.child_named("p", 0).unwrap().get_attr("id").is_none());
    }
}