Skip to content

Commit 759d0d9

Browse files
committed
Merge pull request #67 from servo/to_css-merged
Modernize the ToCss trait and implement it on more things.
2 parents 3ef0bff + d405f03 commit 759d0d9

File tree

6 files changed

+377
-202
lines changed

6 files changed

+377
-202
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ license = "MPL-2.0"
1414

1515
[dependencies]
1616
encoding = "0.2"
17+
text_writer = "0.1.1"

src/ast.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,6 @@ pub enum ComponentValue {
6969
}
7070

7171

72-
impl ComponentValue {
73-
pub fn to_css(&self) -> String {
74-
let mut css = String::new();
75-
self.to_css_push(&mut css);
76-
css
77-
}
78-
79-
pub fn to_css_push(&self, css: &mut String) {
80-
::serializer::to_css_push(self, css)
81-
}
82-
}
83-
84-
8572
#[deriving(PartialEq)]
8673
pub struct Declaration {
8774
pub location: SourceLocation,

src/color.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use std::ascii::AsciiExt;
66
use std::fmt;
77
use std::num::{Float, FloatMath};
88

9+
use text_writer::{mod, TextWriter};
10+
911
use ast::{ComponentValue, SkipWhitespaceIterable};
1012
use ast::ComponentValue::{Number, Percentage, Function, Ident, Hash, IDHash, Comma};
13+
use serializer::ToCss;
1114

1215

1316
#[deriving(Clone, Copy, PartialEq)]
@@ -20,14 +23,19 @@ pub struct RGBA {
2023
pub alpha: f32,
2124
}
2225

23-
impl fmt::Show for RGBA {
24-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
impl ToCss for RGBA {
27+
fn to_css<W>(&self, dest: &mut W) -> text_writer::Result where W: TextWriter {
2528
if self.alpha == 1f32 {
26-
write!(f, "rgb({}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(),
29+
write!(dest, "rgb({}, {}, {})",
30+
(self.red * 255.).round(),
31+
(self.green * 255.).round(),
2732
(self.blue * 255.).round())
2833
} else {
29-
write!(f, "rgba({}, {}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(),
30-
(self.blue * 255.).round(), self.alpha)
34+
write!(dest, "rgba({}, {}, {}, {})",
35+
(self.red * 255.).round(),
36+
(self.green * 255.).round(),
37+
(self.blue * 255.).round(),
38+
self.alpha)
3139
}
3240
}
3341
}
@@ -38,15 +46,23 @@ pub enum Color {
3846
RGBA(RGBA),
3947
}
4048

41-
impl fmt::Show for Color {
42-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49+
impl ToCss for Color {
50+
fn to_css<W>(&self, dest: &mut W) -> text_writer::Result where W: TextWriter {
4351
match self {
44-
&Color::CurrentColor => write!(f, "currentColor"),
45-
&Color::RGBA(c) => write!(f, "{}", c),
52+
&Color::CurrentColor => dest.write_str("currentColor"),
53+
&Color::RGBA(rgba) => rgba.to_css(dest),
4654
}
4755
}
4856
}
4957

58+
impl fmt::Show for RGBA {
59+
#[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_to_css(f) }
60+
}
61+
62+
impl fmt::Show for Color {
63+
#[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_to_css(f) }
64+
}
65+
5066
/// Return `Err(())` on invalid or unsupported value (not a color).
5167
impl Color {
5268
pub fn parse(component_value: &ComponentValue) -> Result<Color, ()> {

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
#![feature(globs, macro_rules)]
99

10-
extern crate encoding; // https://github.com/lifthrasiir/rust-encoding
10+
extern crate encoding;
11+
extern crate text_writer;
1112

1213
#[cfg(test)]
1314
extern crate test;
@@ -24,7 +25,7 @@ pub use parser::{parse_stylesheet_rules, StylesheetParser,
2425
pub use from_bytes::{decode_stylesheet_bytes, parse_stylesheet_rules_from_bytes};
2526
pub use color::{RGBA, Color};
2627
pub use nth::parse_nth;
27-
pub use serializer::{ToCss, serialize_identifier, serialize_string};
28+
pub use serializer::{ToCss, CssStringWriter, serialize_identifier, serialize_string};
2829

2930
pub mod ast;
3031
mod tokenizer;

0 commit comments

Comments
 (0)