Skip to content

Commit

Permalink
restructure writer css values to match as file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Aug 5, 2023
1 parent 63ca574 commit 4f3cb6a
Show file tree
Hide file tree
Showing 21 changed files with 760 additions and 685 deletions.
30 changes: 30 additions & 0 deletions crates/hdx_writer/src/css/values/backgrounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use hdx_ast::css::values::{backgrounds::*, Shorthand};

use crate::{CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for LineWidth {
fn write_css<W: CssWriter>(&self, _sink: &mut W) -> Result {
todo!()
}
}

impl<'a> WriteCss<'a> for BorderShorthand<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
if let Shorthand::Explicit(line_width) = &self.line_width {
line_width.write_css(sink)?;
if self.line_style.is_explicit() {
sink.write_char(' ')?;
}
}
if let Shorthand::Explicit(line_style) = &self.line_style {
line_style.write_css(sink)?;
if self.color.is_explicit() {
sink.write_char(' ')?;
}
}
if let Shorthand::Explicit(color) = &self.color {
color.write_css(sink)?;
}
Ok(())
}
}
9 changes: 9 additions & 0 deletions crates/hdx_writer/src/css/values/box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use hdx_ast::css::values::r#box::*;

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for MarginTrimValue {
fn write_css<W: CssWriter>(&self, _sink: &mut W) -> Result {
todo!()
}
}
13 changes: 13 additions & 0 deletions crates/hdx_writer/src/css/values/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use hdx_ast::css::values::color::*;

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for ColorValue<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Hex(_) => sink.write_str(&self.to_hex(ToHexStyle::Compact).unwrap()),
Self::Named(named) => sink.write_str(named.to_atom().as_ref()),
_ => todo!(),
}
}
}
27 changes: 27 additions & 0 deletions crates/hdx_writer/src/css/values/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ impl<'a> WriteCss<'a> for ContentsValue<'a> {
}
}

impl<'a> WriteCss<'a> for ContentList<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
let mut iter = self.values.iter().peekable();
while let Some(value) = iter.next() {
match value {
ContentElement::String(atom) => {
sink.write_char('"')?;
sink.write_str(atom.as_ref())?;
sink.write_char('"')?;
}
ContentElement::Contents => sink.write_str("contents")?,
ContentElement::Image(_) => todo!(),
ContentElement::Counter(_) => todo!(),
ContentElement::Quote(_) => todo!(),
ContentElement::Leader(_) => todo!(),
}
if iter.peek().is_some() {
sink.write_char(' ')?;
}
}
for alt in &self.alt {
todo!()
}
Ok(())
}
}

impl<'a> WriteCss<'a> for QuotesValue<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Expand Down
9 changes: 9 additions & 0 deletions crates/hdx_writer/src/css/values/counter_styles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use hdx_ast::css::values::counter_styles::*;

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for CounterStyle<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
todo!()
}
}
28 changes: 28 additions & 0 deletions crates/hdx_writer/src/css/values/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use hdx_ast::css::values::display::*;

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for DisplayValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
if let Some(atom) = self.to_atom() {
sink.write_str(atom.as_ref())?;
} else if let DisplayValue::Pair(outside, inside) = self {
if outside != &DisplayOutside::Implicit {
sink.write_str(outside.to_atom().unwrap().as_ref())?;
sink.write_char(' ')?;
}
sink.write_str(inside.to_atom().unwrap().as_ref())?;
} else if let DisplayValue::PairAndMarker(outside, inside, marker) = self {
if outside != &DisplayOutside::Implicit {
sink.write_str(outside.to_atom().unwrap().as_ref())?;
sink.write_char(' ')?;
}
if inside != &DisplayInside::Implicit {
sink.write_str(inside.to_atom().unwrap().as_ref())?;
sink.write_char(' ')?;
}
sink.write_str(marker.to_atom().unwrap().as_ref())?;
}
Ok(())
}
}
116 changes: 116 additions & 0 deletions crates/hdx_writer/src/css/values/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use std::ops::Deref;

use hdx_ast::css::values::expr::*;

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a, T: WriteCss<'a>> WriteCss<'a> for Expr<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::GlobalValue(g) => sink.write_str(g.to_atom().as_ref()),
Self::Literal(val) => val.write_css(sink),
Self::Reference(f) => f.write_css(sink),
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for MathExpr<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::GlobalValue(g) => sink.write_str(g.to_atom().as_ref()),
Self::Literal(val) => val.write_css(sink),
Self::Reference(f) => f.write_css(sink),
Self::Math(f) => f.write_css(sink),
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for ExprList<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::GlobalValue(g) => sink.write_str(g.to_atom().as_ref()),
Self::Values(v) => {
let mut values = v.iter().peekable();
while let Some(value) = values.next() {
value.write_css(sink)?;
if values.peek().is_some() {
sink.write_char(',')?;
sink.write_trivia_char(' ')?;
}
}
Ok(())
}
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for MathExprList<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::GlobalValue(g) => sink.write_str(g.to_atom().as_ref()),
Self::Values(v) => {
let mut values = v.iter().peekable();
while let Some(value) = values.next() {
value.write_css(sink)?;
if values.peek().is_some() {
sink.write_char(',')?;
sink.write_trivia_char(' ')?;
}
}
Ok(())
}
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for ExprListItem<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Literal(val) => val.write_css(sink),
Self::Reference(f) => f.write_css(sink),
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for MathExprListItem<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Literal(val) => val.write_css(sink),
Self::Reference(f) => f.write_css(sink),
Self::Math(f) => f.write_css(sink),
}
}
}

impl<'a, T: WriteCss<'a>> WriteCss<'a> for Reference<'a, T> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Var(atom, opt) => {
sink.write_str("var(")?;
sink.write_str(atom.as_ref())?;
if let Some(val) = opt.deref() {
sink.write_str(",")?;
sink.write_trivia_char(' ')?;
val.write_css(sink)?;
}
sink.write_str(")")
}
Self::Env(atom, opt) => {
sink.write_str("var(")?;
sink.write_str(atom.as_ref())?;
if let Some(val) = opt.deref() {
sink.write_str(",")?;
sink.write_trivia_char(' ')?;
val.write_css(sink)?;
}
sink.write_str(")")
}
}
}
}

impl<'a> WriteCss<'a> for MathFunc<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
todo!()
}
}
83 changes: 83 additions & 0 deletions crates/hdx_writer/src/css/values/fonts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use hdx_ast::css::values::{fonts::*, Angle, MathExpr};
use hdx_syntax::identifier::is_ident_str;

use crate::{CssWriter, Result, Spanned, WriteCss};

impl<'a> WriteCss<'a> for FontWeightValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Normal => sink.write_str("normal"),
Self::Bold => sink.write_str("bold"),
Self::Bolder => sink.write_str("bolder"),
Self::Lighter => sink.write_str("lighter"),
Self::Number(num) => sink.write_str(num.to_string().as_str()),
}
}
}

impl<'a> WriteCss<'a> for FontSizeValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Absolute(size) => size.write_css(sink),
Self::Relative(size) => size.write_css(sink),
Self::LengthPercentage(size) => size.write_css(sink),
Self::Math => sink.write_str("math"),
}
}
}

impl<'a> WriteCss<'a> for FontFamilyValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Named(atom) => {
let bare = atom.as_ref().split_ascii_whitespace().all(is_ident_str);
if !bare {
sink.write_char('"')?;
}
sink.write_str(atom.as_ref())?;
if !bare {
sink.write_char('"')?;
}
Ok(())
}
Self::Serif => sink.write_str("serif"),
Self::SansSerif => sink.write_str("sans-serif"),
Self::Cursive => sink.write_str("cursive"),
Self::Fantasy => sink.write_str("fantasy"),
Self::Monospace => sink.write_str("monospace"),
Self::SystemUi => sink.write_str("system-ui"),
Self::Emoji => sink.write_str("emoji"),
Self::Math => sink.write_str("math"),
Self::Fangsong => sink.write_str("fangsong"),
Self::UiSerif => sink.write_str("ui-serif"),
Self::UiSansSerif => sink.write_str("ui-sans-serif"),
Self::UiMonospace => sink.write_str("ui-monospace"),
Self::UiRounded => sink.write_str("ui-rounded"),
}
}
}

impl<'a> WriteCss<'a> for FontStyleValue<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Normal => sink.write_str("normal"),
Self::Italic => sink.write_str("italic"),
Self::Oblique(Spanned { node: angle, .. }) => {
sink.write_str("oblique")?;
match angle {
MathExpr::Literal(Spanned { node: Angle::Deg(deg), .. }) => {
if *deg != 14.0 {
sink.write_char(' ')?;
angle.write_css(sink)?;
}
}
_ => {
sink.write_char(' ')?;
angle.write_css(sink)?;
}
}
Ok(())
}
}
}
}
9 changes: 9 additions & 0 deletions crates/hdx_writer/src/css/values/images.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use hdx_ast::css::values::images::*;

use crate::{CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for Image<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
todo!()
}
}
47 changes: 47 additions & 0 deletions crates/hdx_writer/src/css/values/inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use hdx_ast::css::values::{inline::*, Shorthand};

use crate::{Atomizable, CssWriter, Result, WriteCss};

impl<'a> WriteCss<'a> for BaselineShiftValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Sub => sink.write_str("sub"),
Self::Super => sink.write_str("super"),
Self::Top => sink.write_str("top"),
Self::Center => sink.write_str("center"),
Self::Bottom => sink.write_str("bottom"),
Self::LengthPercentage(val) => val.write_css(sink),
}
}
}

impl<'a> WriteCss<'a> for VerticalAlignShorthand<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
if let Shorthand::Explicit(baseline_source) = &self.baseline_source {
baseline_source.write_css(sink)?;
if self.alignment_baseline.is_explicit() {
sink.write_char(' ')?;
}
}
if let Shorthand::Explicit(alignment_baseline) = &self.alignment_baseline {
alignment_baseline.write_css(sink)?;
if self.baseline_shift.is_explicit() {
sink.write_char(' ')?;
}
}
if let Shorthand::Explicit(baseline_shift) = &self.baseline_shift {
baseline_shift.write_css(sink)?;
}
Ok(())
}
}

impl<'a> WriteCss<'a> for LineHeightValue {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result {
match self {
Self::Normal => sink.write_str("normal"),
Self::Number(n) => sink.write_str(n.to_string().as_str()),
Self::LengthPercentage(n) => n.write_css(sink),
}
}
}
Loading

0 comments on commit 4f3cb6a

Please sign in to comment.