From e2371518c4b03e1770948d9d2429cfeb46e25a20 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 15 Dec 2015 16:42:05 +1300 Subject: [PATCH] A little more refactoring inside emitter.rs --- src/libsyntax/errors/emitter.rs | 80 +++++++++++++++------------------ 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/src/libsyntax/errors/emitter.rs b/src/libsyntax/errors/emitter.rs index cbb5970bd55..e65eab58d9a 100644 --- a/src/libsyntax/errors/emitter.rs +++ b/src/libsyntax/errors/emitter.rs @@ -39,6 +39,16 @@ pub enum ColorConfig { Never, } +impl ColorConfig { + fn use_color(&self) -> bool { + match *self { + ColorConfig::Always => true, + ColorConfig::Never => false, + ColorConfig::Auto => stderr_isatty(), + } + } +} + // A basic emitter for when we don't have access to a codemap or registry. Used // for reporting very early errors, etc. pub struct BasicEmitter { @@ -64,24 +74,12 @@ impl Emitter for BasicEmitter { } impl BasicEmitter { - // TODO refactor pub fn stderr(color_config: ColorConfig) -> BasicEmitter { - let stderr = io::stderr(); - - let use_color = match color_config { - ColorConfig::Always => true, - ColorConfig::Never => false, - ColorConfig::Auto => stderr_isatty(), - }; - - if use_color { - let dst = match term::stderr() { - Some(t) => Terminal(t), - None => Raw(Box::new(stderr)), - }; + if color_config.use_color() { + let dst = Destination::from_stderr(); BasicEmitter { dst: dst } } else { - BasicEmitter { dst: Raw(Box::new(stderr)) } + BasicEmitter { dst: Raw(Box::new(io::stderr())) } } } } @@ -139,22 +137,11 @@ impl EmitterWriter { registry: Option, code_map: Rc) -> EmitterWriter { - let stderr = io::stderr(); - - let use_color = match color_config { - ColorConfig::Always => true, - ColorConfig::Never => false, - ColorConfig::Auto => stderr_isatty(), - }; - - if use_color { - let dst = match term::stderr() { - Some(t) => Terminal(t), - None => Raw(Box::new(stderr)), - }; + if color_config.use_color() { + let dst = Destination::from_stderr(); EmitterWriter { dst: dst, registry: registry, cm: code_map } } else { - EmitterWriter { dst: Raw(Box::new(stderr)), registry: registry, cm: code_map } + EmitterWriter { dst: Raw(Box::new(io::stderr())), registry: registry, cm: code_map } } } @@ -476,22 +463,18 @@ impl EmitterWriter { loop { let span_name_span = self.cm.with_expn_info(span.expn_id, |expn_info| { - match expn_info { - Some(ei) => { - let (pre, post) = match ei.callee.format { - codemap::MacroAttribute(..) => ("#[", "]"), - codemap::MacroBang(..) => ("", "!"), - }; - let macro_decl_name = format!("in this expansion of {}{}{}", - pre, - ei.callee.name(), - post); - let def_site_span = ei.callee.span; - Some((ei.call_site, macro_decl_name, def_site_span)) - } - // TODO map - None => None, - } + expn_info.map(|ei| { + let (pre, post) = match ei.callee.format { + codemap::MacroAttribute(..) => ("#[", "]"), + codemap::MacroBang(..) => ("", "!"), + }; + let macro_decl_name = format!("in this expansion of {}{}{}", + pre, + ei.callee.name(), + post); + let def_site_span = ei.callee.span; + (ei.call_site, macro_decl_name, def_site_span) + }) }); let (macro_decl_name, def_site_span) = match span_name_span { None => break, @@ -573,6 +556,13 @@ enum Destination { } impl Destination { + fn from_stderr() -> Destination { + match term::stderr() { + Some(t) => Terminal(t), + None => Raw(Box::new(io::stderr())), + } + } + fn print_maybe_styled(&mut self, args: fmt::Arguments, color: term::Attr,