Use builder pattern instead of lots of arguments for EmitterWriter::new

This commit is contained in:
Oli Scherer 2023-07-25 13:25:38 +00:00
parent 29de70da1b
commit 0e7ec9683d
6 changed files with 24 additions and 115 deletions

View file

@ -62,30 +62,11 @@ impl HumanReadableErrorType {
pub fn new_emitter(
self,
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMap>>,
bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
teach: bool,
diagnostic_width: Option<usize>,
macro_backtrace: bool,
track_diagnostics: bool,
terminal_url: TerminalUrl,
) -> EmitterWriter {
let (short, color_config) = self.unzip();
let color = color_config.suggests_using_colors();
EmitterWriter::new(
dst,
source_map,
bundle,
fallback_bundle,
short,
teach,
color,
diagnostic_width,
macro_backtrace,
track_diagnostics,
terminal_url,
)
EmitterWriter::new(dst, fallback_bundle, color).short_message(short)
}
}
@ -668,6 +649,10 @@ pub struct FileWithAnnotatedLines {
impl EmitterWriter {
pub fn stderr(color_config: ColorConfig, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
let dst = Destination::from_stderr(color_config);
Self::create(dst, fallback_bundle)
}
fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
EmitterWriter {
dst,
sm: None,
@ -685,30 +670,10 @@ impl EmitterWriter {
pub fn new(
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMap>>,
fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
short_message: bool,
teach: bool,
colored: bool,
diagnostic_width: Option<usize>,
macro_backtrace: bool,
track_diagnostics: bool,
terminal_url: TerminalUrl,
) -> EmitterWriter {
EmitterWriter {
dst: Raw(dst, colored),
sm: source_map,
fluent_bundle,
fallback_bundle,
short_message,
teach,
ui_testing: false,
diagnostic_width,
macro_backtrace,
track_diagnostics,
terminal_url,
}
Self::create(Raw(dst, colored), fallback_bundle)
}
fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {

View file

@ -359,17 +359,13 @@ impl Diagnostic {
let buf = BufWriter::default();
let output = buf.clone();
je.json_rendered
.new_emitter(
Box::new(buf),
Some(je.sm.clone()),
je.fluent_bundle.clone(),
je.fallback_bundle.clone(),
false,
je.diagnostic_width,
je.macro_backtrace,
je.track_diagnostics,
je.terminal_url,
)
.new_emitter(Box::new(buf), je.fallback_bundle.clone())
.sm(Some(je.sm.clone()))
.fluent_bundle(je.fluent_bundle.clone())
.diagnostic_width(je.diagnostic_width)
.macro_backtrace(je.macro_backtrace)
.track_diagnostics(je.track_diagnostics)
.terminal_url(je.terminal_url)
.ui_testing(je.ui_testing)
.emit_diagnostic(diag);
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();