| 1 |
//! Terminal back-end for emitting diagnostics. |
| 2 |
|
| 3 |
#[cfg(feature = "termcolor")] |
| 4 |
use termcolor::WriteColor; |
| 5 |
|
| 6 |
use crate::diagnostic::Diagnostic; |
| 7 |
use crate::files::Files; |
| 8 |
|
| 9 |
mod config; |
| 10 |
mod renderer; |
| 11 |
mod views; |
| 12 |
|
| 13 |
#[cfg(feature = "termcolor")] |
| 14 |
pub use termcolor; |
| 15 |
|
| 16 |
pub use self::config::{Chars, Config, DisplayStyle}; |
| 17 |
|
| 18 |
#[cfg(feature = "termcolor")] |
| 19 |
pub use self::config::Styles; |
| 20 |
|
| 21 |
/// Emit a diagnostic using the given writer, context, config, and files. |
| 22 |
/// |
| 23 |
/// The return value covers all error cases. These error case can arise if: |
| 24 |
/// * a file was removed from the file database. |
| 25 |
/// * a file was changed so that it is too small to have an index |
| 26 |
/// * IO fails |
| 27 |
pub fn emit<'files, F: Files<'files> + ?Sized>( |
| 28 |
#[cfg(feature = "termcolor")] writer: &mut dyn WriteColor, |
| 29 |
#[cfg(all(not(feature = "termcolor"), feature = "std"))] writer: &mut dyn std::io::Write, |
| 30 |
#[cfg(all(not(feature = "termcolor"), not(feature = "std")))] writer: &mut dyn core::fmt::Write, |
| 31 |
config: &Config, |
| 32 |
files: &'files F, |
| 33 |
diagnostic: &Diagnostic<F::FileId>, |
| 34 |
) -> Result<(), super::files::Error> { |
| 35 |
use self::renderer::Renderer; |
| 36 |
use self::views::{RichDiagnostic, ShortDiagnostic}; |
| 37 |
|
| 38 |
let mut renderer = Renderer::new(writer, config); |
| 39 |
match config.display_style { |
| 40 |
DisplayStyle::Rich => RichDiagnostic::new(diagnostic, config).render(files, &mut renderer), |
| 41 |
DisplayStyle::Medium => ShortDiagnostic::new(diagnostic, true).render(files, &mut renderer), |
| 42 |
DisplayStyle::Short => ShortDiagnostic::new(diagnostic, false).render(files, &mut renderer), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(all(test, feature = "termcolor"))] |
| 47 |
mod tests { |
| 48 |
use alloc::{vec, vec::Vec}; |
| 49 |
|
| 50 |
use super::*; |
| 51 |
|
| 52 |
use crate::diagnostic::Label; |
| 53 |
use crate::files::SimpleFiles; |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn unsized_emit() { |
| 57 |
let mut files = SimpleFiles::new(); |
| 58 |
|
| 59 |
let id = files.add("test", ""); |
| 60 |
let mut writer = termcolor::NoColor::new(Vec::<u8>::new()); |
| 61 |
let diagnostic = Diagnostic::bug().with_labels(vec![Label::primary(id, 0..0)]); |
| 62 |
|
| 63 |
emit(&mut writer, &Config::default(), &files, &diagnostic).unwrap(); |
| 64 |
} |
| 65 |
} |