Skip to content

Commit b6738fc

Browse files
committed
Refactor to use wrapped printer
1 parent ec2c92d commit b6738fc

File tree

17 files changed

+103
-62
lines changed

17 files changed

+103
-62
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ mod parser;
1111
mod media_query;
1212
mod properties;
1313
mod values;
14+
mod printer;
1415

1516
use napi::{CallContext, JsObject, JsUndefined};
1617
use serde::{Deserialize, Serialize};
17-
use cssparser::{Parser, ParserInput, RuleListParser, ToCss};
18+
use cssparser::{Parser, ParserInput, RuleListParser};
19+
use values::traits::ToCss;
1820

1921
use parser::TopLevelRuleParser;
2022

src/parser.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::cell::RefCell;
55
use crate::media_query::*;
66
use crate::properties::*;
77
use crate::values::traits::PropertyHandler;
8+
use crate::printer::Printer;
9+
use crate::values::traits::ToCss;
810

911
#[derive(Debug, Clone)]
1012
pub struct Selectors;
@@ -18,7 +20,7 @@ impl CssString {
1820
}
1921
}
2022

21-
impl ToCss for CssString {
23+
impl cssparser::ToCss for CssString {
2224
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
2325
dest.write_str(self.0.borrow().as_ref())
2426
}
@@ -82,7 +84,7 @@ impl selectors::parser::NonTSPseudoClass for PseudoClass {
8284
}
8385
}
8486

85-
impl ToCss for PseudoClass {
87+
impl cssparser::ToCss for PseudoClass {
8688
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
8789
where
8890
W: fmt::Write,
@@ -94,13 +96,13 @@ impl ToCss for PseudoClass {
9496
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
9597
pub enum PseudoElement {}
9698

97-
impl ToCss for PseudoElement {
98-
fn to_css<W>(&self, _dest: &mut W) -> fmt::Result
99-
where
100-
W: fmt::Write,
101-
{
102-
match *self {}
103-
}
99+
impl cssparser::ToCss for PseudoElement {
100+
fn to_css<W>(&self, _dest: &mut W) -> fmt::Result
101+
where
102+
W: fmt::Write,
103+
{
104+
match *self {}
105+
}
104106
}
105107

106108
impl selectors::parser::PseudoElement for PseudoElement {
@@ -329,7 +331,7 @@ pub struct MediaRule {
329331
}
330332

331333
impl ToCss for MediaRule {
332-
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
334+
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
333335
dest.write_str("@media ")?;
334336
// serialize_string(&self.query, dest)?;
335337
// dest.write_str(";")
@@ -349,7 +351,7 @@ pub struct ImportRule {
349351
}
350352

351353
impl ToCss for ImportRule {
352-
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
354+
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
353355
dest.write_str("@import ")?;
354356
serialize_string(&self.url, dest)?;
355357
// dest.write_str(&self.media)?;
@@ -364,8 +366,9 @@ pub struct StyleRule {
364366
}
365367

366368
impl ToCss for StyleRule {
367-
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
369+
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
368370
// dest.write_str(&self.selectors)?;
371+
use crate::cssparser::ToCss;
369372
self.selectors.to_css(dest)?;
370373
dest.write_str(" {")?;
371374
for prop in self.declarations.iter() {
@@ -430,7 +433,7 @@ pub enum CssRule {
430433
}
431434

432435
impl ToCss for CssRule {
433-
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
436+
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
434437
match self {
435438
CssRule::Media(media) => media.to_css(dest),
436439
CssRule::Import(import) => import.to_css(dest),

src/printer.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::fmt::*;
2+
3+
pub struct Printer<'a, W> {
4+
dest: &'a mut W
5+
}
6+
7+
impl<'a, W: Write + Sized> Printer<'a, W> {
8+
pub fn new(dest: &mut W) -> Printer<W> {
9+
Printer { dest }
10+
}
11+
12+
pub fn write_str(&mut self, s: &str) -> Result {
13+
self.dest.write_str(s)
14+
}
15+
}
16+
17+
impl<'a, W: Write + Sized> Write for Printer<'a, W> {
18+
fn write_str(&mut self, s: &str) -> Result {
19+
self.dest.write_str(s)
20+
}
21+
}

src/properties/align.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::values::macros::*;
33
use crate::values::length::{LengthPercentageOrAuto, LengthPercentage, Length, Percentage};
44
use crate::values::traits::{Parse, ToCss, PropertyHandler};
55
use super::Property;
6+
use crate::printer::Printer;
67

78
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-baseline-position
89
#[derive(Debug, Clone, PartialEq)]
@@ -33,7 +34,7 @@ impl Parse for BaselinePosition {
3334
}
3435

3536
impl ToCss for BaselinePosition {
36-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
37+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
3738
match self {
3839
BaselinePosition::First => dest.write_str("baseline"),
3940
BaselinePosition::Last => dest.write_str("last baseline")
@@ -94,7 +95,7 @@ impl Parse for AlignContent {
9495
}
9596

9697
impl ToCss for AlignContent {
97-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
98+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
9899
match self {
99100
AlignContent::Normal => dest.write_str("normal"),
100101
AlignContent::BaselinePosition(val) => val.to_css(dest),
@@ -149,7 +150,7 @@ impl Parse for JustifyContent {
149150
}
150151

151152
impl ToCss for JustifyContent {
152-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
153+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
153154
match self {
154155
JustifyContent::Normal => dest.write_str("normal"),
155156
JustifyContent::ContentDistribution(val) => val.to_css(dest),
@@ -211,7 +212,7 @@ impl Parse for PlaceContent {
211212
}
212213

213214
impl ToCss for PlaceContent {
214-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
215+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
215216
self.align.to_css(dest)?;
216217
let is_equal = match self.justify {
217218
JustifyContent::Normal if self.align == AlignContent::Normal => true,
@@ -275,7 +276,7 @@ impl Parse for AlignSelf {
275276
}
276277

277278
impl ToCss for AlignSelf {
278-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
279+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
279280
match self {
280281
AlignSelf::Auto => dest.write_str("auto"),
281282
AlignSelf::Normal => dest.write_str("normal"),
@@ -341,7 +342,7 @@ impl Parse for JustifySelf {
341342
}
342343

343344
impl ToCss for JustifySelf {
344-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
345+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
345346
match self {
346347
JustifySelf::Auto => dest.write_str("auto"),
347348
JustifySelf::Normal => dest.write_str("normal"),
@@ -404,7 +405,7 @@ impl Parse for PlaceSelf {
404405
}
405406

406407
impl ToCss for PlaceSelf {
407-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
408+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
408409
self.align.to_css(dest)?;
409410
let is_equal = match &self.justify {
410411
JustifySelf::Auto => true,
@@ -454,7 +455,7 @@ impl Parse for AlignItems {
454455
}
455456

456457
impl ToCss for AlignItems {
457-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
458+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
458459
match self {
459460
AlignItems::Normal => dest.write_str("normal"),
460461
AlignItems::Stretch => dest.write_str("stretch"),
@@ -515,7 +516,7 @@ impl Parse for LegacyJustify {
515516
}
516517

517518
impl ToCss for LegacyJustify {
518-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
519+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
519520
dest.write_str("legacy ")?;
520521
match self {
521522
LegacyJustify::Left => dest.write_str("left"),
@@ -573,7 +574,7 @@ impl Parse for JustifyItems {
573574
}
574575

575576
impl ToCss for JustifyItems {
576-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
577+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
577578
match self {
578579
JustifyItems::Normal => dest.write_str("normal"),
579580
JustifyItems::Stretch => dest.write_str("stretch"),
@@ -635,7 +636,7 @@ impl Parse for PlaceItems {
635636
}
636637

637638
impl ToCss for PlaceItems {
638-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
639+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
639640
self.align.to_css(dest)?;
640641
let is_equal = match &self.justify {
641642
JustifyItems::Normal => self.align == AlignItems::Normal,
@@ -673,7 +674,7 @@ impl Parse for GapValue {
673674
}
674675

675676
impl ToCss for GapValue {
676-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
677+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
677678
match self {
678679
GapValue::Normal => dest.write_str("normal"),
679680
GapValue::LengthPercentage(lp) => lp.to_css(dest)
@@ -697,7 +698,7 @@ impl Parse for Gap {
697698
}
698699

699700
impl ToCss for Gap {
700-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
701+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
701702
self.row.to_css(dest)?;
702703
if self.column != self.row {
703704
dest.write_str(" ")?;

src/properties/background.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::values::macros::*;
55
use crate::values::{color::CssColor, image::Image};
66
use crate::properties::Property;
77
use itertools::izip;
8+
use crate::printer::Printer;
89

910
/// https://www.w3.org/TR/css-backgrounds-3/#background-position
1011
#[derive(Debug, Clone, PartialEq)]
@@ -108,7 +109,7 @@ impl Parse for BackgroundPosition {
108109
}
109110

110111
impl ToCss for BackgroundPosition {
111-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
112+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
112113
match (&self.x, &self.y) {
113114
(
114115
x_pos @ &HorizontalPosition::Side(_, Some(_)),
@@ -190,7 +191,7 @@ impl Parse for BackgroundSize {
190191
}
191192

192193
impl ToCss for BackgroundSize {
193-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
194+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
194195
use BackgroundSize::*;
195196

196197
match &self {
@@ -253,7 +254,7 @@ impl Parse for BackgroundRepeat {
253254
}
254255

255256
impl ToCss for BackgroundRepeat {
256-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
257+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
257258
use BackgroundRepeatKeyword::*;
258259
match (&self.x, &self.y) {
259260
(Repeat, NoRepeat) => dest.write_str("repeat-x"),
@@ -394,7 +395,7 @@ impl Parse for Background {
394395
}
395396

396397
impl ToCss for Background {
397-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
398+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
398399
let mut needs_space = false;
399400

400401
if self.color != CssColor::default() {

src/properties/flex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::values::macros::*;
33
use crate::values::length::{LengthPercentageOrAuto, LengthPercentage, Length, Percentage};
44
use crate::values::traits::{Parse, ToCss, PropertyHandler};
55
use super::Property;
6+
use crate::printer::Printer;
67

78
// https://www.w3.org/TR/2018/CR-css-flexbox-1-20181119/#propdef-flex-direction
89
enum_property!(FlexDirection,
@@ -66,7 +67,7 @@ impl Parse for FlexFlow {
6667
}
6768

6869
impl ToCss for FlexFlow {
69-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
70+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
7071
let mut needs_space = false;
7172
if self.direction != FlexDirection::default() || self.wrap == FlexWrap::default() {
7273
self.direction.to_css(dest)?;
@@ -133,7 +134,7 @@ impl Parse for Flex {
133134
}
134135

135136
impl ToCss for Flex {
136-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
137+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
137138
if self.grow == 0.0 && self.shrink == 0.0 && self.basis == LengthPercentageOrAuto::Auto {
138139
dest.write_str("none")?;
139140
return Ok(())

src/properties/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use flex::*;
1313
use align::*;
1414
use crate::values::{image::*, length::*, border::*, border_image::*, border_radius::*, rect::*, color::*};
1515
use super::values::traits::{Parse, ToCss};
16+
use crate::printer::Printer;
1617

1718
#[derive(Debug, Clone)]
1819
pub enum Property {
@@ -394,7 +395,7 @@ impl Property {
394395
return Ok(Property::Custom(CustomProperty::parse(name, input)?))
395396
}
396397

397-
pub fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
398+
pub fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
398399
use Property::*;
399400

400401
macro_rules! property {

src/properties/outline.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::values::border::{BorderStyle, GenericBorder, BorderSideWidth};
33
use crate::values::traits::{Parse, ToCss, PropertyHandler};
44
use crate::values::color::CssColor;
55
use super::Property;
6+
use crate::printer::Printer;
67

78
#[derive(Debug, Clone, PartialEq)]
89
pub enum OutlineStyle {
@@ -22,7 +23,7 @@ impl Parse for OutlineStyle {
2223
}
2324

2425
impl ToCss for OutlineStyle {
25-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
26+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
2627
match self {
2728
OutlineStyle::Auto => dest.write_str("auto"),
2829
OutlineStyle::BorderStyle(border_style) => border_style.to_css(dest)

src/values/border.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::rect::Rect;
77
use super::macros::*;
88
use super::border_image::*;
99
use super::border_radius::*;
10+
use crate::printer::Printer;
1011

1112
#[derive(Debug, Clone, PartialEq)]
1213
pub enum BorderSideWidth {
@@ -42,7 +43,7 @@ impl Parse for BorderSideWidth {
4243
}
4344

4445
impl ToCss for BorderSideWidth {
45-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
46+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
4647
use BorderSideWidth::*;
4748
match self {
4849
Thin => dest.write_str("thin"),
@@ -132,7 +133,7 @@ impl<S: Parse + Default> Parse for GenericBorder<S> {
132133
}
133134

134135
impl<S: ToCss + Default + PartialEq> ToCss for GenericBorder<S> {
135-
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result where W: std::fmt::Write {
136+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
136137
if self.width != BorderSideWidth::default() {
137138
self.width.to_css(dest)?;
138139
dest.write_str(" ")?;

0 commit comments

Comments
 (0)