Skip to main content

omena_cascade/
property_metadata.rs

1//! CSS property metadata lookups (inheritance, initial values) over the generated authority.
2
3use crate::property_metadata_idl_generated::{
4    CSS_PROPERTY_METADATA_RECORDS_V1, CssPropertyMetadataRecordStaticV1,
5};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum CssPropertyInitialValueV0 {
9    Literal(&'static str),
10    GuaranteedInvalid,
11    Unknown,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum CssPropertyInheritanceV0 {
16    Inherited,
17    NotInherited,
18    Unknown,
19}
20
21pub fn css_property_metadata_for_property(
22    property: &str,
23) -> Option<&'static CssPropertyMetadataRecordStaticV1> {
24    css_property_metadata_for_property_in_records(property, CSS_PROPERTY_METADATA_RECORDS_V1)
25}
26
27#[doc(hidden)]
28pub fn css_property_metadata_for_property_in_records(
29    property: &str,
30    records: &'static [CssPropertyMetadataRecordStaticV1],
31) -> Option<&'static CssPropertyMetadataRecordStaticV1> {
32    records
33        .binary_search_by_key(&property, |record| record.canonical_name)
34        .ok()
35        .map(|index| &records[index])
36}
37
38pub fn css_property_is_inherited(property: &str) -> CssPropertyInheritanceV0 {
39    if property.starts_with("--") {
40        return CssPropertyInheritanceV0::Inherited;
41    }
42
43    match css_property_metadata_for_property(property).and_then(|record| record.inherited) {
44        Some(true) => CssPropertyInheritanceV0::Inherited,
45        Some(false) => CssPropertyInheritanceV0::NotInherited,
46        None => CssPropertyInheritanceV0::Unknown,
47    }
48}
49
50pub fn css_property_initial_value(property: &str) -> CssPropertyInitialValueV0 {
51    if property.starts_with("--") {
52        return CssPropertyInitialValueV0::GuaranteedInvalid;
53    }
54
55    css_property_metadata_for_property(property)
56        .and_then(|record| record.initial_value)
57        .map(CssPropertyInitialValueV0::Literal)
58        .unwrap_or(CssPropertyInitialValueV0::Unknown)
59}