draxl-patch 0.1.1

Structured semantic patch operations over the Draxl model
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use crate::error::{patch_error, PatchError};
use crate::model::{PatchNode, SlotOwner, SlotRef};
use draxl_ast::{Expr, Field, Item, MatchArm, Meta, Param, Pattern, Stmt, Type, Variant};

pub(super) fn slot_ref_label(slot: &SlotRef) -> String {
    format!("{}.{}", slot_owner_label(&slot.owner), slot.slot)
}

pub(super) fn slot_owner_label(owner: &SlotOwner) -> String {
    match owner {
        SlotOwner::File => "file".to_owned(),
        SlotOwner::Node(id) => format!("@{id}"),
    }
}

pub(super) fn patch_node_kind(node: &PatchNode) -> &'static str {
    match node {
        PatchNode::Item(_) => "item",
        PatchNode::Field(_) => "field",
        PatchNode::Variant(_) => "variant",
        PatchNode::Param(_) => "parameter",
        PatchNode::Stmt(_) => "statement",
        PatchNode::MatchArm(_) => "match arm",
        PatchNode::Expr(_) => "expression",
        PatchNode::Type(_) => "type",
        PatchNode::Pattern(_) => "pattern",
        PatchNode::Doc(_) => "doc comment",
        PatchNode::Comment(_) => "line comment",
    }
}

pub(super) fn patch_node_id(node: &PatchNode) -> Option<&str> {
    match node {
        PatchNode::Item(item) => Some(item.meta().id.as_str()),
        PatchNode::Field(field) => Some(field.meta.id.as_str()),
        PatchNode::Variant(variant) => Some(variant.meta.id.as_str()),
        PatchNode::Param(param) => Some(param.meta.id.as_str()),
        PatchNode::Stmt(stmt) => stmt_id(stmt),
        PatchNode::MatchArm(arm) => Some(arm.meta.id.as_str()),
        PatchNode::Expr(expr) => expr_id(expr),
        PatchNode::Type(ty) => Some(ty.meta().id.as_str()),
        PatchNode::Pattern(pattern) => pattern_id(pattern),
        PatchNode::Doc(node) => Some(node.meta.id.as_str()),
        PatchNode::Comment(node) => Some(node.meta.id.as_str()),
    }
}

pub(super) fn require_insert_fragment(node: &PatchNode) -> Result<(), PatchError> {
    let Some(id) = patch_node_id(node) else {
        return Err(patch_error(&format!(
            "{} fragments must carry an outer node id",
            patch_node_kind(node)
        )));
    };
    ensure_fragment_meta_is_body_only(node, id)
}

pub(super) fn require_put_fragment(node: &PatchNode) -> Result<(), PatchError> {
    let Some(id) = patch_node_id(node) else {
        return Err(patch_error(&format!(
            "{} fragments must carry an outer node id",
            patch_node_kind(node)
        )));
    };
    ensure_fragment_meta_is_body_only(node, id)
}

pub(super) fn require_replace_fragment(
    node: &PatchNode,
    target_id: &str,
) -> Result<(), PatchError> {
    ensure_fragment_meta_is_body_only(node, target_id)
}

fn ensure_fragment_meta_is_body_only(
    node: &PatchNode,
    expected_id: &str,
) -> Result<(), PatchError> {
    let kind = patch_node_kind(node);
    match node {
        PatchNode::Item(item) => validate_fragment_meta(item.meta(), expected_id, kind),
        PatchNode::Field(field) => validate_fragment_meta(&field.meta, expected_id, kind),
        PatchNode::Variant(variant) => validate_fragment_meta(&variant.meta, expected_id, kind),
        PatchNode::Param(param) => validate_fragment_meta(&param.meta, expected_id, kind),
        PatchNode::Stmt(stmt) => validate_fragment_meta(
            stmt.meta()
                .ok_or_else(|| patch_error("statement fragments must carry metadata"))?,
            expected_id,
            kind,
        ),
        PatchNode::MatchArm(arm) => validate_fragment_meta(&arm.meta, expected_id, kind),
        PatchNode::Expr(expr) => {
            if let Some(meta) = expr.meta() {
                validate_fragment_meta(meta, expected_id, kind)?;
            }
            Ok(())
        }
        PatchNode::Type(ty) => validate_fragment_meta(ty.meta(), expected_id, kind),
        PatchNode::Pattern(pattern) => {
            if let Some(meta) = pattern.meta() {
                validate_fragment_meta(meta, expected_id, kind)?;
            }
            Ok(())
        }
        PatchNode::Doc(node) => validate_fragment_meta(&node.meta, expected_id, kind),
        PatchNode::Comment(node) => validate_fragment_meta(&node.meta, expected_id, kind),
    }
}

fn validate_fragment_meta(meta: &Meta, expected_id: &str, kind: &str) -> Result<(), PatchError> {
    if meta.id != expected_id {
        return Err(patch_error(&format!(
            "{kind} fragment id `{}` does not match the target id `{expected_id}`",
            meta.id
        )));
    }
    if meta.rank.is_some() {
        return Err(patch_error(&format!(
            "{kind} fragment for `{expected_id}` must omit outer rank metadata"
        )));
    }
    if meta.slot.is_some() {
        return Err(patch_error(&format!(
            "{kind} fragment for `{expected_id}` must omit outer slot metadata"
        )));
    }
    if meta.anchor.is_some() {
        return Err(patch_error(&format!(
            "{kind} fragment for `{expected_id}` must omit outer anchor metadata"
        )));
    }
    Ok(())
}

pub(super) fn stmt_id(stmt: &Stmt) -> Option<&str> {
    match stmt {
        Stmt::Let(node) => Some(node.meta.id.as_str()),
        Stmt::Expr(node) => Some(node.meta.id.as_str()),
        Stmt::Item(item) => Some(item.meta().id.as_str()),
        Stmt::Doc(node) => Some(node.meta.id.as_str()),
        Stmt::Comment(node) => Some(node.meta.id.as_str()),
    }
}

pub(super) fn expr_id(expr: &Expr) -> Option<&str> {
    expr.meta().map(|meta| meta.id.as_str())
}

pub(super) fn pattern_id(pattern: &Pattern) -> Option<&str> {
    pattern.meta().map(|meta| meta.id.as_str())
}

pub(super) fn assign_item_slot_and_rank(
    item: &mut Item,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) -> Result<(), PatchError> {
    match item {
        Item::Mod(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Item::Use(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Item::Struct(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Item::Enum(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Item::Fn(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Item::Doc(node) => assign_meta_slot_and_rank(&mut node.meta, slot, None, true),
        Item::Comment(node) => assign_meta_slot_and_rank(&mut node.meta, slot, None, true),
    }
    Ok(())
}

pub(super) fn assign_stmt_slot_and_rank(
    stmt: &mut Stmt,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) -> Result<(), PatchError> {
    match stmt {
        Stmt::Let(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Stmt::Expr(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
        Stmt::Item(item) => assign_item_slot_and_rank(item, slot, rank, overwrite_rank)?,
        Stmt::Doc(node) => assign_meta_slot_and_rank(&mut node.meta, slot, None, true),
        Stmt::Comment(node) => assign_meta_slot_and_rank(&mut node.meta, slot, None, true),
    }
    Ok(())
}

pub(super) fn assign_expr_slot_and_rank(
    expr: &mut Expr,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) {
    if let Some(meta) = expr.meta_mut() {
        assign_meta_slot_and_rank(meta, slot, rank, overwrite_rank);
    }
}

pub(super) fn assign_pattern_slot_and_rank(
    pattern: &mut Pattern,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) {
    match pattern {
        Pattern::Ident(node) => {
            if let Some(meta) = &mut node.meta {
                assign_meta_slot_and_rank(meta, slot, rank, overwrite_rank);
            }
        }
        Pattern::Wild(node) => {
            if let Some(meta) = &mut node.meta {
                assign_meta_slot_and_rank(meta, slot, rank, overwrite_rank);
            }
        }
    }
}

pub(super) fn assign_type_slot_and_rank(
    ty: &mut Type,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) {
    match ty {
        Type::Path(node) => assign_meta_slot_and_rank(&mut node.meta, slot, rank, overwrite_rank),
    }
}

pub(super) fn assign_meta_slot_and_rank(
    meta: &mut Meta,
    slot: &str,
    rank: Option<&str>,
    overwrite_rank: bool,
) {
    meta.slot = Some(slot.to_owned());
    if overwrite_rank || meta.rank.is_none() {
        meta.rank = rank.map(str::to_owned);
    }
    meta.span = None;
}

pub(super) fn apply_shell_to_item(item: &mut Item, shell: &Meta) {
    apply_shell_to_meta(item.meta_mut(), shell);
}

pub(super) fn apply_shell_to_field(field: &mut Field, shell: &Meta) {
    apply_shell_to_meta(&mut field.meta, shell);
}

pub(super) fn apply_shell_to_variant(variant: &mut Variant, shell: &Meta) {
    apply_shell_to_meta(&mut variant.meta, shell);
}

pub(super) fn apply_shell_to_param(param: &mut Param, shell: &Meta) {
    apply_shell_to_meta(&mut param.meta, shell);
}

pub(super) fn apply_shell_to_stmt(stmt: &mut Stmt, shell: &Meta) {
    match stmt {
        Stmt::Let(node) => apply_shell_to_meta(&mut node.meta, shell),
        Stmt::Expr(node) => apply_shell_to_meta(&mut node.meta, shell),
        Stmt::Item(item) => apply_shell_to_item(item, shell),
        Stmt::Doc(node) => apply_shell_to_meta(&mut node.meta, shell),
        Stmt::Comment(node) => apply_shell_to_meta(&mut node.meta, shell),
    }
}

pub(super) fn apply_shell_to_expr(expr: &mut Expr, shell: &Meta) {
    ensure_expr_meta(expr, shell);
    apply_shell_to_meta(
        expr.meta_mut()
            .expect("expression shell application must leave metadata present"),
        shell,
    );
}

pub(super) fn apply_shell_to_pattern(pattern: &mut Pattern, shell: &Meta) {
    ensure_pattern_meta(pattern, shell);
    match pattern {
        Pattern::Ident(node) => apply_shell_to_meta(
            node.meta
                .as_mut()
                .expect("pattern shell application must leave metadata present"),
            shell,
        ),
        Pattern::Wild(node) => apply_shell_to_meta(
            node.meta
                .as_mut()
                .expect("pattern shell application must leave metadata present"),
            shell,
        ),
    }
}

pub(super) fn apply_shell_to_type(ty: &mut Type, shell: &Meta) {
    match ty {
        Type::Path(node) => apply_shell_to_meta(&mut node.meta, shell),
    }
}

pub(super) fn apply_shell_to_match_arm(arm: &mut MatchArm, shell: &Meta) {
    apply_shell_to_meta(&mut arm.meta, shell);
}

pub(super) fn clear_patch_node_outer_placement(node: &mut PatchNode) {
    match node {
        PatchNode::Item(item) => clear_meta_placement(item.meta_mut()),
        PatchNode::Field(field) => clear_meta_placement(&mut field.meta),
        PatchNode::Variant(variant) => clear_meta_placement(&mut variant.meta),
        PatchNode::Param(param) => clear_meta_placement(&mut param.meta),
        PatchNode::Stmt(stmt) => clear_stmt_outer_placement(stmt),
        PatchNode::MatchArm(arm) => clear_meta_placement(&mut arm.meta),
        PatchNode::Expr(expr) => {
            if let Some(meta) = expr.meta_mut() {
                clear_meta_placement(meta);
            }
        }
        PatchNode::Type(ty) => match ty {
            Type::Path(node) => clear_meta_placement(&mut node.meta),
        },
        PatchNode::Pattern(pattern) => clear_pattern_outer_placement(pattern),
        PatchNode::Doc(node) => clear_meta_placement(&mut node.meta),
        PatchNode::Comment(node) => clear_meta_placement(&mut node.meta),
    }
}

fn apply_shell_to_meta(meta: &mut Meta, shell: &Meta) {
    meta.id = shell.id.clone();
    meta.rank = shell.rank.clone();
    meta.anchor = shell.anchor.clone();
    meta.slot = shell.slot.clone();
    meta.span = None;
}

fn clear_stmt_outer_placement(stmt: &mut Stmt) {
    match stmt {
        Stmt::Let(node) => clear_meta_placement(&mut node.meta),
        Stmt::Expr(node) => clear_meta_placement(&mut node.meta),
        Stmt::Item(item) => clear_meta_placement(item.meta_mut()),
        Stmt::Doc(node) => clear_meta_placement(&mut node.meta),
        Stmt::Comment(node) => clear_meta_placement(&mut node.meta),
    }
}

fn clear_pattern_outer_placement(pattern: &mut Pattern) {
    match pattern {
        Pattern::Ident(node) => {
            if let Some(meta) = &mut node.meta {
                clear_meta_placement(meta);
            }
        }
        Pattern::Wild(node) => {
            if let Some(meta) = &mut node.meta {
                clear_meta_placement(meta);
            }
        }
    }
}

fn clear_meta_placement(meta: &mut Meta) {
    meta.rank = None;
    meta.anchor = None;
    meta.slot = None;
    meta.span = None;
}

fn ensure_expr_meta(expr: &mut Expr, shell: &Meta) {
    match expr {
        Expr::Path(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Lit(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Group(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Binary(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Unary(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Call(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Match(node) => ensure_optional_meta(&mut node.meta, shell),
        Expr::Block(node) => ensure_optional_meta(&mut node.meta, shell),
    }
}

fn ensure_pattern_meta(pattern: &mut Pattern, shell: &Meta) {
    match pattern {
        Pattern::Ident(node) => ensure_optional_meta(&mut node.meta, shell),
        Pattern::Wild(node) => ensure_optional_meta(&mut node.meta, shell),
    }
}

fn ensure_optional_meta(slot: &mut Option<Meta>, shell: &Meta) {
    if slot.is_none() {
        *slot = Some(Meta {
            id: shell.id.clone(),
            rank: None,
            anchor: None,
            slot: None,
            span: None,
        });
    }
}

pub(super) fn expect_item(node: Option<PatchNode>, slot: &str) -> Result<Item, PatchError> {
    match node {
        Some(PatchNode::Item(item)) => Ok(item),
        Some(other) => Err(slot_kind_error(slot, "item", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_field(node: Option<PatchNode>, slot: &str) -> Result<Field, PatchError> {
    match node {
        Some(PatchNode::Field(field)) => Ok(field),
        Some(other) => Err(slot_kind_error(slot, "field", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_variant(node: Option<PatchNode>, slot: &str) -> Result<Variant, PatchError> {
    match node {
        Some(PatchNode::Variant(variant)) => Ok(variant),
        Some(other) => Err(slot_kind_error(slot, "variant", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_param(node: Option<PatchNode>, slot: &str) -> Result<Param, PatchError> {
    match node {
        Some(PatchNode::Param(param)) => Ok(param),
        Some(other) => Err(slot_kind_error(slot, "parameter", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_stmt(node: Option<PatchNode>, slot: &str) -> Result<Stmt, PatchError> {
    match node {
        Some(PatchNode::Stmt(stmt)) => Ok(stmt),
        Some(other) => Err(slot_kind_error(slot, "statement", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_match_arm(
    node: Option<PatchNode>,
    slot: &str,
) -> Result<MatchArm, PatchError> {
    match node {
        Some(PatchNode::MatchArm(arm)) => Ok(arm),
        Some(other) => Err(slot_kind_error(slot, "match arm", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_expr(node: Option<PatchNode>, slot: &str) -> Result<Expr, PatchError> {
    match node {
        Some(PatchNode::Expr(expr)) => Ok(expr),
        Some(other) => Err(slot_kind_error(slot, "expression", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_type(node: Option<PatchNode>, slot: &str) -> Result<Type, PatchError> {
    match node {
        Some(PatchNode::Type(ty)) => Ok(ty),
        Some(other) => Err(slot_kind_error(slot, "type", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

pub(super) fn expect_pattern(node: Option<PatchNode>, slot: &str) -> Result<Pattern, PatchError> {
    match node {
        Some(PatchNode::Pattern(pattern)) => Ok(pattern),
        Some(other) => Err(slot_kind_error(slot, "pattern", Some(&other))),
        None => Err(patch_error("patch node was consumed before use")),
    }
}

fn slot_kind_error(slot: &str, expected: &str, found: Option<&PatchNode>) -> PatchError {
    let found = found.map(patch_node_kind).unwrap_or("unknown fragment");
    patch_error(&format!(
        "slot `{slot}` accepts {expected} nodes only, found {found}"
    ))
}

pub(super) fn is_item_trivia(item: &Item) -> bool {
    matches!(item, Item::Doc(_) | Item::Comment(_))
}

pub(super) fn is_stmt_trivia(stmt: &Stmt) -> bool {
    matches!(stmt, Stmt::Doc(_) | Stmt::Comment(_))
}

pub(super) fn resolved_item_attachment_targets(items: &[Item]) -> Vec<Option<String>> {
    let mut next_semantic = None;
    let mut targets = vec![None; items.len()];
    for index in (0..items.len()).rev() {
        match &items[index] {
            Item::Doc(node) => {
                targets[index] = node.meta.anchor.clone().or_else(|| next_semantic.clone());
            }
            Item::Comment(node) => {
                targets[index] = node.meta.anchor.clone().or_else(|| next_semantic.clone());
            }
            item => {
                next_semantic = Some(item.meta().id.clone());
            }
        }
    }
    targets
}

pub(super) fn resolved_stmt_attachment_targets(stmts: &[Stmt]) -> Vec<Option<String>> {
    let mut next_semantic = None;
    let mut targets = vec![None; stmts.len()];
    for index in (0..stmts.len()).rev() {
        match &stmts[index] {
            Stmt::Doc(node) => {
                targets[index] = node.meta.anchor.clone().or_else(|| next_semantic.clone());
            }
            Stmt::Comment(node) => {
                targets[index] = node.meta.anchor.clone().or_else(|| next_semantic.clone());
            }
            stmt => {
                next_semantic = stmt_id(stmt).map(str::to_owned);
            }
        }
    }
    targets
}

pub(super) fn semantic_item_target_ids(items: &[Item]) -> Vec<&str> {
    items
        .iter()
        .filter(|item| !is_item_trivia(item))
        .map(|item| item.meta().id.as_str())
        .collect()
}

pub(super) fn semantic_stmt_target_ids(stmts: &[Stmt]) -> Vec<&str> {
    stmts
        .iter()
        .filter(|stmt| !is_stmt_trivia(stmt))
        .filter_map(stmt_id)
        .collect()
}