Skip to content

Commit

Permalink
Fixed poor build API
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Oct 28, 2024
1 parent f67d58a commit 80c140d
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/multifile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let b = colors.next();
let c = colors.next();

Report::build(ReportKind::Error, "b.tao", 10)
Report::build(ReportKind::Error, ("b.tao", 10..14))
.with_code(3)
.with_message("Cannot add types Nat and Str".to_string())
.with_label(
Expand Down
2 changes: 1 addition & 1 deletion examples/multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
let out = Color::Fixed(81);
let out2 = colors.next();

Report::build(ReportKind::Error, "sample.tao", 12)
Report::build(ReportKind::Error, ("sample.tao", 32..33))
.with_code(3)
.with_message("Incompatible types".to_string())
.with_label(
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ariadne::{Color, Config, Label, Report, ReportKind, Source};

fn main() {
Report::build(ReportKind::Error, (), 34)
Report::build(ReportKind::Error, 34..34)
.with_message("Incompatible types")
.with_label(Label::new(32..33).with_message("This is of type Nat"))
.with_label(Label::new(42..45).with_message("This is of type Str"))
Expand All @@ -11,7 +11,7 @@ fn main() {

const SOURCE: &str = "a b c d e f";
// also supports labels with no messages to only emphasis on some areas
Report::build(ReportKind::Error, (), 34)
Report::build(ReportKind::Error, 2..3)
.with_message("Incompatible types")
.with_config(Config::default().with_compact(true))
.with_label(Label::new(0..1).with_color(Color::Red))
Expand Down
2 changes: 1 addition & 1 deletion examples/stresstest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ariadne::{Color, ColorGenerator, Config, Label, Report, ReportKind, Source};
fn main() {
let mut colors = ColorGenerator::new();

Report::build(ReportKind::Error, "stresstest.tao", 13)
Report::build(ReportKind::Error, ("stresstest.tao", 13..13))
.with_code(3)
.with_message("Incompatible types".to_string())
.with_label(
Expand Down
147 changes: 147 additions & 0 deletions src/.write.rs.pending-snap

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,21 @@ pub struct Report<'a, S: Span = Range<usize>> {
msg: Option<String>,
notes: Vec<String>,
help: Option<String>,
location: (<S::SourceId as ToOwned>::Owned, usize),
span: S,
labels: Vec<Label<S>>,
config: Config,
}

impl<S: Span> Report<'_, S> {
/// Begin building a new [`Report`].
pub fn build<Id: Into<<S::SourceId as ToOwned>::Owned>>(
kind: ReportKind,
src_id: Id,
offset: usize,
) -> ReportBuilder<S> {
pub fn build(kind: ReportKind, span: S) -> ReportBuilder<S> {
ReportBuilder {
kind,
code: None,
msg: None,
notes: vec![],
help: None,
location: (src_id.into(), offset),
span,
labels: Vec::new(),
config: Config::default(),
}
Expand Down Expand Up @@ -287,7 +283,7 @@ pub struct ReportBuilder<'a, S: Span> {
msg: Option<String>,
notes: Vec<String>,
help: Option<String>,
location: (<S::SourceId as ToOwned>::Owned, usize),
span: S,
labels: Vec<Label<S>>,
config: Config,
}
Expand Down Expand Up @@ -384,7 +380,7 @@ impl<'a, S: Span> ReportBuilder<'a, S> {
msg: self.msg,
notes: self.notes,
help: self.help,
location: self.location,
span: self.span,
labels: self.labels,
config: self.config,
}
Expand Down
50 changes: 25 additions & 25 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ impl<S: Span> Report<'_, S> {
let line_range = src.get_line_range(&char_span);

// File name & reference
let location = if src_id == self.location.0.borrow() {
self.location.1
let location = if src_id == self.span.source() {
self.span.start()
} else {
labels[0].char_span.start
};
Expand Down Expand Up @@ -956,7 +956,7 @@ mod tests {

#[test]
fn one_message() {
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.finish()
Expand All @@ -969,7 +969,7 @@ mod tests {
#[test]
fn two_labels_without_messages() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5))
Expand All @@ -989,7 +989,7 @@ mod tests {
#[test]
fn two_labels_with_messages() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
Expand All @@ -1013,7 +1013,7 @@ mod tests {
#[test]
fn multi_byte_chars() {
let source = "äpplë == örängë;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii().with_index_type(IndexType::Char))
.with_message("can't compare äpplës with örängës")
.with_label(Label::new(0..5).with_message("This is an äpplë"))
Expand All @@ -1037,7 +1037,7 @@ mod tests {
#[test]
fn byte_label() {
let source = "äpplë == örängë;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
.with_message("can't compare äpplës with örängës")
.with_label(Label::new(0..7).with_message("This is an äpplë"))
Expand All @@ -1061,7 +1061,7 @@ mod tests {
#[test]
fn byte_column() {
let source = "äpplë == örängë;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 11)
let msg = Report::build(ReportKind::Error, 11..11)
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
.with_message("can't compare äpplës with örängës")
.with_label(Label::new(0..7).with_message("This is an äpplë"))
Expand All @@ -1085,7 +1085,7 @@ mod tests {
#[test]
fn label_at_end_of_long_line() {
let source = format!("{}orange", "apple == ".repeat(100));
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(
Expand All @@ -1108,7 +1108,7 @@ mod tests {
#[test]
fn label_of_width_zero_at_end_of_line() {
let source = "apple ==\n";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
.with_message("unexpected end of file")
.with_label(Label::new(9..9).with_message("Unexpected end of file"))
Expand All @@ -1129,7 +1129,7 @@ mod tests {
#[test]
fn empty_input() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
Expand All @@ -1150,7 +1150,7 @@ mod tests {
#[test]
fn empty_input_help() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
Expand All @@ -1174,7 +1174,7 @@ mod tests {
#[test]
fn empty_input_note() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
Expand All @@ -1198,7 +1198,7 @@ mod tests {
#[test]
fn empty_input_help_note() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
Expand Down Expand Up @@ -1228,7 +1228,7 @@ mod tests {

for i in 0..=source.len() {
for j in i..=source.len() {
let _ = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let _ = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
.with_message("Label")
.with_label(Label::new(i..j).with_message("Label"))
Expand All @@ -1241,7 +1241,7 @@ mod tests {
#[test]
fn multiline_label() {
let source = "apple\n==\norange";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_label(Label::new(0..source.len()).with_message("illegal comparison"))
.finish()
Expand All @@ -1263,7 +1263,7 @@ mod tests {
#[test]
fn partially_overlapping_labels() {
let source = "https://example.com/";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_label(Label::new(0..source.len()).with_message("URL"))
.with_label(Label::new(0..source.find(':').unwrap()).with_message("scheme"))
Expand All @@ -1286,7 +1286,7 @@ mod tests {
#[test]
fn multiple_labels_same_span() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
Expand Down Expand Up @@ -1321,7 +1321,7 @@ mod tests {
#[test]
fn note() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
Expand All @@ -1347,7 +1347,7 @@ mod tests {
#[test]
fn help() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
Expand All @@ -1373,7 +1373,7 @@ mod tests {
#[test]
fn help_and_note() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
Expand Down Expand Up @@ -1402,13 +1402,13 @@ mod tests {
#[test]
fn single_note_single_line() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..15).with_message("This is a strange comparison"))
.with_note("No need to try, they can't be compared.")
.finish()
.write_to_string(Source::from(source));
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error: can't compare apples with oranges
,-[<unknown>:1:1]
Expand All @@ -1425,7 +1425,7 @@ mod tests {
#[test]
fn multi_notes_single_lines() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..15).with_message("This is a strange comparison"))
Expand All @@ -1451,7 +1451,7 @@ mod tests {
#[test]
fn multi_notes_multi_lines() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
let msg = Report::build(ReportKind::Error, 0..0)
.with_config(no_color_and_ascii())
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..15).with_message("This is a strange comparison"))
Expand Down

0 comments on commit 80c140d

Please sign in to comment.