From 17aa04b7124657aae83a1a4c0e842519b85c0002 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 2 Sep 2017 10:09:59 +0200 Subject: [PATCH] Remove uses of `format!` in components/style/error_reporting.rs --- .../script_layout_interface/reporter.rs | 2 +- components/style/error_reporting.rs | 141 +++++++++--------- tests/unit/style/rule_tree/bench.rs | 2 +- tests/unit/style/stylesheets.rs | 2 +- 4 files changed, 75 insertions(+), 72 deletions(-) diff --git a/components/script_layout_interface/reporter.rs b/components/script_layout_interface/reporter.rs index ac5706f5116..7f5acfef8c1 100644 --- a/components/script_layout_interface/reporter.rs +++ b/components/script_layout_interface/reporter.rs @@ -31,7 +31,7 @@ impl ParseErrorReporter for CSSErrorReporter { url.as_str(), location.line, location.column, - error.to_string()) + error) } //TODO: report a real filename diff --git a/components/style/error_reporting.rs b/components/style/error_reporting.rs index d875e5a645b..bfc9df17762 100644 --- a/components/style/error_reporting.rs +++ b/components/style/error_reporting.rs @@ -9,6 +9,7 @@ use cssparser::{BasicParseError, Token, SourceLocation}; use cssparser::ParseError as CssParseError; use log; +use std::fmt; use style_traits::ParseError; use stylesheets::UrlExtraData; @@ -46,124 +47,126 @@ pub enum ContextualParseError<'a> { InvalidCounterStyleExtendsWithAdditiveSymbols } -impl<'a> ContextualParseError<'a> { - /// Turn a parse error into a string representation. - pub fn to_string(&self) -> String { - fn token_to_str(t: &Token) -> String { +impl<'a> fmt::Display for ContextualParseError<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn token_to_str(t: &Token, f: &mut fmt::Formatter) -> fmt::Result { match *t { - Token::Ident(ref i) => format!("identifier {}", i), - Token::AtKeyword(ref kw) => format!("keyword @{}", kw), - Token::Hash(ref h) => format!("hash #{}", h), - Token::IDHash(ref h) => format!("id selector #{}", h), - Token::QuotedString(ref s) => format!("quoted string \"{}\"", s), - Token::UnquotedUrl(ref u) => format!("url {}", u), - Token::Delim(ref d) => format!("delimiter {}", d), - Token::Number { int_value: Some(i), .. } => format!("number {}", i), - Token::Number { value, .. } => format!("number {}", value), - Token::Percentage { int_value: Some(i), .. } => format!("percentage {}", i), - Token::Percentage { unit_value, .. } => format!("percentage {}", unit_value * 100.), - Token::Dimension { value, ref unit, .. } => format!("dimension {}{}", value, unit), - Token::WhiteSpace(_) => format!("whitespace"), - Token::Comment(_) => format!("comment"), - Token::Colon => format!("colon (:)"), - Token::Semicolon => format!("semicolon (;)"), - Token::Comma => format!("comma (,)"), - Token::IncludeMatch => format!("include match (~=)"), - Token::DashMatch => format!("dash match (|=)"), - Token::PrefixMatch => format!("prefix match (^=)"), - Token::SuffixMatch => format!("suffix match ($=)"), - Token::SubstringMatch => format!("substring match (*=)"), - Token::Column => format!("column (||)"), - Token::CDO => format!("CDO ()"), - Token::Function(ref f) => format!("function {}", f), - Token::ParenthesisBlock => format!("parenthesis ("), - Token::SquareBracketBlock => format!("square bracket ["), - Token::CurlyBracketBlock => format!("curly bracket {{"), - Token::BadUrl(ref _u) => format!("bad url parse error"), - Token::BadString(ref _s) => format!("bad string parse error"), - Token::CloseParenthesis => format!("unmatched close parenthesis"), - Token::CloseSquareBracket => format!("unmatched close square bracket"), - Token::CloseCurlyBracket => format!("unmatched close curly bracket"), + Token::Ident(ref i) => write!(f, "identifier {}", i), + Token::AtKeyword(ref kw) => write!(f, "keyword @{}", kw), + Token::Hash(ref h) => write!(f, "hash #{}", h), + Token::IDHash(ref h) => write!(f, "id selector #{}", h), + Token::QuotedString(ref s) => write!(f, "quoted string \"{}\"", s), + Token::UnquotedUrl(ref u) => write!(f, "url {}", u), + Token::Delim(ref d) => write!(f, "delimiter {}", d), + Token::Number { int_value: Some(i), .. } => write!(f, "number {}", i), + Token::Number { value, .. } => write!(f, "number {}", value), + Token::Percentage { int_value: Some(i), .. } => write!(f, "percentage {}", i), + Token::Percentage { unit_value, .. } => write!(f, "percentage {}", unit_value * 100.), + Token::Dimension { value, ref unit, .. } => write!(f, "dimension {}{}", value, unit), + Token::WhiteSpace(_) => write!(f, "whitespace"), + Token::Comment(_) => write!(f, "comment"), + Token::Colon => write!(f, "colon (:)"), + Token::Semicolon => write!(f, "semicolon (;)"), + Token::Comma => write!(f, "comma (,)"), + Token::IncludeMatch => write!(f, "include match (~=)"), + Token::DashMatch => write!(f, "dash match (|=)"), + Token::PrefixMatch => write!(f, "prefix match (^=)"), + Token::SuffixMatch => write!(f, "suffix match ($=)"), + Token::SubstringMatch => write!(f, "substring match (*=)"), + Token::Column => write!(f, "column (||)"), + Token::CDO => write!(f, "CDO ()"), + Token::Function(ref name) => write!(f, "function {}", name), + Token::ParenthesisBlock => write!(f, "parenthesis ("), + Token::SquareBracketBlock => write!(f, "square bracket ["), + Token::CurlyBracketBlock => write!(f, "curly bracket {{"), + Token::BadUrl(ref _u) => write!(f, "bad url parse error"), + Token::BadString(ref _s) => write!(f, "bad string parse error"), + Token::CloseParenthesis => write!(f, "unmatched close parenthesis"), + Token::CloseSquareBracket => write!(f, "unmatched close square bracket"), + Token::CloseCurlyBracket => write!(f, "unmatched close curly bracket"), } } - fn parse_error_to_str(err: &ParseError) -> String { + fn parse_error_to_str(err: &ParseError, f: &mut fmt::Formatter) -> fmt::Result { match *err { CssParseError::Basic(BasicParseError::UnexpectedToken(ref t)) => { - format!("found unexpected {}", token_to_str(t)) + write!(f, "found unexpected ")?; + token_to_str(t, f) } CssParseError::Basic(BasicParseError::EndOfInput) => { - format!("unexpected end of input") + write!(f, "unexpected end of input") } CssParseError::Basic(BasicParseError::AtRuleInvalid(ref i)) => { - format!("@ rule invalid: {}", i) + write!(f, "@ rule invalid: {}", i) } CssParseError::Basic(BasicParseError::AtRuleBodyInvalid) => { - format!("@ rule invalid") + write!(f, "@ rule invalid") } CssParseError::Basic(BasicParseError::QualifiedRuleInvalid) => { - format!("qualified rule invalid") + write!(f, "qualified rule invalid") } CssParseError::Custom(ref err) => { - format!("{:?}", err) + write!(f, "{:?}", err) } } } match *self { ContextualParseError::UnsupportedPropertyDeclaration(decl, ref err) => { - format!("Unsupported property declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported property declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedFontFaceDescriptor(decl, ref err) => { - format!("Unsupported @font-face descriptor declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported @font-face descriptor declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedFontFeatureValuesDescriptor(decl, ref err) => { - format!("Unsupported @font-feature-values descriptor declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported @font-feature-values descriptor declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::InvalidKeyframeRule(rule, ref err) => { - format!("Invalid keyframe rule: '{}', {}", rule, - parse_error_to_str(err)) + write!(f, "Invalid keyframe rule: '{}', ", rule)?; + parse_error_to_str(err, f) } ContextualParseError::InvalidFontFeatureValuesRule(rule, ref err) => { - format!("Invalid font feature value rule: '{}', {}", rule, - parse_error_to_str(err)) + write!(f, "Invalid font feature value rule: '{}', ", rule)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedKeyframePropertyDeclaration(decl, ref err) => { - format!("Unsupported keyframe property declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported keyframe property declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::InvalidRule(rule, ref err) => { - format!("Invalid rule: '{}', {}", rule, parse_error_to_str(err)) + write!(f, "Invalid rule: '{}', ", rule)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedRule(rule, ref err) => { - format!("Unsupported rule: '{}', {}", rule, parse_error_to_str(err)) + write!(f, "Unsupported rule: '{}', ", rule)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedViewportDescriptorDeclaration(decl, ref err) => { - format!("Unsupported @viewport descriptor declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported @viewport descriptor declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(decl, ref err) => { - format!("Unsupported @counter-style descriptor declaration: '{}', {}", decl, - parse_error_to_str(err)) + write!(f, "Unsupported @counter-style descriptor declaration: '{}', ", decl)?; + parse_error_to_str(err, f) } ContextualParseError::InvalidCounterStyleWithoutSymbols(ref system) => { - format!("Invalid @counter-style rule: 'system: {}' without 'symbols'", system) + write!(f, "Invalid @counter-style rule: 'system: {}' without 'symbols'", system) } ContextualParseError::InvalidCounterStyleNotEnoughSymbols(ref system) => { - format!("Invalid @counter-style rule: 'system: {}' less than two 'symbols'", system) + write!(f, "Invalid @counter-style rule: 'system: {}' less than two 'symbols'", system) } ContextualParseError::InvalidCounterStyleWithoutAdditiveSymbols => { - "Invalid @counter-style rule: 'system: additive' without 'additive-symbols'".into() + write!(f, "Invalid @counter-style rule: 'system: additive' without 'additive-symbols'") } ContextualParseError::InvalidCounterStyleExtendsWithSymbols => { - "Invalid @counter-style rule: 'system: extends …' with 'symbols'".into() + write!(f, "Invalid @counter-style rule: 'system: extends …' with 'symbols'") } ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols => { - "Invalid @counter-style rule: 'system: extends …' with 'additive-symbols'".into() + write!(f, "Invalid @counter-style rule: 'system: extends …' with 'additive-symbols'") } } } @@ -195,7 +198,7 @@ impl ParseErrorReporter for RustLogReporter { location: SourceLocation, error: ContextualParseError) { if log_enabled!(log::LogLevel::Info) { - info!("Url:\t{}\n{}:{} {}", url.as_str(), location.line, location.column, error.to_string()) + info!("Url:\t{}\n{}:{} {}", url.as_str(), location.line, location.column, error) } } } diff --git a/tests/unit/style/rule_tree/bench.rs b/tests/unit/style/rule_tree/bench.rs index 156a2e32b27..35abf6119fe 100644 --- a/tests/unit/style/rule_tree/bench.rs +++ b/tests/unit/style/rule_tree/bench.rs @@ -22,7 +22,7 @@ impl ParseErrorReporter for ErrorringErrorReporter { url: &ServoUrl, location: SourceLocation, error: ContextualParseError) { - panic!("CSS error: {}\t\n{}:{} {}", url.as_str(), location.line, location.column, error.to_string()); + panic!("CSS error: {}\t\n{}:{} {}", url.as_str(), location.line, location.column, error); } } diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs index 69063931a5e..8d1550bbeff 100644 --- a/tests/unit/style/stylesheets.rs +++ b/tests/unit/style/stylesheets.rs @@ -280,7 +280,7 @@ impl ParseErrorReporter for CSSInvalidErrorReporterTest { url: url.clone(), line: location.line, column: location.column, - message: error.to_string() + message: error.to_string(), } ); }