#[derive(Debug, Clone, PartialEq, Eq)]
enum Item {
Function {
location: Option<String>,
name: String,
excerpt: Option<String>,
details: Vec<String>,
},
Expression { location: String, excerpt: String },
Note(String),
}
#[derive(Debug)]
struct Entry {
location: Option<String>,
name: Option<String>,
excerpt: Option<String>,
details: Vec<String>,
}
pub fn compact_trace(trace: &str) -> String {
let Some(items) = parse(trace) else {
return indent(trace.lines().map(str::to_string));
};
let mut lines = Vec::new();
let mut entries: Vec<Entry> = Vec::new();
let mut pending: Option<Entry> = None;
for item in items {
match item {
Item::Function {
location,
name,
excerpt,
details,
} => {
pending = None;
entries.push(Entry {
location,
name: Some(name),
excerpt,
details,
});
}
Item::Expression { location, excerpt } => match entries.last_mut() {
Some(entry) if entry.name.as_deref().is_some_and(is_known) => {
entry.location = Some(location);
entry.excerpt = Some(excerpt);
}
_ => {
pending = Some(Entry {
location: Some(location),
name: None,
excerpt: Some(excerpt),
details: Vec::new(),
});
}
},
Item::Note(note) => match entries.last_mut() {
Some(entry) => entry.details.push(note),
None => lines.push(note),
},
}
}
entries.extend(pending);
for entry in entries {
lines.push(match (entry.location, entry.name) {
(Some(location), Some(name)) => format!("{location}, in {name}"),
(Some(location), None) => location,
(None, Some(name)) => format!("in {name}"),
(None, None) => unreachable!("an entry is a function or is located"),
});
lines.extend(entry.excerpt.map(|excerpt| format!(" {excerpt}")));
lines.extend(
entry
.details
.into_iter()
.map(|detail| format!(" {detail}")),
);
}
indent(lines.into_iter())
}
fn is_known(name: &str) -> bool {
!name.starts_with("<unknown") && !name.starts_with("<ambiguous")
}
fn indent(lines: impl Iterator<Item = String>) -> String {
lines
.map(|line| format!(" {line}"))
.collect::<Vec<_>>()
.join("\n")
}
fn parse(trace: &str) -> Option<Vec<Item>> {
let mut items = Vec::new();
let mut lines = trace.lines();
while let Some(line) = lines.next() {
if let Some(header) = line.strip_prefix("File \"") {
let (path, rest) = header.rsplit_once("\", line ")?;
let (line_number, rest) = rest.split_once(", column ")?;
let (column, name) = match rest.split_once(", in ") {
Some((column, name)) => (column, Some(name)),
None => (rest, None),
};
let line_number = line_number.parse::<usize>().ok()?;
let column = column.parse::<usize>().ok()?;
let excerpt = lines.next()?.strip_prefix(" ")?.to_string();
let carets = lines.next()?.strip_prefix(" ")?;
if !carets.trim_start().starts_with('^') {
return None;
}
let location = format!("{path}:{line_number}:{column}");
items.push(match name {
Some(name) => Item::Function {
location: Some(location),
name: function_name(name),
excerpt: Some(excerpt),
details: Vec::new(),
},
None => Item::Expression { location, excerpt },
});
} else if let Some(name) = line.strip_prefix("in ") {
items.push(Item::Function {
location: None,
name: function_name(name),
excerpt: None,
details: Vec::new(),
});
} else if let Some(detail) = line.strip_prefix(" ") {
let Some(Item::Function { details, .. }) = items.last_mut() else {
return None;
};
details.push(detail.to_string());
} else if line.starts_with("<...") {
items.push(Item::Note(line.to_string()));
} else {
return None;
}
}
Some(items)
}
fn function_name(name: &str) -> String {
name.split_once(" # bound: ")
.map_or(name, |(name, _)| name)
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn functions_are_shown_once_where_they_were() {
let trace = "\
File \"main.rue\", line 5, column 21
test fn test_main() {
^
File \"main.rue\", line 5, column 21, in test_main
test fn test_main() {
^
File \"main.rue\", line 6, column 5
do_the_test();
^^^^^^^^^^^^^
File \"main.rue\", line 10, column 5, in do_the_test
assert value > 10;
^^^^^^^^^^^^^^^^^^
value = 3
File \"main.rue\", line 10, column 5
assert value > 10;
^^^^^^^^^^^^^^^^^^";
assert_eq!(
compact_trace(trace),
" main.rue:6:5, in test_main
do_the_test();
main.rue:10:5, in do_the_test
assert value > 10;
value = 3"
);
}
#[test]
fn notes_and_frames_without_locations_are_kept() {
let trace = "\
<... 2 older frames omitted ...>
File \"main.rue\", line 3, column 5, in <lambda> # bound: limit
item < limit
^^^^^^^^^^^^
limit = 10
item = 30
<... 3 identical frames repeated ...>
in <ambiguous: first | second>
arguments = [-1]
in <ambiguous>
arguments = [-1]";
assert_eq!(
compact_trace(trace),
" <... 2 older frames omitted ...>
main.rue:3:5, in <lambda>
item < limit
limit = 10
item = 30
<... 3 identical frames repeated ...>
in <ambiguous: first | second>
arguments = [-1]
in <ambiguous>
arguments = [-1]"
);
}
#[test]
fn expressions_after_unknown_frames_are_their_own() {
let trace = "\
in <unknown:12d4>
File \"main.rue\", line 3, column 29
fn main(amount: Int) -> Int {
^
File \"main.rue\", line 4, column 5, in main
outer(amount) + 1
^^^^^^^^^^^^^^^^^
amount = 10";
assert_eq!(
compact_trace(trace),
" in <unknown:12d4>
main.rue:4:5, in main
outer(amount) + 1
amount = 10"
);
let trace = "\
in <ambiguous: first | second>
arguments = [-1]
File \"main.rue\", line 6, column 5
assert a > 0;
^^^^^^^^^^^^^";
assert_eq!(
compact_trace(trace),
" in <ambiguous: first | second>
arguments = [-1]
main.rue:6:5
assert a > 0;"
);
}
#[test]
fn inlined_bodies_are_shown_as_functions() {
let trace = "\
File \"main.rue\", line 1, column 24, in main
fn main(x: Int) -> Int {
^
x = 5
File \"main.rue\", line 6, column 46, in checked_sub (inlined)
inline fn checked_sub(a: Int, b: Int) -> Int {
^
File \"main.rue\", line 8, column 9
raise \"underflow\";
^^^^^^^^^^^^^^^^^^";
assert_eq!(
compact_trace(trace),
" main.rue:1:24, in main
fn main(x: Int) -> Int {
x = 5
main.rue:8:9, in checked_sub (inlined)
raise \"underflow\";"
);
}
#[test]
fn a_trace_without_functions_keeps_its_last_frame() {
let trace = "\
File \"main.rue\", line 1, column 1
raise;
^^^^^^";
assert_eq!(compact_trace(trace), " main.rue:1:1\n raise;");
}
#[test]
fn traces_in_another_shape_are_kept_as_they_are() {
for trace in [
"something else",
"File \"main.rue\", line x, column 1\n a\n ^",
"File \"main.rue\", line 1, column 1\n a\n b",
"File \"main.rue\", line 1, column 1\n a\n ^\n x = 1",
] {
assert_eq!(
compact_trace(trace),
indent(trace.lines().map(str::to_string))
);
}
}
}