mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Remove uses of format!
in components/style/error_reporting.rs
This commit is contained in:
parent
f6d20e6461
commit
17aa04b712
4 changed files with 75 additions and 72 deletions
|
@ -31,7 +31,7 @@ impl ParseErrorReporter for CSSErrorReporter {
|
|||
url.as_str(),
|
||||
location.line,
|
||||
location.column,
|
||||
error.to_string())
|
||||
error)
|
||||
}
|
||||
|
||||
//TODO: report a real filename
|
||||
|
|
|
@ -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::CDC => format!("CDC (-->)"),
|
||||
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::CDC => write!(f, "CDC (-->)"),
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue