orrery-cli 0.3.0

Command-line interface for the Orrery diagram language
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
//! Error types and miette diagnostic rendering for the Orrery CLI.
//!
//! [`Error`] is the top-level error type returned by [`crate::run()`]. It
//! wraps either a parse error (with rich source-location diagnostics) or
//! a render pipeline error.
//!
//! The [`Error::reportables()`] method converts an error into individually
//! renderable miette diagnostics. For parse errors that contain multiple
//! diagnostics, each one becomes a separate reportable item.
//!
//! # Multi-File Support
//!
//! [`SourceMapSource`] wraps a [`SourceMap`] reference and implements
//! [`miette::SourceCode`], translating virtual-space spans back to the
//! correct file. This enables miette to render source snippets from any
//! file in the import tree.
//!
//! # Import Traces
//!
//! When an error occurs in an imported file, import trace diagnostics are
//! built from primary label spans and exposed through miette's `related()`
//! method so the import location is visible in the rendered output.

use std::{fmt, iter};

use miette::{
    Diagnostic as MietteDiagnostic, LabeledSpan, MietteError, MietteSpanContents, SourceCode,
    SourceSpan, SpanContents,
};

use orrery::RenderError;
use orrery_parser::{
    Span,
    error::{Diagnostic, ParseError},
    source_map::SourceMap,
};

/// Errors that can occur during CLI execution.
///
/// Separates parse errors (which carry a [`SourceMap`] for rich multi-file
/// diagnostics) from render pipeline errors (I/O, layout, export).
#[derive(Debug, thiserror::Error)]
pub enum Error<'a> {
    /// A parse error with structured diagnostics and a source map.
    #[error("{0}")]
    Parse(ParseError<'a>),
    /// A render pipeline error (I/O, graph, layout, or export).
    #[error("{0}")]
    Render(#[from] RenderError),
}

impl<'a> From<ParseError<'a>> for Error<'a> {
    fn from(err: ParseError<'a>) -> Self {
        Self::Parse(err)
    }
}

impl From<std::io::Error> for Error<'_> {
    fn from(err: std::io::Error) -> Self {
        Self::Render(RenderError::Io(err))
    }
}

impl<'a> Error<'a> {
    /// Convert this error into individually renderable miette diagnostics.
    ///
    /// For [`Error::Parse`], returns one reportable per [`Diagnostic`] in the
    /// error, each backed by the shared [`SourceMap`] for multi-file snippet
    /// rendering and import traces.
    ///
    /// For [`Error::Render`], returns a single reportable.
    pub fn reportables(&'a self) -> Vec<Box<dyn MietteDiagnostic + 'a>> {
        match self {
            Error::Parse(parse_err) => {
                let source_map = parse_err.source_map();
                parse_err
                    .diagnostics()
                    .iter()
                    .map(|d| {
                        Box::new(DiagnosticAdapter::new(d, source_map)) as Box<dyn MietteDiagnostic>
                    })
                    .collect()
            }
            Error::Render(render_err) => {
                vec![Box::new(RenderErrorAdapter(render_err)) as Box<dyn MietteDiagnostic>]
            }
        }
    }
}

/// Newtype wrapper around [`SourceMap`] that implements [`miette::SourceCode`].
///
/// Required because the orphan rule prevents implementing a foreign trait
/// (`miette::SourceCode`) on a foreign type (`SourceMap`) directly.
///
/// The implementation translates a virtual-space [`SourceSpan`] to the
/// owning file, computes a local offset, and delegates to the file's source
/// text wrapped in a [`miette::NamedSource`].
#[derive(Debug, Clone, Copy)]
struct SourceMapSource<'a>(&'a SourceMap<'a>);

impl SourceCode for SourceMapSource<'_> {
    fn read_span<'s>(
        &'s self,
        span: &SourceSpan,
        context_lines_before: usize,
        context_lines_after: usize,
    ) -> Result<Box<dyn SpanContents<'s> + 's>, MietteError> {
        let offset = span.offset();

        let file = self.0.lookup_file(offset).ok_or(MietteError::OutOfBounds)?;

        // Translate virtual offset → local offset within the file.
        let local_offset = offset - file.start_offset();
        let local_span = SourceSpan::new(local_offset.into(), span.len());

        // Delegate context-line extraction to the existing `&str` impl.
        let contents =
            file.source()
                .read_span(&local_span, context_lines_before, context_lines_after)?;

        // Translate the local span back to virtual coordinates so that
        // miette can match label offsets (which are virtual) against the
        // returned data range.
        let local = contents.span();
        let virtual_span =
            SourceSpan::new((local.offset() + file.start_offset()).into(), local.len());

        // Re-wrap with the file name so miette prints it in the header.
        Ok(Box::new(MietteSpanContents::new_named(
            file.name().to_owned(),
            contents.data(),
            virtual_span,
            contents.line(),
            contents.column(),
            contents.line_count(),
        )))
    }
}

/// Adapter for a single orrery [`Diagnostic`].
///
/// Wraps a [`Diagnostic`] together with the [`SourceMap`] so that miette
/// can render source snippets from the correct file and display the import
/// trace chain via [`related()`](MietteDiagnostic::related).
#[derive(thiserror::Error)]
#[error("{}", .diag.message())]
struct DiagnosticAdapter<'a> {
    diag: &'a Diagnostic,
    source_code: SourceMapSource<'a>,
    imports: Vec<ImportDiagnostic<'a>>,
}

impl<'a> DiagnosticAdapter<'a> {
    fn new(diag: &'a Diagnostic, source_map: &'a SourceMap<'a>) -> Self {
        let imports = Self::build_imports(diag, source_map);
        Self {
            diag,
            source_code: SourceMapSource(source_map),
            imports,
        }
    }

    /// Build [`ImportDiagnostic`]s for primary labels that fall in imported files.
    ///
    /// For each primary label, looks up its file and extracts the
    /// `first_imported_at` span — the `import "…";` declaration in the
    /// parent file. Root-file labels (where `first_imported_at` is `None`)
    /// are skipped.
    fn build_imports(
        diag: &Diagnostic,
        source_map: &'a SourceMap<'a>,
    ) -> Vec<ImportDiagnostic<'a>> {
        diag.labels()
            .iter()
            .filter(|l| l.is_primary())
            .filter_map(|l| {
                source_map
                    .lookup_file_by_span(l.span())
                    .and_then(|f| f.first_imported_at())
            })
            .map(|import_span| ImportDiagnostic::new(import_span, source_map))
            .collect()
    }
}

impl fmt::Debug for DiagnosticAdapter<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DiagnosticAdapter")
            .field("diag", &self.diag)
            .finish()
    }
}

impl MietteDiagnostic for DiagnosticAdapter<'_> {
    fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
        self.diag
            .code()
            .map(|c| Box::new(c) as Box<dyn fmt::Display>)
    }

    fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
        self.diag
            .help()
            .map(|h| Box::new(h) as Box<dyn fmt::Display>)
    }

    fn source_code(&self) -> Option<&dyn miette::SourceCode> {
        Some(&self.source_code as &dyn miette::SourceCode)
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
        let labels = self.diag.labels();
        if labels.is_empty() {
            return None;
        }

        Some(Box::new(labels.iter().map(|label| {
            let span = span_to_miette(label.span());
            let message = Some(label.message().to_string());
            if label.is_primary() {
                LabeledSpan::new_primary_with_span(message, span)
            } else {
                LabeledSpan::new_with_span(message, span)
            }
        })))
    }

    fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn MietteDiagnostic> + 'a>> {
        if self.imports.is_empty() {
            return None;
        }
        Some(Box::new(
            self.imports
                .iter()
                .map(|import| import as &dyn MietteDiagnostic),
        ))
    }
}

#[derive(Debug, thiserror::Error)]
struct ImportDiagnostic<'a> {
    span: Span,
    source_code: SourceMapSource<'a>,
    next: Option<Box<ImportDiagnostic<'a>>>,
}

impl<'a> ImportDiagnostic<'a> {
    fn new(span: Span, source_map: &'a SourceMap<'a>) -> Self {
        let next = source_map
            .lookup_file_by_span(span)
            .and_then(|f| f.first_imported_at())
            .map(|import_span| Box::new(ImportDiagnostic::new(import_span, source_map)));
        ImportDiagnostic {
            span,
            source_code: SourceMapSource(source_map),
            next,
        }
    }
}

impl fmt::Display for ImportDiagnostic<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "import trace")
    }
}

impl MietteDiagnostic for ImportDiagnostic<'_> {
    fn source_code(&self) -> Option<&dyn SourceCode> {
        Some(&self.source_code as &dyn SourceCode)
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
        let span = span_to_miette(self.span);
        Some(Box::new(iter::once(LabeledSpan::new_with_span(
            Some("imported here".to_string()),
            span,
        ))))
    }

    fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn MietteDiagnostic> + 'a>> {
        let next = self.next.as_ref()?;
        Some(Box::new(iter::once(next.as_ref() as &dyn MietteDiagnostic)))
    }
}

/// Adapter for [`RenderError`] variants so they can be rendered by miette.
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
struct RenderErrorAdapter<'a>(&'a RenderError);

impl MietteDiagnostic for RenderErrorAdapter<'_> {
    fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
        let code = match &self.0 {
            RenderError::Io(_) => "orrery::io",
            RenderError::Graph(_) => "orrery::graph",
            RenderError::Layout(_) => "orrery::layout",
            RenderError::Export(_) => "orrery::export",
        };
        Some(Box::new(code))
    }
}

/// Convert an orrery [`Span`] to a miette [`SourceSpan`].
fn span_to_miette(span: Span) -> SourceSpan {
    SourceSpan::new(span.start().into(), span.len())
}

#[cfg(test)]
mod tests {
    use orrery_parser::error::ErrorCode;

    use super::*;

    /// Helper: build a SourceMap with a single file.
    fn single_file_source_map<'a>(name: &str, source: &'a str) -> SourceMap<'a> {
        let mut sm = SourceMap::new();
        sm.add_file(name, source, None);
        sm
    }

    #[test]
    fn test_single_diagnostic_with_source_map() {
        let source = "hello";
        let sm = single_file_source_map("test.orr", source);
        let diag = Diagnostic::error("test error")
            .with_code(ErrorCode::E300)
            .with_label(Span::new(0..5), "here")
            .with_help("try this");
        let parse_err = ParseError::new(vec![diag], sm);
        let err = Error::Parse(parse_err);

        let reportables = err.reportables();
        assert_eq!(reportables.len(), 1);
        assert_eq!(reportables[0].to_string(), "test error");
    }

    #[test]
    fn test_multiple_diagnostics() {
        let source = "source code that is long enough for spans";
        let sm = single_file_source_map("test.orr", source);
        let diags = vec![
            Diagnostic::error("first error")
                .with_code(ErrorCode::E300)
                .with_label(Span::new(0..5), "first"),
            Diagnostic::error("second error")
                .with_code(ErrorCode::E301)
                .with_label(Span::new(10..15), "second")
                .with_help("help for second"),
            Diagnostic::error("third error").with_label(Span::new(20..25), "third"),
        ];
        let parse_err = ParseError::new(diags, sm);
        let err = Error::Parse(parse_err);

        let reportables = err.reportables();

        assert_eq!(reportables.len(), 3);
        assert_eq!(reportables[0].to_string(), "first error");
        assert_eq!(reportables[1].to_string(), "second error");
        assert_eq!(reportables[2].to_string(), "third error");
    }

    #[test]
    fn test_single_render_error() {
        let err = Error::Render(RenderError::Graph("graph error".to_string()));

        let reportables = err.reportables();

        assert_eq!(reportables.len(), 1);
        assert_eq!(reportables[0].to_string(), "Graph error: graph error");
    }

    #[test]
    fn test_all_labels_returned() {
        let source = "some source code text";
        let sm = single_file_source_map("test.orr", source);
        let diag = Diagnostic::error("error with labels")
            .with_label(Span::new(0..5), "primary label")
            .with_secondary_label(Span::new(10..15), "secondary label");

        let adapter = DiagnosticAdapter::new(&diag, &sm);

        let labels: Vec<_> = adapter.labels().unwrap().collect();
        assert_eq!(labels.len(), 2);
        assert_eq!(labels[0].label(), Some("primary label"));
        assert_eq!(labels[1].label(), Some("secondary label"));
    }

    #[test]
    fn test_primary_flag_on_labels() {
        let source = "some source code text";
        let sm = single_file_source_map("test.orr", source);
        let diag = Diagnostic::error("error with labels")
            .with_label(Span::new(0..5), "primary")
            .with_secondary_label(Span::new(10..15), "secondary");

        let adapter = DiagnosticAdapter::new(&diag, &sm);

        let labels: Vec<_> = adapter.labels().unwrap().collect();
        assert_eq!(labels.len(), 2);
        assert!(labels[0].primary());
        assert!(!labels[1].primary());
    }

    #[test]
    fn test_source_map_as_source_code() {
        use miette::SourceCode;

        let source = "line one\nline two\nline three";
        let sm = single_file_source_map("main.orr", source);
        let src = SourceMapSource(&sm);

        // Read a span in the middle of the source.
        let span = SourceSpan::new(9.into(), 8); // "line two"
        let contents = src
            .read_span(&span, 0, 0)
            .expect("read_span should succeed");
        let text = std::str::from_utf8(contents.data()).unwrap();
        assert!(text.contains("line two"));
    }

    #[test]
    fn test_source_map_multi_file() {
        use miette::SourceCode;

        let mut sm = SourceMap::new();
        let _base_a = sm.add_file("a.orr", "aaaa", None);
        let base_b = sm.add_file("b.orr", "bbbb", Some(Span::new(0..4)));
        let src = SourceMapSource(&sm);

        // Read from the second file.
        let span = SourceSpan::new(base_b.into(), 4);
        let contents = src
            .read_span(&span, 0, 0)
            .expect("read_span should succeed");
        let name = contents.name().expect("should have a file name");
        assert_eq!(name, "b.orr");
    }

    #[test]
    fn test_import_trace_root_file_no_related() {
        let source = "diagram component;\nbox: Rectangle;";
        let sm = single_file_source_map("main.orr", source);
        let diag = Diagnostic::error("error in root").with_label(Span::new(0..7), "here");

        let adapter = DiagnosticAdapter::new(&diag, &sm);

        // Root file → no import trace → related() returns None.
        assert!(adapter.related().is_none());
    }

    #[test]
    fn test_import_trace_imported_file_has_related() {
        let mut sm = SourceMap::new();
        // Root file: "import \"lib\";\n" (14 bytes)
        let _base_root = sm.add_file("main.orr", "import \"lib\";\n", None);
        // Imported file starts after root + 1-byte gap
        let base_lib = sm.add_file("lib.orr", "library;\ntype Bad;", Some(Span::new(0..13)));

        // Error in the imported file (e.g., at offset base_lib..base_lib+7 = "library")
        let diag = Diagnostic::error("error in lib")
            .with_label(Span::new(base_lib..(base_lib + 7)), "here");

        let adapter = DiagnosticAdapter::new(&diag, &sm);

        // Should have one related diagnostic showing the import chain.
        let related: Vec<_> = adapter.related().unwrap().collect();
        assert_eq!(related.len(), 1);
        assert!(related[0].labels().is_some());
    }
}