mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +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(),
|
url.as_str(),
|
||||||
location.line,
|
location.line,
|
||||||
location.column,
|
location.column,
|
||||||
error.to_string())
|
error)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: report a real filename
|
//TODO: report a real filename
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
use cssparser::{BasicParseError, Token, SourceLocation};
|
use cssparser::{BasicParseError, Token, SourceLocation};
|
||||||
use cssparser::ParseError as CssParseError;
|
use cssparser::ParseError as CssParseError;
|
||||||
use log;
|
use log;
|
||||||
|
use std::fmt;
|
||||||
use style_traits::ParseError;
|
use style_traits::ParseError;
|
||||||
use stylesheets::UrlExtraData;
|
use stylesheets::UrlExtraData;
|
||||||
|
|
||||||
|
@ -46,124 +47,126 @@ pub enum ContextualParseError<'a> {
|
||||||
InvalidCounterStyleExtendsWithAdditiveSymbols
|
InvalidCounterStyleExtendsWithAdditiveSymbols
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ContextualParseError<'a> {
|
impl<'a> fmt::Display for ContextualParseError<'a> {
|
||||||
/// Turn a parse error into a string representation.
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
pub fn to_string(&self) -> String {
|
fn token_to_str(t: &Token, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fn token_to_str(t: &Token) -> String {
|
|
||||||
match *t {
|
match *t {
|
||||||
Token::Ident(ref i) => format!("identifier {}", i),
|
Token::Ident(ref i) => write!(f, "identifier {}", i),
|
||||||
Token::AtKeyword(ref kw) => format!("keyword @{}", kw),
|
Token::AtKeyword(ref kw) => write!(f, "keyword @{}", kw),
|
||||||
Token::Hash(ref h) => format!("hash #{}", h),
|
Token::Hash(ref h) => write!(f, "hash #{}", h),
|
||||||
Token::IDHash(ref h) => format!("id selector #{}", h),
|
Token::IDHash(ref h) => write!(f, "id selector #{}", h),
|
||||||
Token::QuotedString(ref s) => format!("quoted string \"{}\"", s),
|
Token::QuotedString(ref s) => write!(f, "quoted string \"{}\"", s),
|
||||||
Token::UnquotedUrl(ref u) => format!("url {}", u),
|
Token::UnquotedUrl(ref u) => write!(f, "url {}", u),
|
||||||
Token::Delim(ref d) => format!("delimiter {}", d),
|
Token::Delim(ref d) => write!(f, "delimiter {}", d),
|
||||||
Token::Number { int_value: Some(i), .. } => format!("number {}", i),
|
Token::Number { int_value: Some(i), .. } => write!(f, "number {}", i),
|
||||||
Token::Number { value, .. } => format!("number {}", value),
|
Token::Number { value, .. } => write!(f, "number {}", value),
|
||||||
Token::Percentage { int_value: Some(i), .. } => format!("percentage {}", i),
|
Token::Percentage { int_value: Some(i), .. } => write!(f, "percentage {}", i),
|
||||||
Token::Percentage { unit_value, .. } => format!("percentage {}", unit_value * 100.),
|
Token::Percentage { unit_value, .. } => write!(f, "percentage {}", unit_value * 100.),
|
||||||
Token::Dimension { value, ref unit, .. } => format!("dimension {}{}", value, unit),
|
Token::Dimension { value, ref unit, .. } => write!(f, "dimension {}{}", value, unit),
|
||||||
Token::WhiteSpace(_) => format!("whitespace"),
|
Token::WhiteSpace(_) => write!(f, "whitespace"),
|
||||||
Token::Comment(_) => format!("comment"),
|
Token::Comment(_) => write!(f, "comment"),
|
||||||
Token::Colon => format!("colon (:)"),
|
Token::Colon => write!(f, "colon (:)"),
|
||||||
Token::Semicolon => format!("semicolon (;)"),
|
Token::Semicolon => write!(f, "semicolon (;)"),
|
||||||
Token::Comma => format!("comma (,)"),
|
Token::Comma => write!(f, "comma (,)"),
|
||||||
Token::IncludeMatch => format!("include match (~=)"),
|
Token::IncludeMatch => write!(f, "include match (~=)"),
|
||||||
Token::DashMatch => format!("dash match (|=)"),
|
Token::DashMatch => write!(f, "dash match (|=)"),
|
||||||
Token::PrefixMatch => format!("prefix match (^=)"),
|
Token::PrefixMatch => write!(f, "prefix match (^=)"),
|
||||||
Token::SuffixMatch => format!("suffix match ($=)"),
|
Token::SuffixMatch => write!(f, "suffix match ($=)"),
|
||||||
Token::SubstringMatch => format!("substring match (*=)"),
|
Token::SubstringMatch => write!(f, "substring match (*=)"),
|
||||||
Token::Column => format!("column (||)"),
|
Token::Column => write!(f, "column (||)"),
|
||||||
Token::CDO => format!("CDO (<!--)"),
|
Token::CDO => write!(f, "CDO (<!--)"),
|
||||||
Token::CDC => format!("CDC (-->)"),
|
Token::CDC => write!(f, "CDC (-->)"),
|
||||||
Token::Function(ref f) => format!("function {}", f),
|
Token::Function(ref name) => write!(f, "function {}", name),
|
||||||
Token::ParenthesisBlock => format!("parenthesis ("),
|
Token::ParenthesisBlock => write!(f, "parenthesis ("),
|
||||||
Token::SquareBracketBlock => format!("square bracket ["),
|
Token::SquareBracketBlock => write!(f, "square bracket ["),
|
||||||
Token::CurlyBracketBlock => format!("curly bracket {{"),
|
Token::CurlyBracketBlock => write!(f, "curly bracket {{"),
|
||||||
Token::BadUrl(ref _u) => format!("bad url parse error"),
|
Token::BadUrl(ref _u) => write!(f, "bad url parse error"),
|
||||||
Token::BadString(ref _s) => format!("bad string parse error"),
|
Token::BadString(ref _s) => write!(f, "bad string parse error"),
|
||||||
Token::CloseParenthesis => format!("unmatched close parenthesis"),
|
Token::CloseParenthesis => write!(f, "unmatched close parenthesis"),
|
||||||
Token::CloseSquareBracket => format!("unmatched close square bracket"),
|
Token::CloseSquareBracket => write!(f, "unmatched close square bracket"),
|
||||||
Token::CloseCurlyBracket => format!("unmatched close curly 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 {
|
match *err {
|
||||||
CssParseError::Basic(BasicParseError::UnexpectedToken(ref t)) => {
|
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) => {
|
CssParseError::Basic(BasicParseError::EndOfInput) => {
|
||||||
format!("unexpected end of input")
|
write!(f, "unexpected end of input")
|
||||||
}
|
}
|
||||||
CssParseError::Basic(BasicParseError::AtRuleInvalid(ref i)) => {
|
CssParseError::Basic(BasicParseError::AtRuleInvalid(ref i)) => {
|
||||||
format!("@ rule invalid: {}", i)
|
write!(f, "@ rule invalid: {}", i)
|
||||||
}
|
}
|
||||||
CssParseError::Basic(BasicParseError::AtRuleBodyInvalid) => {
|
CssParseError::Basic(BasicParseError::AtRuleBodyInvalid) => {
|
||||||
format!("@ rule invalid")
|
write!(f, "@ rule invalid")
|
||||||
}
|
}
|
||||||
CssParseError::Basic(BasicParseError::QualifiedRuleInvalid) => {
|
CssParseError::Basic(BasicParseError::QualifiedRuleInvalid) => {
|
||||||
format!("qualified rule invalid")
|
write!(f, "qualified rule invalid")
|
||||||
}
|
}
|
||||||
CssParseError::Custom(ref err) => {
|
CssParseError::Custom(ref err) => {
|
||||||
format!("{:?}", err)
|
write!(f, "{:?}", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match *self {
|
match *self {
|
||||||
ContextualParseError::UnsupportedPropertyDeclaration(decl, ref err) => {
|
ContextualParseError::UnsupportedPropertyDeclaration(decl, ref err) => {
|
||||||
format!("Unsupported property declaration: '{}', {}", decl,
|
write!(f, "Unsupported property declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::UnsupportedFontFaceDescriptor(decl, ref err) => {
|
ContextualParseError::UnsupportedFontFaceDescriptor(decl, ref err) => {
|
||||||
format!("Unsupported @font-face descriptor declaration: '{}', {}", decl,
|
write!(f, "Unsupported @font-face descriptor declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::UnsupportedFontFeatureValuesDescriptor(decl, ref err) => {
|
ContextualParseError::UnsupportedFontFeatureValuesDescriptor(decl, ref err) => {
|
||||||
format!("Unsupported @font-feature-values descriptor declaration: '{}', {}", decl,
|
write!(f, "Unsupported @font-feature-values descriptor declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::InvalidKeyframeRule(rule, ref err) => {
|
ContextualParseError::InvalidKeyframeRule(rule, ref err) => {
|
||||||
format!("Invalid keyframe rule: '{}', {}", rule,
|
write!(f, "Invalid keyframe rule: '{}', ", rule)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::InvalidFontFeatureValuesRule(rule, ref err) => {
|
ContextualParseError::InvalidFontFeatureValuesRule(rule, ref err) => {
|
||||||
format!("Invalid font feature value rule: '{}', {}", rule,
|
write!(f, "Invalid font feature value rule: '{}', ", rule)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::UnsupportedKeyframePropertyDeclaration(decl, ref err) => {
|
ContextualParseError::UnsupportedKeyframePropertyDeclaration(decl, ref err) => {
|
||||||
format!("Unsupported keyframe property declaration: '{}', {}", decl,
|
write!(f, "Unsupported keyframe property declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::InvalidRule(rule, ref err) => {
|
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) => {
|
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) => {
|
ContextualParseError::UnsupportedViewportDescriptorDeclaration(decl, ref err) => {
|
||||||
format!("Unsupported @viewport descriptor declaration: '{}', {}", decl,
|
write!(f, "Unsupported @viewport descriptor declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(decl, ref err) => {
|
ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(decl, ref err) => {
|
||||||
format!("Unsupported @counter-style descriptor declaration: '{}', {}", decl,
|
write!(f, "Unsupported @counter-style descriptor declaration: '{}', ", decl)?;
|
||||||
parse_error_to_str(err))
|
parse_error_to_str(err, f)
|
||||||
}
|
}
|
||||||
ContextualParseError::InvalidCounterStyleWithoutSymbols(ref system) => {
|
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) => {
|
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 => {
|
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 => {
|
ContextualParseError::InvalidCounterStyleExtendsWithSymbols => {
|
||||||
"Invalid @counter-style rule: 'system: extends …' with 'symbols'".into()
|
write!(f, "Invalid @counter-style rule: 'system: extends …' with 'symbols'")
|
||||||
}
|
}
|
||||||
ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols => {
|
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,
|
location: SourceLocation,
|
||||||
error: ContextualParseError) {
|
error: ContextualParseError) {
|
||||||
if log_enabled!(log::LogLevel::Info) {
|
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,
|
url: &ServoUrl,
|
||||||
location: SourceLocation,
|
location: SourceLocation,
|
||||||
error: ContextualParseError) {
|
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(),
|
url: url.clone(),
|
||||||
line: location.line,
|
line: location.line,
|
||||||
column: location.column,
|
column: location.column,
|
||||||
message: error.to_string()
|
message: error.to_string(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue