Skip to content

Commit

Permalink
Move Servo-specific ToCss to style_traits
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 6, 2016
1 parent 60e09ad commit 5dbc8d0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
1 change: 1 addition & 0 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 1 addition & 37 deletions components/style/values/mod.rs
Expand Up @@ -7,11 +7,7 @@
//! [values]: https://drafts.csswg.org/css-values/

pub use cssparser::RGBA;

use app_units::Au;
use cssparser::CssStringWriter;
use std::fmt::{self, Write};
use url::Url;
pub use style_traits::ToCss as LocalToCss;

macro_rules! define_numbered_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => {
Expand Down Expand Up @@ -48,38 +44,6 @@ macro_rules! define_numbered_css_keyword_enum {
pub mod computed;
pub mod specified;

/// The real ToCss trait can't be implemented for types in crates that don't
/// depend on each other.
pub trait LocalToCss {
/// Serialize `self` in CSS syntax, writing to `dest`.
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write;

/// Serialize `self` in CSS syntax and return a string.
///
/// (This is a convenience wrapper for `to_css` and probably should not be overridden.)
#[inline]
fn to_css_string(&self) -> String {
let mut s = String::new();
self.to_css(&mut s).unwrap();
s
}
}

impl LocalToCss for Au {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
write!(dest, "{}px", self.to_f64_px())
}
}

impl LocalToCss for Url {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("url(\""));
try!(write!(CssStringWriter::new(dest), "{}", self));
try!(dest.write_str("\")"));
Ok(())
}
}

pub type CSSFloat = f32;

pub const FONT_MEDIUM_PX: i32 = 16;
Expand Down
1 change: 1 addition & 0 deletions components/style_traits/Cargo.toml
Expand Up @@ -22,3 +22,4 @@ heapsize_derive = {version = "0.1", optional = true}
rustc-serialize = "0.3"
serde = {version = "0.8", optional = true}
serde_derive = {version = "0.8", optional = true}
url = "1.2"
2 changes: 2 additions & 0 deletions components/style_traits/lib.rs
Expand Up @@ -23,6 +23,7 @@ extern crate euclid;
extern crate rustc_serialize;
#[cfg(feature = "servo")] extern crate serde;
#[cfg(feature = "servo")] #[macro_use] extern crate serde_derive;
extern crate url;

/// Opaque type stored in type-unsafe work queues for parallel layout.
/// Must be transmutable to and from TNode.
Expand Down Expand Up @@ -61,3 +62,4 @@ pub mod cursor;
pub mod values;
pub mod viewport;

pub use values::ToCss;
48 changes: 47 additions & 1 deletion components/style_traits/values.rs
Expand Up @@ -2,6 +2,53 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use app_units::Au;
use cssparser::CssStringWriter;
use std::fmt::{self, Write};
use url::Url;

/// The real ToCss trait can't be implemented for types in crates that don't
/// depend on each other.
pub trait ToCss {
/// Serialize `self` in CSS syntax, writing to `dest`.
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write;

/// Serialize `self` in CSS syntax and return a string.
///
/// (This is a convenience wrapper for `to_css` and probably should not be overridden.)
#[inline]
fn to_css_string(&self) -> String {
let mut s = String::new();
self.to_css(&mut s).unwrap();
s
}
}

impl ToCss for Au {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
write!(dest, "{}px", self.to_f64_px())
}
}

impl ToCss for Url {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("url(\""));
try!(write!(CssStringWriter::new(dest), "{}", self));
try!(dest.write_str("\")"));
Ok(())
}
}

macro_rules! impl_to_css_for_predefined_type {
($name: ty) => {
impl<'a> ToCss for $name {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
::cssparser::ToCss::to_css(self, dest)
}
}
};
}

#[macro_export]
macro_rules! define_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident ),+,) => {
Expand Down Expand Up @@ -61,7 +108,6 @@ macro_rules! __define_css_keyword_enum__actual {
}
}


pub mod specified {
use app_units::Au;

Expand Down
1 change: 1 addition & 0 deletions ports/geckolib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5dbc8d0

Please sign in to comment.