Skip to content

macros: Keep the ability for ascii_case_insensitive_phf_map to reference custom types. #354

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

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
32 changes: 22 additions & 10 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,36 @@ macro_rules! ascii_case_insensitive_phf_map {
ascii_case_insensitive_phf_map!($name -> $ValueType = { $( $key => $value, )+ })
};
($name: ident -> $ValueType: ty = { $( $key: tt => $value: expr, )+ }) => {
mod $name {
use $crate::_cssparser_internal_phf as phf;
use $crate::_cssparser_internal_phf as phf;

// See macro above for context.
mod cssparser_internal {
$crate::_cssparser_internal_max_len! {
$( $key )+
}
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
$(
$key => $value,
)*
};
}

static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
$(
$key => $value,
)*
};

// While the obvious choice for this would be an inner module, it's not possible to
// reference from types from there, see:
// <https://github.com/rust-lang/rust/issues/114369>
//
// So we abuse a struct with static associated functions instead.
#[allow(non_camel_case_types)]
struct $name;
impl $name {
#[allow(dead_code)]
pub fn entries() -> impl Iterator<Item = (&'static &'static str, &'static $ValueType)> {
fn entries() -> impl Iterator<Item = (&'static &'static str, &'static $ValueType)> {
MAP.entries()
}

pub fn get(input: &str) -> Option<&'static $ValueType> {
$crate::_cssparser_internal_to_lowercase!(input, MAX_LENGTH => lowercase);
fn get(input: &str) -> Option<&'static $ValueType> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are these not pub any more?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because they wasn't earlier effectively, as the containing module was private.

$crate::_cssparser_internal_to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
MAP.get(lowercase?)
}
}
Expand Down