codespan_reporting/
term.rs1use std::str::FromStr;
4use termcolor::{ColorChoice, WriteColor};
5
6use crate::diagnostic::Diagnostic;
7use crate::files::Files;
8
9mod config;
10mod renderer;
11mod views;
12
13pub use termcolor;
14
15pub use self::config::{Chars, Config, DisplayStyle, Styles};
16
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
48pub struct ColorArg(pub ColorChoice);
49
50impl ColorArg {
51 pub const VARIANTS: &'static [&'static str] = &["auto", "always", "ansi", "never"];
59}
60
61impl FromStr for ColorArg {
62 type Err = &'static str;
63
64 fn from_str(src: &str) -> Result<ColorArg, &'static str> {
65 match src {
66 _ if src.eq_ignore_ascii_case("auto") => Ok(ColorArg(ColorChoice::Auto)),
67 _ if src.eq_ignore_ascii_case("always") => Ok(ColorArg(ColorChoice::Always)),
68 _ if src.eq_ignore_ascii_case("ansi") => Ok(ColorArg(ColorChoice::AlwaysAnsi)),
69 _ if src.eq_ignore_ascii_case("never") => Ok(ColorArg(ColorChoice::Never)),
70 _ => Err("valid values: auto, always, ansi, never"),
71 }
72 }
73}
74
75impl Into<ColorChoice> for ColorArg {
76 fn into(self) -> ColorChoice {
77 self.0
78 }
79}
80
81pub fn emit<'files, F: Files<'files>>(
88 writer: &mut dyn WriteColor,
89 config: &Config,
90 files: &'files F,
91 diagnostic: &Diagnostic<F::FileId>,
92) -> Result<(), super::files::Error> {
93 use self::renderer::Renderer;
94 use self::views::{RichDiagnostic, ShortDiagnostic};
95
96 let mut renderer = Renderer::new(writer, config);
97 match config.display_style {
98 DisplayStyle::Rich => RichDiagnostic::new(diagnostic, config).render(files, &mut renderer),
99 DisplayStyle::Medium => ShortDiagnostic::new(diagnostic, true).render(files, &mut renderer),
100 DisplayStyle::Short => ShortDiagnostic::new(diagnostic, false).render(files, &mut renderer),
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 use crate::diagnostic::Label;
109 use crate::files::SimpleFiles;
110
111 #[test]
112 fn unsized_emit() {
113 let mut files = SimpleFiles::new();
114
115 let id = files.add("test", "");
116 let mut writer = termcolor::NoColor::new(Vec::<u8>::new());
117 let diagnostic = Diagnostic::bug().with_labels(vec![Label::primary(id, 0..0)]);
118
119 emit(&mut writer, &Config::default(), &files, &diagnostic).unwrap();
120 }
121}