Skip to content

Commit

Permalink
Added API utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Feb 26, 2022
1 parent 689782a commit 0cf04bf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
47 changes: 36 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<S> Label<S> {
/// A type representing a diagnostic that is ready to be written to output.
pub struct Report<S: Span = Range<usize>> {
kind: ReportKind,
code: Option<u32>,
code: Option<String>,
msg: Option<String>,
note: Option<String>,
help: Option<String>,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl fmt::Display for ReportKind {
/// A type used to build a [`Report`].
pub struct ReportBuilder<S: Span> {
kind: ReportKind,
code: Option<u32>,
code: Option<String>,
msg: Option<String>,
note: Option<String>,
help: Option<String>,
Expand All @@ -212,32 +212,57 @@ pub struct ReportBuilder<S: Span> {

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

/// Give this report a message.
/// Set the message of this report.
pub fn set_message<M: ToString>(&mut self, msg: M) {
self.msg = Some(msg.to_string());
}

/// Add a message to this report.
pub fn with_message<M: ToString>(mut self, msg: M) -> Self {
self.msg = Some(msg.to_string());
self
}

/// Give the diagnostic a final note.
pub fn with_note<N: ToString>(mut self, note: N) -> Self {
/// Set the note of this report.
pub fn set_note<N: ToString>(&mut self, note: N) {
self.note = Some(note.to_string());
}

/// Set the note of this report.
pub fn with_note<N: ToString>(mut self, note: N) -> Self {
self.set_note(note);
self
}

/// Give the diagnostic a help message.
pub fn with_help<N: ToString>(mut self, note: N) -> Self {
/// Set the help message of this report.
pub fn set_help<N: ToString>(&mut self, note: N) {
self.help = Some(note.to_string());
}

/// Set the help message of this report.
pub fn with_help<N: ToString>(mut self, note: N) -> Self {
self.set_help(note);
self
}

/// Add a new label to the diagnostic.
pub fn with_label(mut self, label: Label<S>) -> Self {
/// Add a label to the report.
pub fn add_label(&mut self, label: Label<S>) {
self.labels.push(label);
}

/// Add multiple labels to the report.
pub fn add_labels<L: IntoIterator<Item = Label<S>>>(&mut self, labels: L) {
self.labels.extend(labels);
}

/// Add a label to the report.
pub fn with_label(mut self, label: Label<S>) -> Self {
self.add_label(label);
self
}

Expand Down
6 changes: 3 additions & 3 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<S: Span> Report<S> {

// --- Header ---

let code = self.code.map(|c| format!("[{}{:02}] ", self.kind.letter(), c));
let code = self.code.as_ref().map(|c| format!("[{}] ", c));
let id = format!("{}{}:", Show(code), self.kind);
let kind_color = match self.kind {
ReportKind::Error => self.config.error_color(),
Expand Down Expand Up @@ -538,7 +538,7 @@ impl<S: Span> Report<S> {
write_margin(&mut w, 0, false, Some((0, false)), &[], &None)?;
write!(w, "{}: {}\n", "Help".fg(self.config.note_color()), note)?;
}

// Note
if let (Some(note), true) = (&self.note, is_final_group) {
if !self.config.compact {
Expand All @@ -548,7 +548,7 @@ impl<S: Span> Report<S> {
write_margin(&mut w, 0, false, Some((0, false)), &[], &None)?;
write!(w, "{}: {}\n", "Note".fg(self.config.note_color()), note)?;
}

// Tail of report
if !self.config.compact {
if is_final_group {
Expand Down

0 comments on commit 0cf04bf

Please sign in to comment.