glacier-web-cli 0.5.0

Command-line tool for creating and running Glacier applications.
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
//! Compiler diagnostics shown against `.glacier` source.
//!
//! rustc reports errors in the compiled `.glacier/…/x.rs`. For a diagnostic
//! touching such a file, this renders it afresh from cargo's JSON, in
//! rustc's layout, with the `.glacier` line in the snippet, carets at the
//! mapped columns, and suggestions applied to the `.glacier` line. Lines
//! match one to one by construction; columns go through the source map.
//! Diagnostics that don't touch a generated file keep rustc's rendering.

use std::fmt::Write as _;
use std::rc::Rc;

use glacier_dialect::SourceMap;
use serde_json::Value;

/// A `.glacier` file behind a generated `.rs` file.
pub struct Source {
    /// The `.glacier` path to show.
    pub path: String,
    pub lines: Vec<String>,
    pub map: SourceMap,
}

/// Finds the `.glacier` source behind a generated file name, if it is one.
pub type Lookup<'a> = dyn FnMut(&str) -> Option<Rc<Source>> + 'a;

struct Style {
    color: bool,
}

impl Style {
    fn paint(&self, code: &str, text: &str) -> String {
        if self.color {
            format!("\x1b[{code}m{text}\x1b[0m")
        } else {
            text.to_owned()
        }
    }

    fn level(&self, level: &str) -> String {
        let code = match level {
            "error" => "1;31",
            "warning" => "1;33",
            "help" => "1;36",
            _ => "1;32",
        };
        self.paint(code, level)
    }

    fn gutter(&self, text: &str) -> String {
        self.paint("1;34", text)
    }
}

/// A span, located in the `.glacier` source when it's in a generated file.
struct Located<'a> {
    span: &'a Value,
    /// Where to show it: the `.glacier` path, or the file rustc named.
    path: String,
    line: usize,
    /// 0-based, end exclusive, on `line` (the span's first line).
    start: usize,
    end: usize,
    last_line: usize,
    /// The lines to show, first to last.
    text: Vec<String>,
    /// Whether the location went through a source map.
    mapped: bool,
}

fn number(value: &Value) -> usize {
    value.as_u64().unwrap_or(1) as usize
}

fn locate<'a>(span: &'a Value, lookup: &mut Lookup) -> Located<'a> {
    let file = span["file_name"].as_str().unwrap_or_default();
    let line = number(&span["line_start"]);
    let last_line = number(&span["line_end"]).max(line);
    let (col_start, col_end) = (
        number(&span["column_start"]) - 1,
        number(&span["column_end"]) - 1,
    );
    match lookup(file) {
        Some(source) => {
            let start = source.map.to_glacier(line - 1, col_start);
            let end = if last_line == line {
                source.map.to_glacier(line - 1, col_end)
            } else {
                source.map.to_glacier(last_line - 1, col_end)
            };
            let text = (line..=last_line)
                .map(|n| source.lines.get(n - 1).cloned().unwrap_or_default())
                .collect();
            Located {
                span,
                path: source.path.clone(),
                line,
                start,
                end: if last_line == line {
                    end.max(start + 1)
                } else {
                    end
                },
                last_line,
                text,
                mapped: true,
            }
        }
        None => Located {
            span,
            path: file.to_owned(),
            line,
            start: col_start,
            end: if last_line == line {
                col_end.max(col_start + 1)
            } else {
                col_end
            },
            last_line,
            text: span["text"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(|t| t["text"].as_str().map(str::to_owned))
                .collect(),
            mapped: false,
        },
    }
}

/// `diagnostic` (rustc's JSON) as data: its primary span located in the
/// `.glacier` source when it's in a generated file, with any suggested
/// fix. For tools (`glacier check --json`, the MCP) that read errors
/// rather than show them.
pub fn structured(diagnostic: &Value, lookup: &mut Lookup) -> Value {
    let spans = diagnostic["spans"].as_array().cloned().unwrap_or_default();
    let primary = spans
        .iter()
        .find(|s| s["is_primary"] == true)
        .or(spans.first());
    let mut out = serde_json::json!({
        "source": "rustc",
        "severity": diagnostic["level"],
        "message": diagnostic["message"],
    });
    if let Some(code) = diagnostic["code"]["code"].as_str() {
        out["code"] = code.into();
    }
    if let Some(span) = primary {
        let at = locate(span, lookup);
        out["file"] = at.path.clone().into();
        out["line"] = at.line.into();
        out["column"] = (at.start + 1).into();
        out["end_line"] = at.last_line.into();
        out["end_column"] = (at.end + 1).into();
        if let Some(label) = span["label"].as_str() {
            out["label"] = label.into();
        }
    }
    let mut notes = Vec::new();
    for child in diagnostic["children"].as_array().into_iter().flatten() {
        if let Some(message) = child["message"].as_str().filter(|m| !m.is_empty()) {
            notes.push(format!(
                "{}: {message}",
                child["level"].as_str().unwrap_or("note")
            ));
        }
        // The first concrete replacement rustc suggests is the fix.
        let suggestion = child["spans"]
            .as_array()
            .into_iter()
            .flatten()
            .find(|s| s["suggested_replacement"].is_string());
        if out.get("fix").is_none()
            && let Some(span) = suggestion
        {
            let at = locate(span, lookup);
            out["fix"] = serde_json::json!({
                "file": at.path,
                "line": at.line,
                "column": at.start + 1,
                "end_line": at.last_line,
                "end_column": at.end + 1,
                "replacement": span["suggested_replacement"],
            });
        }
    }
    if !notes.is_empty() {
        out["notes"] = notes.into();
    }
    out
}

/// Renders `diagnostic` against `.glacier` source, or `None` when none of
/// its spans is in a generated file.
pub fn render(diagnostic: &Value, lookup: &mut Lookup, color: bool) -> Option<String> {
    let style = Style { color };
    let spans: Vec<Located> = diagnostic["spans"]
        .as_array()
        .into_iter()
        .flatten()
        .map(|span| locate(span, lookup))
        .collect();
    if !spans.iter().any(|s| s.mapped) {
        return None;
    }

    let level = diagnostic["level"].as_str().unwrap_or("error");
    let message = diagnostic["message"].as_str().unwrap_or_default();
    let code = diagnostic["code"]["code"]
        .as_str()
        .map(|c| format!("[{c}]"))
        .unwrap_or_default();
    let mut out = format!(
        "{}{}: {}\n",
        style.level(level),
        style.paint(if level == "error" { "1;31" } else { "1;33" }, &code),
        style.paint("1", message)
    );

    // Suggestions in children, located like spans.
    let children: Vec<(&Value, Vec<Located>)> = diagnostic["children"]
        .as_array()
        .into_iter()
        .flatten()
        .map(|child| {
            let spans = child["spans"]
                .as_array()
                .into_iter()
                .flatten()
                .map(|span| locate(span, lookup))
                .collect();
            (child, spans)
        })
        .collect();

    let width = spans
        .iter()
        .chain(children.iter().flat_map(|(_, s)| s))
        .map(|s| s.last_line.to_string().len())
        .max()
        .unwrap_or(1);
    let pad = " ".repeat(width);
    let bar = style.gutter("|");

    // Spans grouped by file, the primary span's file first.
    let mut files: Vec<&str> = Vec::new();
    let primary_first = spans
        .iter()
        .filter(|s| s.span["is_primary"].as_bool() == Some(true))
        .chain(spans.iter());
    for located in primary_first {
        if !files.contains(&located.path.as_str()) {
            files.push(&located.path);
        }
    }
    for (i, file) in files.iter().enumerate() {
        let mut group: Vec<&Located> = spans.iter().filter(|s| s.path == *file).collect();
        group.sort_by_key(|s| (s.line, s.span["is_primary"].as_bool() != Some(true)));
        let first = group
            .iter()
            .find(|s| s.span["is_primary"].as_bool() == Some(true))
            .unwrap_or(&group[0]);
        let arrow = if i == 0 { "-->" } else { ":::" };
        let _ = writeln!(
            out,
            "{pad}{} {file}:{}:{}",
            style.gutter(arrow),
            first.line,
            first.start + 1
        );
        let _ = writeln!(out, "{pad} {bar}");
        let mut shown_line = None;
        for located in group {
            let primary = located.span["is_primary"].as_bool() == Some(true);
            if shown_line != Some(located.line) {
                for (offset, text) in located.text.iter().enumerate() {
                    let n = located.line + offset;
                    let _ = writeln!(
                        out,
                        "{} {bar} {text}",
                        style.gutter(&format!("{n:>width$}"))
                    );
                }
                shown_line = Some(located.line);
            }
            let label = located.span["label"].as_str().unwrap_or_default();
            let (mark, paint) = if primary {
                ("^", if level == "error" { "1;31" } else { "1;33" })
            } else {
                ("-", "1;34")
            };
            let marks = if located.last_line == located.line {
                format!(
                    "{}{}",
                    " ".repeat(located.start),
                    mark.repeat(located.end - located.start)
                )
            } else {
                // A multi-line span: point at where it ends.
                format!("{}{mark}", " ".repeat(located.end.saturating_sub(1)))
            };
            let label = if label.is_empty() {
                String::new()
            } else {
                format!(" {label}")
            };
            let _ = writeln!(
                out,
                "{pad} {bar} {}",
                style.paint(paint, &format!("{marks}{label}"))
            );
        }
        if i + 1 < files.len() {
            let _ = writeln!(out, "{pad} {bar}");
        }
    }

    for (i, (child, located)) in children.iter().enumerate() {
        let level = child["level"].as_str().unwrap_or("note");
        let message = child["message"].as_str().unwrap_or_default();
        match suggestion(located) {
            // A fix in a .glacier line: show the line with it applied.
            Some((line, text)) => {
                let _ = writeln!(out, "{pad} {bar}");
                let _ = writeln!(out, "{}: {}", style.level(level), style.paint("1", message));
                let _ = writeln!(out, "{pad} {bar}");
                let _ = writeln!(
                    out,
                    "{} {bar} {text}",
                    style.gutter(&format!("{line:>width$}"))
                );
            }
            None => {
                let after_suggestion = i > 0 && suggestion(&children[i - 1].1).is_some();
                if i == 0 || after_suggestion {
                    let _ = writeln!(out, "{pad} {bar}");
                }
                let indent = format!("{pad}   {}", " ".repeat(level.len() + 2));
                let message = message.replace('\n', &format!("\n{indent}"));
                let _ = writeln!(
                    out,
                    "{pad} {} {}: {message}",
                    style.gutter("="),
                    style.paint("1", level)
                );
            }
        }
    }
    out.push('\n');
    Some(out)
}

/// The `.glacier` line with a child's suggested replacements applied, when
/// they all sit on one line of one `.glacier` file.
fn suggestion(spans: &[Located]) -> Option<(usize, String)> {
    let first = spans.first()?;
    let all_here = spans.iter().all(|s| {
        s.mapped
            && s.path == first.path
            && s.line == first.line
            && s.last_line == s.line
            && s.span["suggested_replacement"].is_string()
    });
    if !all_here {
        return None;
    }
    let mut line: Vec<char> = first.text.first()?.chars().collect();
    let mut edits: Vec<&Located> = spans.iter().collect();
    edits.sort_by_key(|s| std::cmp::Reverse(s.start));
    for edit in edits {
        let replacement = edit.span["suggested_replacement"].as_str()?;
        if replacement.contains('\n') || edit.end > line.len() {
            return None;
        }
        // An insertion has an empty span; others replace their columns.
        let end = if edit.span["column_start"] == edit.span["column_end"] {
            edit.start
        } else {
            edit.end
        };
        line.splice(edit.start..end, replacement.chars());
    }
    Some((first.line, line.into_iter().collect()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    const GLACIER: &str = "alias Models.Post\n\ncontroller Posts do\n  def show(ctx) do\n    post = Post.fnd(ctx.id()?)!\n    render post, title: 5\n  end\nend\n";

    fn lookup() -> impl FnMut(&str) -> Option<Rc<Source>> {
        let source = Rc::new(Source {
            path: "src/controllers/posts.glacier".into(),
            lines: GLACIER.lines().map(str::to_owned).collect(),
            map: glacier_dialect::transpile(GLACIER, "posts.glacier")
                .unwrap()
                .map,
        });
        move |file: &str| {
            file.ends_with(".glacier/controllers/posts.rs")
                .then(|| source.clone())
        }
    }

    fn rust_line(n: usize) -> String {
        glacier_dialect::transpile(GLACIER, "posts.glacier")
            .unwrap()
            .rust
            .lines()
            .nth(n - 1)
            .unwrap()
            .to_owned()
    }

    fn span(
        file: &str,
        line: usize,
        start: usize,
        end: usize,
        primary: bool,
        label: &str,
    ) -> Value {
        json!({
            "file_name": file, "line_start": line, "line_end": line,
            "column_start": start, "column_end": end, "is_primary": primary,
            "label": label, "suggested_replacement": null,
            "text": [{"text": "pub struct Post {"}],
        })
    }

    #[test]
    fn shows_the_glacier_line_and_maps_suggestions() {
        let rust = rust_line(5);
        let col = rust.find("fnd").unwrap() + 1;
        let generated = "/app/.glacier/controllers/posts.rs";
        let mut suggestion = span(generated, 5, col, col + 3, true, "");
        suggestion["suggested_replacement"] = json!("find");
        let diagnostic = json!({
            "level": "error",
            "message": "no associated function named `fnd` found for struct `Post`",
            "code": {"code": "E0599"},
            "spans": [
                span(generated, 5, col, col + 3, true, "not found in `Post`"),
                span("src/models/post.rs", 4, 1, 16, false, "`fnd` not found for this struct"),
            ],
            "children": [
                {"level": "help", "message": "there is an associated function `find` with a similar name", "spans": [suggestion]},
                {"level": "note", "message": "a note\nover two lines", "spans": []},
            ],
        });
        let out = render(&diagnostic, &mut lookup(), false).unwrap();
        assert_eq!(
            out,
            "error[E0599]: no associated function named `fnd` found for struct `Post`
 --> src/controllers/posts.glacier:5:17
  |
5 |     post = Post.fnd(ctx.id()?)!
  |                 ^^^ not found in `Post`
  |
 ::: src/models/post.rs:4:1
  |
4 | pub struct Post {
  | --------------- `fnd` not found for this struct
  |
help: there is an associated function `find` with a similar name
  |
5 |     post = Post.find(ctx.id()?)!
  |
  = note: a note
          over two lines

"
        );
    }

    #[test]
    fn leaves_other_diagnostics_to_rustc() {
        let diagnostic = json!({
            "level": "warning", "message": "unused", "code": null,
            "spans": [span("src/lib.rs", 1, 1, 2, true, "")], "children": [],
        });
        assert!(render(&diagnostic, &mut lookup(), false).is_none());
    }

    #[test]
    fn maps_carets_through_translated_code() {
        // `render post, title: 5` → `render!(post; title = 5)`: the `5`.
        let rust = rust_line(6);
        let col = rust.find("5").unwrap() + 1;
        let diagnostic = json!({
            "level": "error", "message": "mismatched types", "code": {"code": "E0308"},
            "spans": [span("/app/.glacier/controllers/posts.rs", 6, col, col + 1, true, "expected `String`")],
            "children": [],
        });
        let out = render(&diagnostic, &mut lookup(), false).unwrap();
        assert!(
            out.contains(
                "6 |     render post, title: 5\n  |                         ^ expected `String`\n"
            ),
            "{out}"
        );
    }
}