Skip to content

Commit 64f8001

Browse files
committed
Use ariadne
#1323
1 parent 5e3dcff commit 64f8001

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export JUST_LOG := log
1212
watch +args='test':
1313
cargo watch --clear --exec '{{ args }}'
1414

15-
[group: 'test']
15+
[group('test', 'bogus extra argument')]
1616
test:
1717
cargo test --all
1818

src/compile_error.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,102 @@ impl<'src> CompileError<'src> {
1919
}
2020
}
2121

22+
pub(crate) fn render_compile_error(error: &CompileError) {
23+
use ariadne::{Label, Report, ReportKind, Source};
24+
25+
let token = error.token;
26+
let source = Source::from(token.src);
27+
28+
let start = token.offset;
29+
let end = token.offset + token.length;
30+
31+
let path = format!("{}", token.path.display());
32+
let label = Label::new((&path, start..end));
33+
34+
let report = Report::build(ReportKind::Error, &path, start);
35+
36+
let report = match &*error.kind {
37+
CompileErrorKind::AttributeArgumentCountMismatch { attribute, found, min, max } => {
38+
39+
let label_msg = format!("Found {found} {}", Count("argument", *found));
40+
41+
let note = if min == max {
42+
format!("`{attribute}` takes {min} {}", Count("argument", *min))
43+
} else {
44+
format!("`{attribute}` takes between {min} and {max} arguments")
45+
};
46+
47+
report
48+
.with_code(1)
49+
.with_message("Attribute argument count mismatch")
50+
.with_label(label.with_message(label_msg))
51+
.with_note(note)
52+
.finish()
53+
},
54+
_ => {
55+
56+
let message = format!("{error}");
57+
report
58+
.with_message(message)
59+
.with_label(label)
60+
.finish()
61+
}
62+
/*
63+
CompileErrorKind::BacktickShebang => todo!(),
64+
CompileErrorKind::CircularRecipeDependency { recipe, circle } => todo!(),
65+
CompileErrorKind::CircularVariableDependency { variable, circle } => todo!(),
66+
CompileErrorKind::DependencyArgumentCountMismatch { dependency, found, min, max } => todo!(),
67+
CompileErrorKind::Redefinition { first, first_type, name, second_type } => todo!(),
68+
CompileErrorKind::DuplicateAttribute { attribute, first } => todo!(),
69+
CompileErrorKind::DuplicateParameter { recipe, parameter } => todo!(),
70+
CompileErrorKind::DuplicateSet { setting, first } => todo!(),
71+
CompileErrorKind::DuplicateVariable { variable } => todo!(),
72+
CompileErrorKind::DuplicateUnexport { variable } => todo!(),
73+
CompileErrorKind::ExpectedKeyword { expected, found } => todo!(),
74+
CompileErrorKind::ExportUnexported { variable } => todo!(),
75+
CompileErrorKind::ExtraLeadingWhitespace => todo!(),
76+
CompileErrorKind::ExtraneousAttributes { count } => todo!(),
77+
CompileErrorKind::FunctionArgumentCountMismatch { function, found, expected } => todo!(),
78+
CompileErrorKind::Include => todo!(),
79+
CompileErrorKind::InconsistentLeadingWhitespace { expected, found } => todo!(),
80+
CompileErrorKind::Internal { message } => todo!(),
81+
CompileErrorKind::InvalidAttribute { item_kind, item_name, attribute } => todo!(),
82+
CompileErrorKind::InvalidEscapeSequence { character } => todo!(),
83+
CompileErrorKind::MismatchedClosingDelimiter { close, open, open_line } => todo!(),
84+
CompileErrorKind::MixedLeadingWhitespace { whitespace } => todo!(),
85+
CompileErrorKind::ParameterFollowsVariadicParameter { parameter } => todo!(),
86+
CompileErrorKind::ParsingRecursionDepthExceeded => todo!(),
87+
CompileErrorKind::RequiredParameterFollowsDefaultParameter { parameter } => todo!(),
88+
CompileErrorKind::ShebangAndScriptAttribute { recipe } => todo!(),
89+
CompileErrorKind::ShellExpansion { err } => todo!(),
90+
CompileErrorKind::UndefinedVariable { variable } => todo!(),
91+
CompileErrorKind::UnexpectedCharacter { expected } => todo!(),
92+
CompileErrorKind::UnexpectedClosingDelimiter { close } => todo!(),
93+
CompileErrorKind::UnexpectedEndOfToken { expected } => todo!(),
94+
CompileErrorKind::UnexpectedToken { expected, found } => todo!(),
95+
CompileErrorKind::UnicodeEscapeCharacter { character } => todo!(),
96+
CompileErrorKind::UnicodeEscapeDelimiter { character } => todo!(),
97+
CompileErrorKind::UnicodeEscapeEmpty => todo!(),
98+
CompileErrorKind::UnicodeEscapeLength { hex } => todo!(),
99+
CompileErrorKind::UnicodeEscapeRange { hex } => todo!(),
100+
CompileErrorKind::UnicodeEscapeUnterminated => todo!(),
101+
CompileErrorKind::UnknownAliasTarget { alias, target } => todo!(),
102+
CompileErrorKind::UnknownAttribute { attribute } => todo!(),
103+
CompileErrorKind::UnknownDependency { recipe, unknown } => todo!(),
104+
CompileErrorKind::UnknownFunction { function } => todo!(),
105+
CompileErrorKind::UnknownSetting { setting } => todo!(),
106+
CompileErrorKind::UnknownStartOfToken => todo!(),
107+
CompileErrorKind::UnpairedCarriageReturn => todo!(),
108+
CompileErrorKind::UnterminatedBacktick => todo!(),
109+
CompileErrorKind::UnterminatedInterpolation => todo!(),
110+
CompileErrorKind::UnterminatedString => todo!(),
111+
*/
112+
};
113+
114+
report.eprint((&path, source))
115+
.unwrap();
116+
}
117+
22118
fn capitalize(s: &str) -> String {
23119
let mut chars = s.chars();
24120
match chars.next() {

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,13 @@ impl<'src> ColorDisplay for Error<'src> {
504504
}
505505
}
506506

507+
pub(crate) fn render_error(error: &Error, color: Color) {
508+
match error {
509+
Error::Compile { compile_error } => compile_error::render_compile_error(compile_error),
510+
_ => eprintln!("{}", error.color_display(color.stderr())),
511+
}
512+
}
513+
507514
fn format_cmd(binary: &OsString, arguments: &Vec<OsString>) -> String {
508515
iter::once(binary)
509516
.chain(arguments)

src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
2929
})
3030
.map_err(|error| {
3131
if !verbosity.quiet() && error.print_message() {
32-
eprintln!("{}", error.color_display(color.stderr()));
32+
crate::error::render_error(&error, color);
3333
}
3434
error.code().unwrap_or(EXIT_FAILURE)
3535
})

0 commit comments

Comments
 (0)