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

(ast) add color_hdr support #61

Merged
merged 1 commit into from
Dec 11, 2024
Merged

(ast) add color_hdr support #61

merged 1 commit into from
Dec 11, 2024

Conversation

keithamus
Copy link
Owner

This adds supports for the CSS color-hdr, by commenting out the DynamicRangeLimit enum and adding a new DynamicRangeLimitMix struct in the crates/hdx_ast/src/css/values/color_hdr/types.rs file.

DynamicRangeLimitMix is a struct that represents the dynamic-range-limit-mix() function in CSS.

@keithamus keithamus force-pushed the ast-add-color-hdr-support branch from 6946110 to c6d4b39 Compare December 10, 2024 23:49
Copy link
Owner Author

@keithamus keithamus left a comment

Choose a reason for hiding this comment

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

This might serve as a good example of how to implement some values, and so I'll leave some commentary regarding this PR:

@@ -1 +1,2 @@
pub(crate) use crate::traits::StyleValue;
pub(crate) use hdx_proc_macro::*;
Copy link
Owner Author

Choose a reason for hiding this comment

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

All impls.rs need to include at least these two lines so that the macros work.

Comment on lines +11 to +18
// https://drafts.csswg.org/css-color-hdr-1/#dynamic-range-limit
#[value(" standard | high | constrained-high | <dynamic-range-limit-mix()> ")]
#[initial("high")]
#[applies_to("all elements")]
#[inherited("no")]
#[percentages("n/a")]
#[canonical_order("per grammar")]
#[animation_type("by dynamic-range-limit-mix()")]
Copy link
Owner Author

Choose a reason for hiding this comment

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

This is alll autogenerated. It doesn't need changing other than oncommenting.

#[percentages("n/a")]
#[canonical_order("per grammar")]
#[animation_type("by dynamic-range-limit-mix()")]
pub enum DynamicRangeLimit<'a> {}
Copy link
Owner Author

Choose a reason for hiding this comment

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

A small change is needed here: the <'a> lifetime is needed because this enum depends on DynamicRangeLimitMix which itself has <'a> (because it depends on a Vec<'a>).

pub(crate) use crate::css::types::*;
pub(crate) use crate::css::units::*;
use crate::css::units::LengthPercentage;
use bumpalo::collections::Vec;
Copy link
Owner Author

Choose a reason for hiding this comment

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

We use use bumpalo::collections::Vec rather than std::vec::Vec so we can use bump allocations. They work the same other than bumpalo's vec requires a lifetime, so Vec<'a, T> rather than Vec<T>, in addition rather than Vec::new() we need Vec::new_in(bump).

Comment on lines +5 to +8
mod func {
use hdx_parser::custom_function;
custom_function!(DynamicRangeLimitMix, atom!("dynamic-range-limit-mix"));
}
Copy link
Owner Author

Choose a reason for hiding this comment

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

When comparing just one or two functions or idents, it can be more convenient to use custom_function! or custom_keyword! macros, which generate a mini parsable struct. They get a little unweildy if using many, so only use sparingly. I may end up removing this, depending on how much it gets used overall.

}
}

impl<'a> Parse<'a> for DynamicRangeLimitMix<'a> {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Every AST Node needs the Parse trait which is given a parser and can repeatedly parse other tokens or trees until it can complet its own node.

}
}

impl<'a> ToCursors<'a> for DynamicRangeLimitMix<'a> {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Every AST node must also implement the ToCursors trait which is given a CursorStream and must feed its cursors, in order, to the cursor stream. This then lets us write the cursor stream back out to a string.


impl<'a> ToCursors<'a> for DynamicRangeLimitMix<'a> {
fn to_cursors(&self, s: &mut CursorStream<'a>) {
s.append(self.function.into());
Copy link
Owner Author

Choose a reason for hiding this comment

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

Something like a T![Function] is a "newtyped" Cursor, so calling .into() should convert it into a Cursor. For things made up of multiple cursors you'll need to call ToCursors::to_cursors(&self.the_thing, s).

Comment on lines +72 to +75
#[test]
fn size_test() {
assert_size!(DynamicRangeLimitMix, 56);
}
Copy link
Owner Author

Choose a reason for hiding this comment

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

Important to track sizes of all structs so we can figure out places to compress or align or otherwise optimize hot nodes.

@@ -1350,8 +1350,8 @@ impl DefType {

pub fn requires_allocator_lifetime(&self) -> bool {
if let Self::Custom(DefIdent(ident), _) = self {
return matches!(ident, &atom!("OutlineColor") | &atom!("BorderTopColor") | &atom!("AnchorName"));
}
return matches!(ident, &atom!("OutlineColor") | &atom!("BorderTopColor") | &atom!("AnchorName") | &atom!("DynamicRangeLimitMix"));
Copy link
Owner Author

Choose a reason for hiding this comment

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

Because DynamicRangeLimitMix required the <'a> lifetime, it needs to be added here. This is... not ideal, but it works for now. The generator needs to be smarter.

@keithamus keithamus marked this pull request as ready for review December 11, 2024 00:07
@keithamus keithamus merged commit e330564 into main Dec 11, 2024
3 of 4 checks passed
@keithamus keithamus deleted the ast-add-color-hdr-support branch December 11, 2024 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant