Skip to content

Commit

Permalink
(ast/types) add systemcolor keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Nov 5, 2024
1 parent c81549a commit b2dceb2
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 168 deletions.
35 changes: 20 additions & 15 deletions crates/hdx_ast/src/css/types/color/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod named;
mod syntax;
mod system;

use crate::css::units::{Angle, CSSFloat, Percent};
use hdx_atom::{atom, Atomizable};
Expand All @@ -9,13 +10,13 @@ use hdx_writer::{CssWriter, Result as WriterResult, WriteCss};
use std::str::Chars;

pub use named::*;
pub use system::*;
pub use syntax::*;

mod kw {
use hdx_parser::custom_keyword;
custom_keyword!(None, atom!("none"));
custom_keyword!(Currentcolor, atom!("currentcolor"));
custom_keyword!(Canvastext, atom!("canvastext"));
custom_keyword!(Transparent, atom!("transparent"));
}

Expand Down Expand Up @@ -195,13 +196,12 @@ impl<'a> WriteCss<'a> for AbsoluteColorFunction {
}
}

#[derive(Debug, Default, Clone, PartialEq, Hash)]
#[derive(Debug, Clone, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
pub enum Color {
#[default]
Currentcolor,
Canvastext,
Transparent,
System(SystemColor),
Hex(u32),
Named(NamedColor),
Absolute(AbsoluteColorFunction),
Expand All @@ -210,6 +210,12 @@ pub enum Color {
// Mix(ColorMixSyntax, Box<'a, Color<'a>>, u8, Box<'a, Color<'a>>),
}

impl Color {
// Alias CanvasText for #[initial()]
#[allow(non_upper_case_globals)]
pub const Canvastext: Color = Color::System(SystemColor::CanvasText);
}

trait HexableChars {
fn next_as_hex(&mut self) -> Option<u32>;
}
Expand Down Expand Up @@ -237,18 +243,14 @@ impl<'a> Peek<'a> for Color {
.peek::<func::Color>()
.or_else(|| parser.peek::<func::ColorMix>())
.or_else(|| parser.peek::<AbsoluteColorFunction>())
.or_else(|| parser.peek::<kw::Currentcolor>())
.or_else(|| parser.peek::<kw::Canvastext>())
.or_else(|| parser.peek::<kw::Transparent>())
.or_else(|| parser.peek::<Token![Hash]>())
.or_else(|| {
if let Some(token) = parser.peek::<Token![Ident]>() {
if NamedColor::from_atom(&parser.parse_atom_lower(token)).is_some() {
return Some(token);
}
}
None
parser.peek::<Token![Ident]>().filter(|token| {
let atom = parser.parse_atom_lower(*token);
matches!(atom, atom!("currentcolor") | atom!("canvastext") | atom!("transparent"))
|| NamedColor::from_atom(&atom).is_some()
})
})
.or_else(|| parser.peek::<Token![Hash]>())
}
}

Expand All @@ -265,6 +267,9 @@ impl<'a> Parse<'a> for Color {
if let Some(named) = NamedColor::from_atom(&name) {
parser.hop(token);
Ok(Color::Named(named))
} else if let Some(named) = SystemColor::from_atom(&name) {
parser.hop(token);
Ok(Color::System(named))
} else {
Err(diagnostics::UnexpectedIdent(name, token.span()))?
}
Expand Down Expand Up @@ -321,7 +326,7 @@ impl<'a> WriteCss<'a> for Color {
match self {
Self::Currentcolor => kw::Currentcolor::atom().write_css(sink),
Self::Transparent => kw::Transparent::atom().write_css(sink),
Self::Canvastext => kw::Canvastext::atom().write_css(sink),
Self::System(name) => name.to_atom().write_css(sink),
Self::Named(name) => name.to_atom().write_css(sink),
Self::Absolute(func) => func.write_css(sink),
Self::Hex(d) => {
Expand Down
Loading

0 comments on commit b2dceb2

Please sign in to comment.