clash_starlark 0.7.2

Starlark policy evaluator for Clash — compiles .star files to JSON policy
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
//! Pretty-printer for Starlark AST nodes.
//!
//! Converts a `Vec<Stmt>` into formatted Starlark source text.

use std::fmt::Write;

use super::ast::{DictEntry, Expr, Param, Stmt};

const INDENT: &str = "    ";
const MAX_LINE: usize = 88;

/// Format a single expression as a Starlark source string.
pub fn expr_to_string(expr: &Expr) -> String {
    format_expr(expr, 0)
}

/// Serialize a list of statements into formatted Starlark source.
pub fn serialize(stmts: &[Stmt]) -> String {
    let mut out = String::new();
    for stmt in stmts {
        write_stmt(&mut out, stmt, 0);
    }
    out
}

fn write_stmt(out: &mut String, stmt: &Stmt, depth: usize) {
    let prefix = INDENT.repeat(depth);
    match stmt {
        Stmt::Load { module, names } => {
            write_load(out, module, names, &prefix);
        }
        Stmt::Assign { target, value } => {
            let rhs = format_expr(value, depth);
            out.push_str(&prefix);
            out.push_str(target);
            out.push_str(" = ");
            out.push_str(&rhs);
            out.push('\n');
        }
        Stmt::FuncDef { name, params, body } => {
            out.push_str(&prefix);
            out.push_str("def ");
            out.push_str(name);
            out.push('(');
            for (i, p) in params.iter().enumerate() {
                if i > 0 {
                    out.push_str(", ");
                }
                write_param(out, p);
            }
            out.push_str("):\n");
            for s in body {
                write_stmt(out, s, depth + 1);
            }
        }
        Stmt::Return(expr) => {
            let val = format_expr(expr, depth);
            out.push_str(&prefix);
            out.push_str("return ");
            out.push_str(&val);
            out.push('\n');
        }
        Stmt::Expr(expr) => {
            out.push_str(&prefix);
            out.push_str(&format_expr(expr, depth));
            out.push('\n');
        }
        Stmt::Comment(text) => {
            out.push_str(&prefix);
            out.push_str("# ");
            out.push_str(text);
            out.push('\n');
        }
        Stmt::Blank => {
            out.push('\n');
        }
    }
}

fn write_load(out: &mut String, module: &str, names: &[String], prefix: &str) {
    // Try single line first
    let name_list: String = names
        .iter()
        .map(|n| format!("\"{n}\""))
        .collect::<Vec<_>>()
        .join(", ");
    let oneline = format!("{prefix}load(\"{module}\", {name_list})\n");
    if oneline.len() <= MAX_LINE {
        out.push_str(&oneline);
        return;
    }
    // Multi-line
    out.push_str(prefix);
    let _ = writeln!(out, "load(\"{module}\",");
    let inner = format!("{prefix}{INDENT}");
    for name in names {
        out.push_str(&inner);
        let _ = writeln!(out, "\"{name}\",");
    }
    out.push_str(prefix);
    out.push_str(")\n");
}

fn write_param(out: &mut String, param: &Param) {
    out.push_str(&param.name);
    if let Some(default) = &param.default {
        out.push_str(" = ");
        out.push_str(&format_expr(default, 0));
    }
}

/// Format an expression as a string. `depth` is the current indentation level
/// used to decide whether to break into multiple lines.
fn format_expr(expr: &Expr, depth: usize) -> String {
    match expr {
        Expr::String(s) => format!("\"{}\"", escape_str(s)),
        Expr::Bool(true) => "True".to_string(),
        Expr::Bool(false) => "False".to_string(),
        Expr::Int(n) => n.to_string(),
        Expr::None => "None".to_string(),
        Expr::Ident(name) => name.clone(),
        Expr::Raw(s) => s.clone(),
        Expr::Commented { expr, .. } => {
            // The comment is handled by the list/call formatter. In raw
            // expression context we just emit the inner expression.
            format_expr(expr, depth)
        }
        Expr::List(items) => format_sequence(items, "[", "]", depth),
        Expr::Tuple(items) => {
            if items.len() == 1 {
                format!("({},)", format_expr(&items[0], depth))
            } else {
                format_sequence(items, "(", ")", depth)
            }
        }
        Expr::Dict(entries) => format_dict(entries, depth),
        Expr::Call { func, args, kwargs } => format_call(func, args, kwargs, depth),
        Expr::Attr { value, attr } => {
            format!("{}.{}", format_expr(value, depth), attr)
        }
    }
}

fn escape_str(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

/// Format a bracketed sequence: `[a, b, c]` or multi-line.
fn format_sequence(items: &[Expr], open: &str, close: &str, depth: usize) -> String {
    if items.is_empty() {
        return format!("{open}{close}");
    }
    // Check if any items have comments — if so, force multi-line
    let has_comments = items.iter().any(|e| matches!(e, Expr::Commented { .. }));
    let inlined: Vec<String> = items.iter().map(|e| format_expr(e, depth + 1)).collect();
    if !has_comments {
        let oneline = format!("{open}{}{close}", inlined.join(", "));
        let approx_col = depth * INDENT.len() + oneline.len();
        if approx_col <= MAX_LINE && !oneline.contains('\n') {
            return oneline;
        }
    }
    // Multi-line
    let inner = INDENT.repeat(depth + 1);
    let outer = INDENT.repeat(depth);
    let mut out = format!("{open}\n");
    for (item, expr) in inlined.iter().zip(items.iter()) {
        // Emit comment line before the item if it's a Commented expression
        if let Expr::Commented { comment, .. } = expr {
            let _ = writeln!(out, "{inner}# {comment}");
        }
        write_indented_item(&mut out, item, &inner);
        out.push_str(",\n");
    }
    let _ = write!(out, "{outer}{close}");
    out
}

/// Format a dict literal, always multi-line for readability when nested.
fn format_dict(entries: &[DictEntry], depth: usize) -> String {
    if entries.is_empty() {
        return "{}".to_string();
    }
    let inner_depth = depth + 1;
    let inner = INDENT.repeat(inner_depth);
    let outer = INDENT.repeat(depth);

    // Try single-line first (unless forced multi-line)
    let parts: Vec<String> = entries
        .iter()
        .map(|e| {
            format!(
                "{}: {}",
                format_expr(&e.key, inner_depth),
                format_expr(&e.value, inner_depth)
            )
        })
        .collect();
    let oneline = format!("{{{}}}", parts.join(", "));
    let approx_col = depth * INDENT.len() + oneline.len();
    if approx_col <= MAX_LINE && !oneline.contains('\n') {
        return oneline;
    }

    // Multi-line
    let mut out = "{\n".to_string();
    for e in entries {
        let k = format_expr(&e.key, inner_depth);
        let v = format_expr(&e.value, inner_depth);
        let _ = writeln!(out, "{inner}{k}: {v},");
    }
    let _ = write!(out, "{outer}}}");
    out
}

/// Format a function/method call, with single-line or multi-line layout.
fn format_call(func: &Expr, args: &[Expr], kwargs: &[(String, Expr)], depth: usize) -> String {
    let func_str = format_expr(func, depth);
    let all_args = format_arg_list(args, kwargs, depth);

    // Try single line (comments force multi-line)
    let has_commented_args = args.iter().any(|e| matches!(e, Expr::Commented { .. }));
    let oneline = format!("{func_str}({all_args})");
    let approx_col = depth * INDENT.len() + oneline.len();
    if !has_commented_args && approx_col <= MAX_LINE && !oneline.contains('\n') {
        return oneline;
    }

    // Multi-line: each arg on its own line
    let inner = INDENT.repeat(depth + 1);
    let outer = INDENT.repeat(depth);
    let mut out = format!("{func_str}(\n");
    for a in args {
        // Emit comment line before the item if it's a Commented expression
        if has_commented_args {
            if let Expr::Commented { comment, .. } = a {
                let _ = writeln!(out, "{inner}# {comment}");
            }
        }
        write_indented_item(&mut out, &format_expr(a, depth + 1), &inner);
        out.push_str(",\n");
    }
    for (k, v) in kwargs {
        let formatted = format_expr(v, depth + 1);
        if formatted.contains('\n') {
            let _ = write!(out, "{inner}{k} = ");
            // First line of value is already on the same line as `key = `
            // Remaining lines are self-indented by format_expr
            out.push_str(&formatted);
            out.push_str(",\n");
        } else {
            let _ = writeln!(out, "{inner}{k} = {formatted},");
        }
    }
    let _ = write!(out, "{outer})");
    out
}

/// Write an expression as an indented list/call item. Multi-line expressions
/// already carry their own indentation for continuation lines.
fn write_indented_item(out: &mut String, formatted: &str, indent: &str) {
    if formatted.contains('\n') {
        // The expression's first line gets our indent; continuation lines
        // are already absolutely indented by format_expr.
        let mut first = true;
        for line in formatted.lines() {
            if first {
                let _ = write!(out, "{indent}{line}");
                first = false;
            } else {
                let _ = write!(out, "\n{line}");
            }
        }
    } else {
        let _ = write!(out, "{indent}{formatted}");
    }
}

fn format_arg_list(args: &[Expr], kwargs: &[(String, Expr)], depth: usize) -> String {
    let mut parts: Vec<String> = args.iter().map(|a| format_expr(a, depth)).collect();
    for (k, v) in kwargs {
        parts.push(format!("{k} = {}", format_expr(v, depth)));
    }
    parts.join(", ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codegen::ast::*;
    use indoc::indoc;
    use pretty_assertions::assert_eq;

    #[test]
    fn load_single_line() {
        let stmts = vec![Stmt::load("@clash//std.star", &["when", "tool", "policy"])];
        let src = serialize(&stmts);
        assert_eq!(
            src,
            "load(\"@clash//std.star\", \"when\", \"tool\", \"policy\")\n"
        );
    }

    #[test]
    fn load_wraps_when_long() {
        let names: Vec<&str> = vec![
            "when", "tool", "policy", "sandbox", "cwd", "home", "tempdir", "path", "regex",
            "domains", "domain", "allow", "deny", "ask",
        ];
        let stmts = vec![Stmt::load("@clash//std.star", &names)];
        let src = serialize(&stmts);
        assert!(src.starts_with("load(\"@clash//std.star\",\n"));
        assert!(src.contains("    \"when\",\n"));
    }

    #[test]
    fn simple_function() {
        let stmts = vec![Stmt::def(
            "main",
            vec![Stmt::Return(Expr::call("policy", vec![]))],
        )];
        let src = serialize(&stmts);
        assert_eq!(src, "def main():\n    return policy()\n");
    }

    #[test]
    fn call_with_kwargs() {
        let expr = Expr::call_kwargs("allow", vec![], vec![("sandbox", Expr::ident("dev"))]);
        let s = format_expr(&expr, 0);
        assert_eq!(s, "allow(sandbox = dev)");
    }

    #[test]
    fn method_chain() {
        let expr = Expr::call("tool", vec![Expr::string("Read")])
            .sandbox(Expr::ident("_fs_box"))
            .allow();
        let s = format_expr(&expr, 0);
        assert_eq!(s, "tool(\"Read\").sandbox(_fs_box).allow()");
    }

    #[test]
    fn dict_nested() {
        let inner = Expr::dict(vec![DictEntry::new(
            Expr::string("push"),
            Expr::call("deny", vec![]),
        )]);
        let outer = Expr::dict(vec![DictEntry::new(Expr::string("git"), inner)]);
        let s = format_expr(&outer, 0);
        assert_eq!(s, r#"{"git": {"push": deny()}}"#);
    }

    #[test]
    fn assign_short_stays_single_line() {
        let stmts = vec![Stmt::assign(
            "_fs_box",
            Expr::call_kwargs(
                "sandbox",
                vec![],
                vec![
                    ("name", Expr::string("cwd")),
                    (
                        "fs",
                        Expr::list(vec![Expr::call("cwd", vec![]).recurse().allow()]),
                    ),
                ],
            ),
        )];
        let src = serialize(&stmts);
        assert_eq!(
            src,
            "_fs_box = sandbox(name = \"cwd\", fs = [cwd().recurse().allow()])\n"
        );
    }

    #[test]
    fn assign_long_goes_multiline() {
        let stmts = vec![Stmt::assign(
            "_fs_box",
            Expr::call_kwargs(
                "sandbox",
                vec![],
                vec![
                    ("name", Expr::string("cwd")),
                    (
                        "fs",
                        Expr::list(vec![
                            Expr::call_kwargs(
                                "cwd",
                                vec![],
                                vec![("follow_worktrees", Expr::bool(true))],
                            )
                            .recurse()
                            .allow_kwargs(vec![
                                ("read", Expr::bool(true)),
                                ("write", Expr::bool(true)),
                            ]),
                            Expr::call("home", vec![])
                                .child(".claude")
                                .recurse()
                                .allow_kwargs(vec![
                                    ("read", Expr::bool(true)),
                                    ("write", Expr::bool(true)),
                                ]),
                        ]),
                    ),
                ],
            ),
        )];
        let src = serialize(&stmts);
        assert!(
            src.contains("_fs_box = sandbox(\n"),
            "expected multi-line sandbox call"
        );
        assert!(src.contains("name = \"cwd\""), "expected name kwarg");
        assert!(
            src.contains("follow_worktrees = True"),
            "expected follow_worktrees"
        );
    }

    #[test]
    fn commented_items_in_list() {
        let stmts = vec![Stmt::Expr(Expr::list(vec![
            Expr::commented("first group", Expr::string("a")),
            Expr::commented("second group", Expr::string("b")),
        ]))];
        let src = serialize(&stmts);
        assert_eq!(
            src,
            "\
[
    # first group
    \"a\",
    # second group
    \"b\",
]
"
        );
    }

    #[test]
    fn load_multi_line_exact() {
        let names: Vec<&str> = vec![
            "when", "tool", "policy", "sandbox", "cwd", "home", "tempdir", "path", "regex",
            "domains", "domain", "allow", "deny", "ask",
        ];
        let stmts = vec![Stmt::load("@clash//std.star", &names)];
        let src = serialize(&stmts);
        assert_eq!(
            src,
            "\
load(\"@clash//std.star\",
    \"when\",
    \"tool\",
    \"policy\",
    \"sandbox\",
    \"cwd\",
    \"home\",
    \"tempdir\",
    \"path\",
    \"regex\",
    \"domains\",
    \"domain\",
    \"allow\",
    \"deny\",
    \"ask\",
)
"
        );
    }

    #[test]
    fn quick_policy_snapshot() {
        use crate::codegen::builder::*;

        let stmts = vec![
            Stmt::load("@clash//std.star", &["policy", "settings", "allow", "ask"]),
            Stmt::Blank,
            Stmt::Expr(settings(ask(), None)),
            Stmt::Blank,
            Stmt::Expr(policy(
                "test",
                ask(),
                vec![
                    crate::match_tree! {
                        "Bash" => {
                            ("git", "cargo") => allow(),
                        },
                    },
                    tool_match(&["Read"], allow()),
                    tool_match(&["Write"], allow()),
                ],
                None,
            )),
        ];
        let src = serialize(&stmts);
        assert_eq!(
            src,
            indoc! {r#"
                load("@clash//std.star", "policy", "settings", "allow", "ask")

                settings(default = ask())

                policy(
                    "test",
                    merge(
                        {tool("Bash"): {("git", "cargo"): allow()}},
                        {tool("Read"): allow()},
                        {tool("Write"): allow()},
                    ),
                    default = ask(),
                )
            "#}
        );
    }

    #[test]
    fn tuple_key_single() {
        let s = format_expr(&Expr::tuple(vec![Expr::string("a")]), 0);
        assert_eq!(s, "(\"a\",)");
    }

    #[test]
    fn escape_strings() {
        let s = format_expr(&Expr::string("hello \"world\"\nnewline"), 0);
        assert_eq!(s, "\"hello \\\"world\\\"\\nnewline\"");
    }

    #[test]
    fn raw_and_none() {
        assert_eq!(
            format_expr(&Expr::raw("arbitrary code"), 0),
            "arbitrary code"
        );
        assert_eq!(format_expr(&Expr::None, 0), "None");
        assert_eq!(format_expr(&Expr::Int(42), 0), "42");
    }

    #[test]
    fn bool_values() {
        assert_eq!(format_expr(&Expr::bool(true), 0), "True");
        assert_eq!(format_expr(&Expr::bool(false), 0), "False");
    }
}