diff --git a/compiler_base/error/Cargo.toml b/compiler_base/error/Cargo.toml index e614b79f9..58f3764bf 100644 --- a/compiler_base/error/Cargo.toml +++ b/compiler_base/error/Cargo.toml @@ -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" diff --git a/compiler_base/error/src/diagnostic/diagnostic_handler.rs b/compiler_base/error/src/diagnostic/diagnostic_handler.rs index ae2b99bf6..805a6cb72 100644 --- a/compiler_base/error/src/diagnostic/diagnostic_handler.rs +++ b/compiler_base/error/src/diagnostic/diagnostic_handler.rs @@ -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; @@ -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::::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>> { + 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::::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>> { + 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 @@ -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> { + 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> { + 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, diff --git a/compiler_base/error/src/emitter.rs b/compiler_base/error/src/emitter.rs index 754f415c2..bcf336942 100644 --- a/compiler_base/error/src/emitter.rs +++ b/compiler_base/error/src/emitter.rs @@ -447,7 +447,7 @@ pub fn emit_diagnostic_to_uncolored_text(diag: &Diagnostic) -> /// 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, } diff --git a/compiler_base/error/src/tests.rs b/compiler_base/error/src/tests.rs index 60fa1fc9d..dfa1cd3cc 100644 --- a/compiler_base/error/src/tests.rs +++ b/compiler_base/error/src/tests.rs @@ -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;