draxl-rust 0.1.1

Rust language adapter for Draxl
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use draxl_ast::{Expr, File, Item, Pattern, Stmt};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeKind {
    File,
    Mod,
    Use,
    Struct,
    Enum,
    Fn,
    Field,
    Variant,
    Param,
    LetStmt,
    ExprStmt,
    MatchArm,
    PatternIdent,
    PatternWild,
    Type,
    ExprPath,
    ExprLit,
    ExprGroup,
    ExprBinary,
    ExprUnary,
    ExprCall,
    ExprMatch,
    ExprBlock,
    Doc,
    Comment,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FragmentKind {
    Item,
    Field,
    Variant,
    Param,
    Stmt,
    MatchArm,
    Expr,
    Type,
    Pattern,
    Doc,
    Comment,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueKind {
    Ident,
    Str,
    Bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotArity {
    Ranked,
    Single,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttachmentContainerKind {
    Items,
    Stmts,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlotSpec {
    pub public_name: &'static str,
    pub meta_slot_name: &'static str,
    pub fragment_kind: FragmentKind,
    pub arity: SlotArity,
    pub occupant_removable: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PathSpec {
    pub public_name: &'static str,
    pub value_kind: ValueKind,
    pub clearable: bool,
}

pub fn slot_spec(owner: NodeKind, slot: &str) -> Option<SlotSpec> {
    match (owner, slot) {
        (NodeKind::File, "items") => Some(SlotSpec {
            public_name: "items",
            meta_slot_name: "file_items",
            fragment_kind: FragmentKind::Item,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Mod, "items") => Some(SlotSpec {
            public_name: "items",
            meta_slot_name: "items",
            fragment_kind: FragmentKind::Item,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Struct, "fields") => Some(SlotSpec {
            public_name: "fields",
            meta_slot_name: "fields",
            fragment_kind: FragmentKind::Field,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Enum, "variants") => Some(SlotSpec {
            public_name: "variants",
            meta_slot_name: "variants",
            fragment_kind: FragmentKind::Variant,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Fn, "params") => Some(SlotSpec {
            public_name: "params",
            meta_slot_name: "params",
            fragment_kind: FragmentKind::Param,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Fn, "body") | (NodeKind::ExprBlock, "body") => Some(SlotSpec {
            public_name: "body",
            meta_slot_name: "body",
            fragment_kind: FragmentKind::Stmt,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::ExprMatch, "arms") => Some(SlotSpec {
            public_name: "arms",
            meta_slot_name: "arms",
            fragment_kind: FragmentKind::MatchArm,
            arity: SlotArity::Ranked,
            occupant_removable: true,
        }),
        (NodeKind::Fn, "ret") => Some(SlotSpec {
            public_name: "ret",
            meta_slot_name: "ret",
            fragment_kind: FragmentKind::Type,
            arity: SlotArity::Single,
            occupant_removable: true,
        }),
        (NodeKind::Field, "ty") | (NodeKind::Param, "ty") => Some(SlotSpec {
            public_name: "ty",
            meta_slot_name: "ty",
            fragment_kind: FragmentKind::Type,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::LetStmt, "pat") => Some(SlotSpec {
            public_name: "pat",
            meta_slot_name: "pat",
            fragment_kind: FragmentKind::Pattern,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::LetStmt, "init") => Some(SlotSpec {
            public_name: "init",
            meta_slot_name: "init",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprStmt, "expr") => Some(SlotSpec {
            public_name: "expr",
            meta_slot_name: "expr",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprGroup, "expr") | (NodeKind::ExprUnary, "expr") => Some(SlotSpec {
            public_name: "expr",
            meta_slot_name: "expr",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprBinary, "lhs") => Some(SlotSpec {
            public_name: "lhs",
            meta_slot_name: "lhs",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprBinary, "rhs") => Some(SlotSpec {
            public_name: "rhs",
            meta_slot_name: "rhs",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprCall, "callee") => Some(SlotSpec {
            public_name: "callee",
            meta_slot_name: "callee",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::ExprMatch, "scrutinee") => Some(SlotSpec {
            public_name: "scrutinee",
            meta_slot_name: "scrutinee",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::MatchArm, "pat") => Some(SlotSpec {
            public_name: "pat",
            meta_slot_name: "pat",
            fragment_kind: FragmentKind::Pattern,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        (NodeKind::MatchArm, "guard") => Some(SlotSpec {
            public_name: "guard",
            meta_slot_name: "guard",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: true,
        }),
        (NodeKind::MatchArm, "body") => Some(SlotSpec {
            public_name: "body",
            meta_slot_name: "body",
            fragment_kind: FragmentKind::Expr,
            arity: SlotArity::Single,
            occupant_removable: false,
        }),
        _ => None,
    }
}

pub fn ranked_slot_spec(owner: NodeKind, slot: &str) -> Option<SlotSpec> {
    slot_spec(owner, slot).filter(|spec| spec.arity == SlotArity::Ranked)
}

pub fn single_slot_spec(owner: NodeKind, slot: &str) -> Option<SlotSpec> {
    slot_spec(owner, slot).filter(|spec| spec.arity == SlotArity::Single)
}

pub fn removable_slot_spec(owner: NodeKind, slot: &str) -> Option<SlotSpec> {
    slot_spec(owner, slot).filter(|spec| spec.occupant_removable)
}

pub fn path_spec(kind: NodeKind, path: &str) -> Option<PathSpec> {
    match (kind, path) {
        (NodeKind::Mod, "name")
        | (NodeKind::Struct, "name")
        | (NodeKind::Enum, "name")
        | (NodeKind::Fn, "name")
        | (NodeKind::Field, "name")
        | (NodeKind::Variant, "name")
        | (NodeKind::Param, "name")
        | (NodeKind::PatternIdent, "name") => Some(PathSpec {
            public_name: "name",
            value_kind: ValueKind::Ident,
            clearable: false,
        }),
        (NodeKind::Doc, "text") | (NodeKind::Comment, "text") => Some(PathSpec {
            public_name: "text",
            value_kind: ValueKind::Str,
            clearable: true,
        }),
        (NodeKind::ExprBinary, "op") => Some(PathSpec {
            public_name: "op",
            value_kind: ValueKind::Ident,
            clearable: false,
        }),
        (NodeKind::ExprUnary, "op") => Some(PathSpec {
            public_name: "op",
            value_kind: ValueKind::Ident,
            clearable: true,
        }),
        (NodeKind::ExprStmt, "semi") => Some(PathSpec {
            public_name: "semi",
            value_kind: ValueKind::Bool,
            clearable: true,
        }),
        _ => None,
    }
}

pub fn clearable_path_spec(kind: NodeKind, path: &str) -> Option<PathSpec> {
    path_spec(kind, path).filter(|spec| spec.clearable)
}

pub fn replace_fragment_kind(kind: NodeKind) -> FragmentKind {
    match kind {
        NodeKind::Mod | NodeKind::Use | NodeKind::Struct | NodeKind::Enum | NodeKind::Fn => {
            FragmentKind::Item
        }
        NodeKind::Field => FragmentKind::Field,
        NodeKind::Variant => FragmentKind::Variant,
        NodeKind::Param => FragmentKind::Param,
        NodeKind::LetStmt | NodeKind::ExprStmt => FragmentKind::Stmt,
        NodeKind::MatchArm => FragmentKind::MatchArm,
        NodeKind::PatternIdent | NodeKind::PatternWild => FragmentKind::Pattern,
        NodeKind::Type => FragmentKind::Type,
        NodeKind::ExprPath
        | NodeKind::ExprLit
        | NodeKind::ExprGroup
        | NodeKind::ExprBinary
        | NodeKind::ExprUnary
        | NodeKind::ExprCall
        | NodeKind::ExprMatch
        | NodeKind::ExprBlock => FragmentKind::Expr,
        NodeKind::Doc => FragmentKind::Doc,
        NodeKind::Comment => FragmentKind::Comment,
        NodeKind::File => FragmentKind::Item,
    }
}

pub fn item_kind(item: &Item) -> NodeKind {
    match item {
        Item::Mod(_) => NodeKind::Mod,
        Item::Use(_) => NodeKind::Use,
        Item::Struct(_) => NodeKind::Struct,
        Item::Enum(_) => NodeKind::Enum,
        Item::Fn(_) => NodeKind::Fn,
        Item::Doc(_) => NodeKind::Doc,
        Item::Comment(_) => NodeKind::Comment,
    }
}

pub fn stmt_kind(stmt: &Stmt) -> NodeKind {
    match stmt {
        Stmt::Let(_) => NodeKind::LetStmt,
        Stmt::Expr(_) => NodeKind::ExprStmt,
        Stmt::Item(item) => item_kind(item),
        Stmt::Doc(_) => NodeKind::Doc,
        Stmt::Comment(_) => NodeKind::Comment,
    }
}

pub fn expr_kind(expr: &Expr) -> NodeKind {
    match expr {
        Expr::Path(_) => NodeKind::ExprPath,
        Expr::Lit(_) => NodeKind::ExprLit,
        Expr::Group(_) => NodeKind::ExprGroup,
        Expr::Binary(_) => NodeKind::ExprBinary,
        Expr::Unary(_) => NodeKind::ExprUnary,
        Expr::Call(_) => NodeKind::ExprCall,
        Expr::Match(_) => NodeKind::ExprMatch,
        Expr::Block(_) => NodeKind::ExprBlock,
    }
}

pub fn pattern_kind(pattern: &Pattern) -> NodeKind {
    match pattern {
        Pattern::Ident(_) => NodeKind::PatternIdent,
        Pattern::Wild(_) => NodeKind::PatternWild,
    }
}

pub fn node_kind_label(kind: NodeKind) -> &'static str {
    match kind {
        NodeKind::File => "file",
        NodeKind::Mod => "module",
        NodeKind::Use => "use item",
        NodeKind::Struct => "struct",
        NodeKind::Enum => "enum",
        NodeKind::Fn => "function",
        NodeKind::Field => "field",
        NodeKind::Variant => "variant",
        NodeKind::Param => "parameter",
        NodeKind::LetStmt => "let statement",
        NodeKind::ExprStmt => "expression statement",
        NodeKind::MatchArm => "match arm",
        NodeKind::PatternIdent => "identifier pattern",
        NodeKind::PatternWild => "wildcard pattern",
        NodeKind::Type => "type",
        NodeKind::ExprPath => "path expression",
        NodeKind::ExprLit => "literal expression",
        NodeKind::ExprGroup => "grouped expression",
        NodeKind::ExprBinary => "binary expression",
        NodeKind::ExprUnary => "unary expression",
        NodeKind::ExprCall => "call expression",
        NodeKind::ExprMatch => "match expression",
        NodeKind::ExprBlock => "block expression",
        NodeKind::Doc => "doc comment",
        NodeKind::Comment => "line comment",
    }
}

pub fn value_kind_label(value_kind: ValueKind) -> &'static str {
    match value_kind {
        ValueKind::Ident => "an identifier value",
        ValueKind::Str => "a string value",
        ValueKind::Bool => "a boolean value",
    }
}

pub fn attachment_container_kind_for_owner(kind: NodeKind) -> Option<AttachmentContainerKind> {
    match kind {
        NodeKind::File | NodeKind::Mod => Some(AttachmentContainerKind::Items),
        NodeKind::Fn | NodeKind::ExprBlock => Some(AttachmentContainerKind::Stmts),
        _ => None,
    }
}

pub fn attachment_closure_allowed(
    owner_kind: NodeKind,
    slot: &str,
    closure_kind: AttachmentContainerKind,
) -> bool {
    let Some(spec) = ranked_slot_spec(owner_kind, slot) else {
        return false;
    };
    matches!(
        (
            closure_kind,
            attachment_container_kind_for_owner(owner_kind),
            spec.fragment_kind
        ),
        (
            AttachmentContainerKind::Items,
            Some(AttachmentContainerKind::Items),
            FragmentKind::Item
        ) | (
            AttachmentContainerKind::Stmts,
            Some(AttachmentContainerKind::Stmts),
            FragmentKind::Stmt
        )
    )
}

pub fn is_attachable_kind(kind: NodeKind) -> bool {
    matches!(kind, NodeKind::Doc | NodeKind::Comment)
}

pub fn invalid_ranked_slot_message(owner_label: &str, slot: &str) -> String {
    format!("slot `{owner_label}.{slot}` is not available for ranked insertion")
}

pub fn invalid_single_slot_message(owner_label: &str, slot: &str) -> String {
    format!("slot `{owner_label}.{slot}` is not available for `put`")
}

pub fn invalid_set_path_message(node_id: &str, path: &str, kind: NodeKind) -> String {
    format!(
        "path `@{node_id}.{path}` is not settable on {}",
        node_kind_label(kind)
    )
}

pub fn invalid_clear_path_message(node_id: &str, path: &str, kind: NodeKind) -> String {
    format!(
        "path `@{node_id}.{path}` is not clearable on {}",
        node_kind_label(kind)
    )
}

pub fn required_slot_error_message(action: &str, target_id: &str, slot: &str) -> String {
    format!(
        "{} target `{}` cannot be removed from required slot `{}`",
        action, target_id, slot
    )
}

pub fn unsupported_slot_error_message(action: &str, target_id: &str, slot: &str) -> String {
    format!(
        "{} target `{}` is in unsupported slot `{}`",
        action, target_id, slot
    )
}

pub fn trivia_move_target_message() -> &'static str {
    "move does not support doc or comment targets; use attach, detach, replace, or delete"
}

pub fn single_slot_attachment_closure_message() -> &'static str {
    "cannot move a node with attached docs/comments into a single-child slot"
}

pub fn invalid_attachment_closure_destination_message(
    closure_kind: AttachmentContainerKind,
) -> &'static str {
    match closure_kind {
        AttachmentContainerKind::Items => {
            "cannot move item attachments into a non-item ranked slot"
        }
        AttachmentContainerKind::Stmts => {
            "cannot move statement attachments into a non-body ranked slot"
        }
    }
}

pub fn invalid_attachment_container_owner_message(
    owner_label: &str,
    closure_kind: AttachmentContainerKind,
) -> String {
    match closure_kind {
        AttachmentContainerKind::Items => {
            format!("owner `{owner_label}` does not expose an item attachment container")
        }
        AttachmentContainerKind::Stmts => {
            format!("owner `{owner_label}` does not expose a statement body slot")
        }
    }
}

pub fn attach_target_not_sibling_message(target_id: &str, node_id: &str) -> String {
    format!("attach target `{target_id}` is not a sibling semantic node for `{node_id}`")
}

pub fn detach_requires_following_sibling_message(node_id: &str) -> String {
    format!("detach source `{node_id}` needs a following sibling semantic node")
}

pub fn find_node_kind(file: &File, node_id: &str) -> Option<NodeKind> {
    for item in &file.items {
        if let Some(kind) = find_in_item(item, node_id) {
            return Some(kind);
        }
    }
    None
}

fn find_in_item(item: &Item, node_id: &str) -> Option<NodeKind> {
    if item.meta().id == node_id {
        return Some(item_kind(item));
    }

    match item {
        Item::Mod(module) => {
            for child in &module.items {
                if let Some(kind) = find_in_item(child, node_id) {
                    return Some(kind);
                }
            }
            None
        }
        Item::Struct(strukt) => {
            for field in &strukt.fields {
                if field.meta.id == node_id {
                    return Some(NodeKind::Field);
                }
                if field.ty.meta().id == node_id {
                    return Some(NodeKind::Type);
                }
            }
            None
        }
        Item::Enum(enm) => {
            for variant in &enm.variants {
                if variant.meta.id == node_id {
                    return Some(NodeKind::Variant);
                }
            }
            None
        }
        Item::Fn(function) => {
            for param in &function.params {
                if param.meta.id == node_id {
                    return Some(NodeKind::Param);
                }
                if param.ty.meta().id == node_id {
                    return Some(NodeKind::Type);
                }
            }
            if function
                .ret_ty
                .as_ref()
                .is_some_and(|ret_ty| ret_ty.meta().id == node_id)
            {
                return Some(NodeKind::Type);
            }
            find_in_block(&function.body, node_id)
        }
        Item::Use(_) | Item::Doc(_) | Item::Comment(_) => None,
    }
}

fn find_in_block(block: &draxl_ast::Block, node_id: &str) -> Option<NodeKind> {
    if block.meta.as_ref().is_some_and(|meta| meta.id == node_id) {
        return Some(NodeKind::ExprBlock);
    }

    for stmt in &block.stmts {
        if let Some(kind) = find_in_stmt(stmt, node_id) {
            return Some(kind);
        }
    }
    None
}

fn find_in_stmt(stmt: &Stmt, node_id: &str) -> Option<NodeKind> {
    match stmt {
        Stmt::Let(node) => {
            if node.meta.id == node_id {
                return Some(NodeKind::LetStmt);
            }
            find_in_pattern(&node.pat, node_id).or_else(|| find_in_expr(&node.value, node_id))
        }
        Stmt::Expr(node) => {
            if node.meta.id == node_id {
                return Some(NodeKind::ExprStmt);
            }
            find_in_expr(&node.expr, node_id)
        }
        Stmt::Item(item) => find_in_item(item, node_id),
        Stmt::Doc(node) => (node.meta.id == node_id).then_some(NodeKind::Doc),
        Stmt::Comment(node) => (node.meta.id == node_id).then_some(NodeKind::Comment),
    }
}

fn find_in_expr(expr: &Expr, node_id: &str) -> Option<NodeKind> {
    if expr.meta().is_some_and(|meta| meta.id == node_id) {
        return Some(expr_kind(expr));
    }

    match expr {
        Expr::Group(group) => find_in_expr(&group.expr, node_id),
        Expr::Binary(binary) => {
            find_in_expr(&binary.lhs, node_id).or_else(|| find_in_expr(&binary.rhs, node_id))
        }
        Expr::Unary(unary) => find_in_expr(&unary.expr, node_id),
        Expr::Call(call) => {
            if let Some(kind) = find_in_expr(&call.callee, node_id) {
                return Some(kind);
            }
            for arg in &call.args {
                if let Some(kind) = find_in_expr(arg, node_id) {
                    return Some(kind);
                }
            }
            None
        }
        Expr::Match(match_expr) => {
            if let Some(kind) = find_in_expr(&match_expr.scrutinee, node_id) {
                return Some(kind);
            }
            for arm in &match_expr.arms {
                if arm.meta.id == node_id {
                    return Some(NodeKind::MatchArm);
                }
                if let Some(kind) = find_in_pattern(&arm.pat, node_id) {
                    return Some(kind);
                }
                if let Some(guard) = &arm.guard {
                    if let Some(kind) = find_in_expr(guard, node_id) {
                        return Some(kind);
                    }
                }
                if let Some(kind) = find_in_expr(&arm.body, node_id) {
                    return Some(kind);
                }
            }
            None
        }
        Expr::Block(block) => find_in_block(block, node_id),
        Expr::Path(_) | Expr::Lit(_) => None,
    }
}

fn find_in_pattern(pattern: &Pattern, node_id: &str) -> Option<NodeKind> {
    if pattern.meta().is_some_and(|meta| meta.id == node_id) {
        return Some(pattern_kind(pattern));
    }
    None
}