Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start implementing <font-weight-absolute> #62

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions crates/hdx_ast/src/css/values/fonts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use impls::*;
// pub enum FontFamilyStyleValue<'a> {}

// // https://drafts.csswg.org/css-fonts-5/#font-weight
// #[value(" <font-weight-absolute> | bolder | lighter ")]
// #[initial("normal")]
// #[applies_to("all elements and text")]
// #[inherited("yes")]
// #[percentages("n/a")]
// #[canonical_order("per grammar")]
// #[animation_type("by computed value type")]
// pub enum FontWeightStyleValue {}
#[value(" <font-weight-absolute> | bolder | lighter ")]
#[initial("normal")]
#[applies_to("all elements and text")]
#[inherited("yes")]
#[percentages("n/a")]
#[canonical_order("per grammar")]
#[animation_type("by computed value type")]
pub enum FontWeightStyleValue {}

// https://drafts.csswg.org/css-fonts-5/#font-width
#[value(" normal | <percentage [0,∞]> | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded ")]
Expand Down
80 changes: 79 additions & 1 deletion crates/hdx_ast/src/css/values/fonts/types.rs
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
pub(crate) use crate::css::units::*;
use hdx_atom::atom;
use hdx_lexer::Cursor;
use hdx_parser::{diagnostics, Build, CursorStream, Parse, Parser, Peek, Result as ParserResult, ToCursors, T};

mod func {
use hdx_parser::custom_keyword;
Copy link
Owner

@keithamus keithamus Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is, by convention, mod kw.

But I'd probably get rid of all the keywords and just peek an ident.

custom_keyword!(Normal, atom!("normal"));
custom_keyword!(Bold, atom!("bold"));
custom_keyword!(Bolder, atom!("bolder"));
custom_keyword!(Lighter, atom!("lighter"));
}

// https://drafts.csswg.org/css-fonts-4/#propdef-font-weight
// <font-weight-absolute> = [normal | bold | <number [1,1000]>]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(rename_all = "kebab-case"))]
pub enum FontWeightAbsolute {
Normal(T![Ident]),
Bold(T![Ident]),
Bolder(T![Ident]),
Lighter(T![Ident]),
}

impl<'a> Peek<'a> for FontWeightAbsolute {
fn peek(p: &Parser<'a>) -> bool {
p.peek::<func::Normal>()
|| p.peek::<func::Bold>()
|| p.peek::<func::Bolder>()
|| p.peek::<func::Lighter>()
|| p.peek::<T![Number]>()
}
}

impl<'a> Parse<'a> for FontWeightAbsolute {
fn parse(p: &mut Parser<'a>) -> ParserResult<Self> {
let ident = p.parse::<T![Ident]>()?;
let c: Cursor = ident.into();
match p.parse_atom_lower(c) {
atom!("normal") => Ok(Self::Normal(<T![Ident]>::build(p, c))),
atom!("bold") => Ok(Self::Bold(<T![Ident]>::build(p, c))),
atom!("bolder") => Ok(Self::Bolder(<T![Ident]>::build(p, c))),
atom!("lighter") => Ok(Self::Lighter(<T![Ident]>::build(p, c))),
atom => Err(diagnostics::UnexpectedIdent(atom, c.into()))?,
}
Comment on lines +39 to +43
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should have a Number => Ok(...) arm here as well but wasn't sure how to do that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’ll need something like

if p.peek::<T![Ident]>() {} else if p.peek::<T![Number]>() {}

}
}

impl<'a> ToCursors<'a> for FontWeightAbsolute {
fn to_cursors(&self, s: &mut CursorStream<'a>) {
match self {
Self::Normal(c) => s.append(c.into()),
Self::Bold(c) => s.append(c.into()),
Self::Bolder(c) => s.append(c.into()),
Self::Lighter(c) => s.append(c.into()),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;

#[test]
fn size_test() {
assert_size!(FontWeightAbsolute, 56);
}

#[test]
fn test_writes() {
assert_parse!(FontWeightAbsolute, "normal");
assert_parse!(FontWeightAbsolute, "bold");
assert_parse!(FontWeightAbsolute, "bolder");
assert_parse!(FontWeightAbsolute, "lighter");
assert_parse!(FontWeightAbsolute, "100");
assert_parse!(FontWeightAbsolute, "500");
assert_parse!(FontWeightAbsolute, "900");
}
}
Comment on lines +60 to +78
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I run the tests for just this file in isolation so I can quickly get just warnings and errors that relate to this change? I'm a bit overwhelmed with all the output when I run cargo test :D

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo test -p hdx_ast scopes to just the hdx_astpackage, but you can also filter by module or test name so in the case runningcargo test -p hdx_ast css::values::fonts` should scope it to just these tests.

Loading