Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Report/ReportKind lifetime #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let b = colors.next();
let out = Color::Fixed(81);

Report::build(ReportKind::Error, ("sample.tao", 12..12))
Report::build(ErrorKind, ("sample.tao", 12..12))
.with_code(3)
.with_message(format!("Incompatible types"))
.with_label(
Expand Down
4 changes: 2 additions & 2 deletions examples/multifile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ariadne::{sources, ColorGenerator, Fmt, Label, Report, ReportKind};
use ariadne::{sources, ColorGenerator, ErrorKind, Fmt, Label, Report};

fn main() {
let mut colors = ColorGenerator::new();
Expand All @@ -8,7 +8,7 @@ fn main() {
let b = colors.next();
let c = colors.next();

Report::build(ReportKind::Error, ("b.tao", 10..14))
Report::build(ErrorKind, ("b.tao", 10..14))
.with_code(3)
.with_message("Cannot add types Nat and Str".to_string())
.with_label(
Expand Down
4 changes: 2 additions & 2 deletions examples/multiline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ariadne::{Color, ColorGenerator, Fmt, Label, Report, ReportKind, Source};
use ariadne::{Color, ColorGenerator, ErrorKind, Fmt, Label, Report, Source};

fn main() {
let mut colors = ColorGenerator::new();
Expand All @@ -9,7 +9,7 @@ fn main() {
let out = Color::Fixed(81);
let out2 = colors.next();

Report::build(ReportKind::Error, ("sample.tao", 32..33))
Report::build(ErrorKind, ("sample.tao", 32..33))
.with_code(3)
.with_message("Incompatible types".to_string())
.with_label(
Expand Down
6 changes: 3 additions & 3 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};
use ariadne::{Color, Config, ErrorKind, Label, Report, Source};

fn main() {
Report::build(ReportKind::Error, 34..34)
Report::build(ErrorKind, 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, 2..3)
Report::build(ErrorKind, 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
4 changes: 2 additions & 2 deletions examples/stresstest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ariadne::{Color, ColorGenerator, Config, Label, Report, ReportKind, Source};
use ariadne::{Color, ColorGenerator, Config, ErrorKind, Label, Report, Source};

fn main() {
let mut colors = ColorGenerator::new();

Report::build(ReportKind::Error, ("stresstest.tao", 13..13))
Report::build(ErrorKind, ("stresstest.tao", 13..13))
.with_code(3)
.with_message("Incompatible types".to_string())
.with_label(
Expand Down
95 changes: 56 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use yansi::Color;
pub use crate::draw::StdoutFmt;

use crate::display::*;
use std::fmt::Debug;
use std::{
cmp::{Eq, PartialEq},
fmt,
Expand Down Expand Up @@ -198,8 +199,8 @@ impl<S: Span> Label<S> {
}

/// A type representing a diagnostic that is ready to be written to output.
pub struct Report<'a, S: Span = Range<usize>> {
kind: ReportKind<'a>,
pub struct Report<K, S: Span = Range<usize>> {
kind: K,
code: Option<String>,
msg: Option<String>,
notes: Vec<String>,
Expand All @@ -209,11 +210,11 @@ pub struct Report<'a, S: Span = Range<usize>> {
config: Config,
}

impl<S: Span> Report<'_, S> {
impl<K: ReportKind, S: Span> Report<K, S> {
/// Begin building a new [`Report`].
///
/// The span is the primary location at which the error should be reported.
pub fn build(kind: ReportKind, span: S) -> ReportBuilder<S> {
pub fn build(kind: K, span: S) -> ReportBuilder<K, S> {
ReportBuilder {
kind,
code: None,
Expand All @@ -240,7 +241,7 @@ impl<S: Span> Report<'_, S> {
}
}

impl<'a, S: Span> fmt::Debug for Report<'a, S> {
impl<K: Debug, S: Span> fmt::Debug for Report<K, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Report")
.field("kind", &self.kind)
Expand All @@ -252,35 +253,60 @@ impl<'a, S: Span> fmt::Debug for Report<'a, S> {
.finish()
}
}

/// A type that defines the kind of report being produced.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReportKind<'a> {
/// The report is an error and indicates a critical problem that prevents the program performing the requested
/// action.
Error,
/// The report is a warning and indicates a likely problem, but not to the extent that the requested action cannot
/// be performed.
Warning,
/// The report is advice to the user about a potential anti-pattern of other benign issues.
Advice,
/// The report is of a kind not built into Ariadne.
Custom(&'a str, Color),
pub trait ReportKind {
/// The name of the report kind. This will be displayed in the output.
fn name(&self) -> &str;

/// The color that should be used for reports of this kind.
fn color(&self) -> Color;
}

impl fmt::Display for ReportKind<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ReportKind::Error => write!(f, "Error"),
ReportKind::Warning => write!(f, "Warning"),
ReportKind::Advice => write!(f, "Advice"),
ReportKind::Custom(s, _) => write!(f, "{}", s),
}
/// The report is an error and indicates a critical problem that prevents the program performing the requested
/// action.
pub struct ErrorKind;

impl ReportKind for ErrorKind {
fn name(&self) -> &str {
"Error"
}

fn color(&self) -> Color {
Color::Red
}
}

/// The report is a warning and indicates a likely problem, but not to the extent that the requested action cannot
/// be performed.
pub struct WarningKind;

impl ReportKind for WarningKind {
fn name(&self) -> &str {
"Warning"
}

fn color(&self) -> Color {
Color::Yellow
}
}

/// The report is advice to the user about a potential anti-pattern of other benign issues.
pub struct AdviceKind;

impl ReportKind for AdviceKind {
fn name(&self) -> &str {
"Advice"
}

fn color(&self) -> Color {
Color::Fixed(147)
}
}

/// A type used to build a [`Report`].
pub struct ReportBuilder<'a, S: Span> {
kind: ReportKind<'a>,
pub struct ReportBuilder<K, S: Span> {
kind: K,
code: Option<String>,
msg: Option<String>,
notes: Vec<String>,
Expand All @@ -290,7 +316,7 @@ pub struct ReportBuilder<'a, S: Span> {
config: Config,
}

impl<'a, S: Span> ReportBuilder<'a, S> {
impl<K, S: Span> ReportBuilder<K, S> {
/// Give this report a numerical code that may be used to more precisely look up the error in documentation.
pub fn with_code<C: fmt::Display>(mut self, code: C) -> Self {
self.code = Some(format!("{:02}", code));
Expand Down Expand Up @@ -375,7 +401,7 @@ impl<'a, S: Span> ReportBuilder<'a, S> {
}

/// Finish building the [`Report`].
pub fn finish(self) -> Report<'a, S> {
pub fn finish(self) -> Report<K, S> {
Report {
kind: self.kind,
code: self.code,
Expand All @@ -389,7 +415,7 @@ impl<'a, S: Span> ReportBuilder<'a, S> {
}
}

impl<'a, S: Span> fmt::Debug for ReportBuilder<'a, S> {
impl<K: Debug, S: Span> fmt::Debug for ReportBuilder<K, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReportBuilder")
.field("kind", &self.kind)
Expand Down Expand Up @@ -512,15 +538,6 @@ impl Config {
self
}

fn error_color(&self) -> Option<Color> {
Some(Color::Red).filter(|_| self.color)
}
fn warning_color(&self) -> Option<Color> {
Some(Color::Yellow).filter(|_| self.color)
}
fn advice_color(&self) -> Option<Color> {
Some(Color::Fixed(147)).filter(|_| self.color)
}
fn margin_color(&self) -> Option<Color> {
Some(Color::Fixed(246)).filter(|_| self.color)
}
Expand Down
20 changes: 8 additions & 12 deletions src/source.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::*;

use std::io::Error;
use std::{
collections::{hash_map::Entry, HashMap},
fs,
path::{Path, PathBuf},
};
use std::io::Error;

/// A trait implemented by [`Source`] caches.
pub trait Cache<Id: ?Sized> {
Expand Down Expand Up @@ -332,9 +332,7 @@ impl Cache<Path> for FileCache {
Ok::<_, Error>(match self.files.entry(path.to_path_buf()) {
// TODO: Don't allocate here
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(Source::from(
fs::read_to_string(path)?,
)),
Entry::Vacant(entry) => entry.insert(Source::from(fs::read_to_string(path)?)),
})
}
fn display<'a>(&self, path: &'a Path) -> Option<impl fmt::Display + 'a> {
Expand Down Expand Up @@ -403,14 +401,12 @@ where
I: IntoIterator<Item = (Id, S)>,
S: AsRef<str>,
{
FnCache::new(
(move |id| Err(format!("Failed to fetch source '{}'", id))) as fn(&_) -> _,
)
.with_sources(
iter.into_iter()
.map(|(id, s)| (id, Source::from(s)))
.collect(),
)
FnCache::new((move |id| Err(format!("Failed to fetch source '{}'", id))) as fn(&_) -> _)
.with_sources(
iter.into_iter()
.map(|(id, s)| (id, Source::from(s)))
.collect(),
)
}

#[cfg(test)]
Expand Down
Loading