Skip to content

Commit 4d92454

Browse files
authored
Colourize examples in error output (#64)
Since we went to all the trouble of having fancy ANSI colour when formatting code and printing to a terminal (something that Technique v0 did very nicely and we finally caught up with in #61), there's no reason we couldn't use the same styles when rendering help text and examples in compiler error messages. This ended up extending into a fairly broad refactoring, moving the formatting and display of error messages out of the library crate and into the application layer, renaming a number of (internal) traits and their methods. Considerable revision of the examples in the errors also accompanied this change.
2 parents b1022da + 2a24c1d commit 4d92454

File tree

20 files changed

+1479
-719
lines changed

20 files changed

+1479
-719
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "technique"
3-
version = "0.3.4"
3+
version = "0.3.5"
44
edition = "2021"
55
description = "A domain specific language for procedures."
66
authors = [ "Andrew Cowie" ]

src/error/display.rs

Lines changed: 0 additions & 209 deletions
This file was deleted.

src/error/mod.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/formatting/formatter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ impl Formatter {
912912
}
913913
}
914914

915+
915916
impl ToString for Formatter {
916917
fn to_string(&self) -> String {
917918
let mut result = String::new();

src/formatting/renderer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ pub enum Syntax {
3535
/// Trait for different rendering backends (the no-op no-markup one, ANSI
3636
/// escapes for terminal colouring, Typst markup for documents)
3737
pub trait Render {
38-
/// Render content with the specified type/style
39-
fn render(&self, content_type: Syntax, content: &str) -> String;
38+
/// Apply styling to content with the specified syntax type
39+
fn style(&self, content_type: Syntax, content: &str) -> String;
4040
}
4141

4242
/// Returns content unchanged, with no markup applied
4343
pub struct Identity;
4444

4545
impl Render for Identity {
46-
fn render(&self, _syntax: Syntax, content: &str) -> String {
46+
fn style(&self, _syntax: Syntax, content: &str) -> String {
4747
content.to_string()
4848
}
4949
}
@@ -68,12 +68,12 @@ fn format_to_fragments(technique: &Document, width: u8) -> Vec<(Syntax, String)>
6868
crate::formatting::formatter::format_with_renderer(technique, width)
6969
}
7070

71-
/// Pass 2: apply markup to fragments via render() and combine.
71+
/// Pass 2: apply markup to fragments via style() and combine.
7272
fn render_to_string(renderer: &impl Render, fragments: Vec<(Syntax, String)>) -> String {
7373
let mut output = String::new();
7474

7575
for (syntax, content) in fragments {
76-
let rendered = renderer.render(syntax, &content);
76+
let rendered = renderer.style(syntax, &content);
7777
output.push_str(&rendered);
7878
}
7979

src/language/error.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::{fmt, path::Path};
2+
3+
#[derive(Debug, Clone, PartialEq, Eq)]
4+
pub struct LoadingError<'i> {
5+
pub problem: String,
6+
pub details: String,
7+
pub filename: &'i Path,
8+
}
9+
10+
11+
impl<'i> fmt::Display for LoadingError<'i> {
12+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13+
write!(f, "{}: {}", self.problem, self.details)
14+
}
15+
}

src/language/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Types representing the Technique procedures language
22

3+
mod error;
34
mod quantity;
45
mod types;
56

67
// Re-export all public symbols
8+
pub use error::*;
79
pub use quantity::*;
810
pub use types::*;

src/language/quantity.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,6 @@ pub fn convert_superscript(input: &str) -> String {
166166
.collect()
167167
}
168168

169-
fn to_superscript(num: i8) -> String {
170-
num.to_string()
171-
.chars()
172-
.map(|c| match c {
173-
'0' => '⁰',
174-
'1' => '¹',
175-
'2' => '²',
176-
'3' => '³',
177-
'4' => '⁴',
178-
'5' => '⁵',
179-
'6' => '⁶',
180-
'7' => '⁷',
181-
'8' => '⁸',
182-
'9' => '⁹',
183-
'-' => '⁻',
184-
_ => c,
185-
})
186-
.collect()
187-
}
188-
189169
impl Display for Decimal {
190170
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191171
if self.precision == 0 {
@@ -230,8 +210,28 @@ impl<'i> Display for Quantity<'i> {
230210
}
231211
}
232212

213+
fn to_superscript(num: i8) -> String {
214+
num.to_string()
215+
.chars()
216+
.map(|c| match c {
217+
'0' => '⁰',
218+
'1' => '¹',
219+
'2' => '²',
220+
'3' => '³',
221+
'4' => '⁴',
222+
'5' => '⁵',
223+
'6' => '⁶',
224+
'7' => '⁷',
225+
'8' => '⁸',
226+
'9' => '⁹',
227+
'-' => '⁻',
228+
_ => c,
229+
})
230+
.collect()
231+
}
232+
233233
#[cfg(test)]
234-
mod tests {
234+
mod check {
235235
use super::*;
236236

237237
#[test]

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pub mod error;
1+
pub mod formatting;
22
pub mod language;
33
pub mod parsing;
44
pub mod regex;
5-
pub mod formatting;

0 commit comments

Comments
 (0)