Skip to content

Commit

Permalink
Format component of style_traits
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Sep 8, 2018
1 parent 81655a9 commit a2fc2ae
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 64 deletions.
59 changes: 36 additions & 23 deletions components/style_traits/lib.rs
Expand Up @@ -8,27 +8,35 @@

#![crate_name = "style_traits"]
#![crate_type = "rlib"]

#![deny(unsafe_code, missing_docs)]

extern crate app_units;
#[macro_use] extern crate bitflags;
#[macro_use] extern crate cssparser;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate cssparser;
extern crate euclid;
extern crate malloc_size_of;
#[macro_use] extern crate malloc_size_of_derive;
#[macro_use]
extern crate malloc_size_of_derive;
extern crate selectors;
#[cfg(feature = "servo")] #[macro_use] extern crate serde;
#[cfg(feature = "servo")] extern crate webrender_api;
#[cfg(feature = "servo")]
#[macro_use]
extern crate serde;
extern crate servo_arc;
#[cfg(feature = "servo")] extern crate servo_atoms;
#[cfg(feature = "servo")] extern crate servo_url;

#[cfg(feature = "servo")] pub use webrender_api::DevicePixel;
#[cfg(feature = "servo")]
extern crate servo_atoms;
#[cfg(feature = "servo")]
extern crate servo_url;
#[cfg(feature = "servo")]
extern crate webrender_api;
#[cfg(feature = "servo")]
pub use webrender_api::DevicePixel;

use cssparser::{CowRcStr, Token};
use selectors::parser::SelectorParseErrorKind;
#[cfg(feature = "servo")] use servo_atoms::Atom;
#[cfg(feature = "servo")]
use servo_atoms::Atom;

/// One hardware pixel.
///
Expand All @@ -40,7 +48,10 @@ pub enum DevicePixel {}
/// Represents a mobile style pinch zoom factor.
/// TODO(gw): Once WR supports pinch zoom, use a type directly from webrender_api.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize, MallocSizeOf))]
#[cfg_attr(
feature = "servo",
derive(Deserialize, Serialize, MallocSizeOf)
)]
pub struct PinchZoomFactor(f32);

impl PinchZoomFactor {
Expand Down Expand Up @@ -183,16 +194,14 @@ impl<'i> StyleParseErrorKind<'i> {
{
let name = name.into();
let variant = match value_error.kind {
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => {
match e {
ValueParseErrorKind::InvalidColor(token) => {
StyleParseErrorKind::InvalidColor(name, token)
}
ValueParseErrorKind::InvalidFilter(token) => {
StyleParseErrorKind::InvalidFilter(name, token)
}
}
}
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => match e {
ValueParseErrorKind::InvalidColor(token) => {
StyleParseErrorKind::InvalidColor(name, token)
},
ValueParseErrorKind::InvalidFilter(token) => {
StyleParseErrorKind::InvalidFilter(name, token)
},
},
_ => StyleParseErrorKind::OtherInvalidValue(name),
};
cssparser::ParseError {
Expand Down Expand Up @@ -236,5 +245,9 @@ impl ParsingMode {
/// Speculatively execute paint code in the worklet thread pool.
pub trait SpeculativePainter: Send + Sync {
/// <https://drafts.css-houdini.org/css-paint-api/#draw-a-paint-image>
fn speculatively_draw_a_paint_image(&self, properties: Vec<(Atom, String)>, arguments: Vec<String>);
fn speculatively_draw_a_paint_image(
&self,
properties: Vec<(Atom, String)>,
arguments: Vec<String>,
);
}
2 changes: 1 addition & 1 deletion components/style_traits/specified_value_info.rs
Expand Up @@ -110,7 +110,7 @@ macro_rules! impl_generic_specified_value_info {
$param::collect_completion_keywords(f);
}
}
}
};
}
impl_generic_specified_value_info!(Option<T>);
impl_generic_specified_value_info!(Vec<T>);
Expand Down
96 changes: 68 additions & 28 deletions components/style_traits/values.rs
Expand Up @@ -44,7 +44,9 @@ use std::fmt::{self, Write};
/// implement `Debug` by a single call to `ToCss::to_css`.
pub trait ToCss {
/// Serialize `self` in CSS syntax, writing to `dest`.
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write;
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write;

/// Serialize `self` in CSS syntax and return a string.
///
Expand All @@ -57,22 +59,34 @@ pub trait ToCss {
}
}

impl<'a, T> ToCss for &'a T where T: ToCss + ?Sized {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
impl<'a, T> ToCss for &'a T
where
T: ToCss + ?Sized,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
(*self).to_css(dest)
}
}

impl ToCss for str {
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
serialize_string(self, dest)
}
}

impl ToCss for String {
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
serialize_string(self, dest)
}
}
Expand All @@ -82,7 +96,10 @@ where
T: ToCss,
{
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
self.as_ref().map_or(Ok(()), |value| value.to_css(dest))
}
}
Expand All @@ -103,7 +120,10 @@ where
/// Creates a new `CssWriter`.
#[inline]
pub fn new(inner: &'w mut W) -> Self {
Self { inner, prefix: Some("") }
Self {
inner,
prefix: Some(""),
}
}
}

Expand Down Expand Up @@ -179,7 +199,7 @@ where
#[inline]
fn write_item<F>(&mut self, f: F) -> fmt::Result
where
F: FnOnce(&mut CssWriter<'b, W>) -> fmt::Result
F: FnOnce(&mut CssWriter<'b, W>) -> fmt::Result,
{
let old_prefix = self.inner.prefix;
if old_prefix.is_none() {
Expand All @@ -192,7 +212,7 @@ where
match (old_prefix, self.inner.prefix) {
(_, None) => {
// This call produced output and cleaned up after itself.
}
},
(None, Some(p)) => {
// Some previous call to `item` produced output,
// but this one did not, prefix should be the same as
Expand All @@ -201,12 +221,12 @@ where
// We clean up here even though it's not necessary just
// to be able to do all these assertion checks.
self.inner.prefix = None;
}
},
(Some(old), Some(new)) => {
// No previous call to `item` produced output, and this one
// either.
debug_assert_eq!(old, new);
}
},
}
Ok(())
}
Expand Down Expand Up @@ -282,7 +302,7 @@ impl Separator for Comma {
parse_one: F,
) -> Result<Vec<T>, ParseError<'i, E>>
where
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,
{
input.parse_comma_separated(parse_one)
}
Expand All @@ -298,16 +318,16 @@ impl Separator for Space {
mut parse_one: F,
) -> Result<Vec<T>, ParseError<'i, E>>
where
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,
{
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
let mut results = vec![parse_one(input)?];
loop {
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
if let Ok(item) = input.try(&mut parse_one) {
results.push(item);
} else {
return Ok(results)
return Ok(results);
}
}
}
Expand All @@ -323,15 +343,15 @@ impl Separator for CommaWithSpace {
mut parse_one: F,
) -> Result<Vec<T>, ParseError<'i, E>>
where
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>
F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,
{
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
let mut results = vec![parse_one(input)?];
loop {
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
let comma_location = input.current_source_location();
let comma = input.try(|i| i.expect_comma()).is_ok();
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
if let Ok(item) = input.try(&mut parse_one) {
results.push(item);
} else if comma {
Expand All @@ -355,8 +375,14 @@ impl OneOrMoreSeparated for UnicodeRange {
type S = Comma;
}

impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreSeparated {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
impl<T> ToCss for Vec<T>
where
T: ToCss + OneOrMoreSeparated,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
let mut iter = self.iter();
iter.next().unwrap().to_css(dest)?;
for item in iter {
Expand All @@ -367,24 +393,35 @@ impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreSeparated {
}
}

impl<T> ToCss for Box<T> where T: ?Sized + ToCss {
impl<T> ToCss for Box<T>
where
T: ?Sized + ToCss,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where W: Write,
where
W: Write,
{
(**self).to_css(dest)
}
}

impl<T> ToCss for Arc<T> where T: ?Sized + ToCss {
impl<T> ToCss for Arc<T>
where
T: ?Sized + ToCss,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where W: Write,
where
W: Write,
{
(**self).to_css(dest)
}
}

impl ToCss for Au {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
self.to_f64_px().to_css(dest)?;
dest.write_str("px")
}
Expand All @@ -393,7 +430,10 @@ impl ToCss for Au {
macro_rules! impl_to_css_for_predefined_type {
($name: ty) => {
impl<'a> ToCss for $name {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
::cssparser::ToCss::to_css(self, dest)
}
}
Expand Down

0 comments on commit a2fc2ae

Please sign in to comment.