Skip to content
Merged
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 compiler_base/error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compiler_base_error"
version = "0.0.8"
version = "0.0.9"
edition = "2021"
authors = ["zongzhe1024@163.com"]
license = "Apache-2.0 OR MIT"
Expand Down
75 changes: 73 additions & 2 deletions compiler_base/error/src/diagnostic/diagnostic_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! For more information about template loader, see doc in "compiler_base/error/src/diagnostic/diagnostic_message.rs".

use crate::{
diagnostic::diagnostic_message::TemplateLoader, Diagnostic, DiagnosticStyle, Emitter,
EmitterWriter,
diagnostic::diagnostic_message::TemplateLoader, emit_diagnostic_to_uncolored_text,
emitter::EmitResultText, Diagnostic, DiagnosticStyle, Emitter, EmitterWriter,
};
use anyhow::{bail, Context, Result};
use compiler_base_span::fatal_error::FatalError;
Expand Down Expand Up @@ -256,6 +256,62 @@ impl DiagnosticHandler {
}
}

/// Emit all the diagnostics into strings and return.
///
/// # Examples
///
/// ```rust
/// use compiler_base_error::DiagnosticStyle;
/// use compiler_base_error::diagnostic_handler::DiagnosticHandler;
/// use compiler_base_error::Diagnostic;
/// use compiler_base_error::components::Label;
///
/// let mut diag_1 = Diagnostic::<DiagnosticStyle>::new();
/// diag_1.append_component(Box::new(Label::Note));
///
/// let mut diag_handler = DiagnosticHandler::default();
///
/// assert_eq!(diag_handler.diagnostics_count().unwrap(), 0);
///
/// diag_handler.add_err_diagnostic(diag_1);
/// assert_eq!(diag_handler.diagnostics_count().unwrap(), 1);
/// assert_eq!(diag_handler.emit_all_diags_into_string().unwrap().get(0).unwrap().as_ref().unwrap(), "note");
/// ```
pub fn emit_all_diags_into_string(&mut self) -> Result<Vec<Result<String>>> {
match self.handler_inner.lock() {
Ok(inner) => Ok(inner.emit_all_diags_into_string()),
Err(_) => bail!("Emit Diagnostics Failed."),
}
}

/// Emit the [`index`]th diagnostics into strings and return.
///
/// # Examples
///
/// ```rust
/// use compiler_base_error::DiagnosticStyle;
/// use compiler_base_error::diagnostic_handler::DiagnosticHandler;
/// use compiler_base_error::Diagnostic;
/// use compiler_base_error::components::Label;
///
/// let mut diag_1 = Diagnostic::<DiagnosticStyle>::new();
/// diag_1.append_component(Box::new(Label::Note));
///
/// let mut diag_handler = DiagnosticHandler::default();
///
/// assert_eq!(diag_handler.diagnostics_count().unwrap(), 0);
///
/// diag_handler.add_err_diagnostic(diag_1);
/// assert_eq!(diag_handler.diagnostics_count().unwrap(), 1);
/// assert_eq!(diag_handler.emit_nth_diag_into_string(0).unwrap().unwrap().unwrap(), "note");
/// ```
pub fn emit_nth_diag_into_string(&mut self, index: usize) -> Result<Option<Result<String>>> {
match self.handler_inner.lock() {
Ok(inner) => Ok(inner.emit_nth_diag_into_string(index)),
Err(_) => bail!("Emit Diagnostics Failed."),
}
}

/// Emit the diagnostic messages generated from error to to terminal stderr.
///
/// # Examples
Expand Down Expand Up @@ -587,6 +643,21 @@ impl DiagnosticHandlerInner {
self.diagnostics.len()
}

/// Emit all the diagnostics into strings and return.
pub(crate) fn emit_all_diags_into_string(&self) -> Vec<Result<String>> {
self.diagnostics
.iter()
.map(|d| Ok(emit_diagnostic_to_uncolored_text(d)?))
.collect()
}

/// Emit the [`index`]th diagnostic into string and return.
pub(crate) fn emit_nth_diag_into_string(&self, index: usize) -> Option<Result<String>> {
self.diagnostics
.get(index)
.map(|d| emit_diagnostic_to_uncolored_text(d))
}

/// Emit the diagnostic messages generated from error to to terminal stderr.
pub(crate) fn emit_error_diagnostic(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion compiler_base/error/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ pub fn emit_diagnostic_to_uncolored_text(diag: &Diagnostic<DiagnosticStyle>) ->

/// Used to save the result of emit into a [`String`],
/// because trait [`Write`] and [`Send`] cannot be directly implemented by [`String`].
struct EmitResultText {
pub(crate) struct EmitResultText {
test_res: String,
}

Expand Down
5 changes: 3 additions & 2 deletions compiler_base/error/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ mod test_errors {

mod test_emitter {
use crate::{
components::Label, emit_diagnostic_to_uncolored_text, emitter::Destination, Diagnostic,
Emitter, EmitterWriter,
components::Label, diagnostic_handler::DiagnosticHandler,
emit_diagnostic_to_uncolored_text, emitter::Destination, Diagnostic, Emitter,
EmitterWriter,
};
use std::io::{self, Write};
use termcolor::Ansi;
Expand Down