Skip to content

Commit

Permalink
Macro support for uncased
Browse files Browse the repository at this point in the history
  • Loading branch information
edef1c committed Feb 17, 2024
1 parent 999e6a2 commit 97be044
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 28 deletions.
4 changes: 2 additions & 2 deletions phf_codegen/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ edition = "2021"

[dependencies]
phf = { version = "^0.11.2", features = ["uncased", "unicase"] }
uncased = { version = "0.9.6", default-features = false }
uncased = { version = "0.9.7", default-features = false }
unicase = "2.4.0"

[build-dependencies]
phf_codegen = { version = "^0.11.2", path = ".." }
unicase = "2.4.0"
uncased = { version = "0.9.6", default-features = false }
uncased = { version = "0.9.7", default-features = false }
2 changes: 2 additions & 0 deletions phf_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ proc-macro = true

[features]
unicase = ["unicase_", "phf_shared/unicase"]
uncased = ["uncased_", "phf_shared/uncased"]

[dependencies]
syn = { version = "2", features = ["full"] }
quote = "1"
proc-macro2 = "1"
unicase_ = { package = "unicase", version = "2.4.0", optional = true }
uncased_ = { package = "uncased", version = "0.9.7", optional = true }

phf_generator = "0.11.1"
phf_shared = { version = "^0.11.2", default-features = false }
58 changes: 33 additions & 25 deletions phf_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::hash::Hasher;
use syn::parse::{self, Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{parse_macro_input, Error, Expr, ExprLit, Lit, Token, UnOp};
#[cfg(feature = "uncased")]
use uncased_::Uncased;
#[cfg(feature = "unicase")]
use unicase_::UniCase;

Expand All @@ -33,6 +35,8 @@ enum ParsedKey {
Bool(bool),
#[cfg(feature = "unicase")]
UniCase(UniCase<String>),
#[cfg(feature = "uncased")]
Uncased(Uncased<'static>),
}

impl PhfHash for ParsedKey {
Expand All @@ -57,6 +61,8 @@ impl PhfHash for ParsedKey {
ParsedKey::Bool(s) => s.phf_hash(state),
#[cfg(feature = "unicase")]
ParsedKey::UniCase(s) => s.phf_hash(state),
#[cfg(feature = "uncased")]
ParsedKey::Uncased(s) => s.phf_hash(state),
}
}
}
Expand Down Expand Up @@ -138,34 +144,36 @@ impl ParsedKey {
}
}
Expr::Group(group) => ParsedKey::from_expr(&group.expr),
#[cfg(feature = "unicase")]
Expr::Call(call) => {
if let Expr::Path(ep) = call.func.as_ref() {
let segments = &mut ep.path.segments.iter().rev();
let last = &segments.next()?.ident;
let last_ahead = &segments.next()?.ident;
let is_unicode = last_ahead == "UniCase" && last == "unicode";
let is_ascii = last_ahead == "UniCase" && last == "ascii";
if call.args.len() == 1 && (is_unicode || is_ascii) {
if let Some(Expr::Lit(ExprLit {
Expr::Call(call) if call.args.len() == 1 => {
match (call.func.as_ref(), call.args.first().unwrap()) {
(
Expr::Path(ep),
Expr::Lit(ExprLit {
lit: Lit::Str(_value),
attrs: _,
lit: Lit::Str(s),
})) = call.args.first()
{
let v = if is_unicode {
UniCase::unicode(s.value())
} else {
UniCase::ascii(s.value())
};
Some(ParsedKey::UniCase(v))
} else {
None
}),
) => {
let _value = _value.value();

let segments = &mut ep.path.segments.iter().rev();
let last = segments.next()?.ident.to_string();
let last_ahead = segments.next()?.ident.to_string();

match (&*last_ahead, &*last) {
#[cfg(feature = "unicase")]
("UniCase", "unicode") => {
Some(ParsedKey::UniCase(UniCase::unicode(_value)))
}
#[cfg(feature = "unicase")]
("UniCase", "ascii") => {
Some(ParsedKey::UniCase(UniCase::ascii(_value)))
}
#[cfg(feature = "uncased")]
("UncasedStr", "new") => Some(ParsedKey::Uncased(Uncased::new(_value))),
_ => None,
}
} else {
None
}
} else {
None
_ => None,
}
}
_ => None,
Expand Down
3 changes: 2 additions & 1 deletion phf_macros_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ categories = ["data-structures"]
[dev-dependencies]
trybuild = "1.0"
phf = { version = "0.11", features = ["macros"] }
phf_macros = { version = "0.11", features = ["unicase"] }
phf_macros = { version = "0.11", features = ["unicase", "uncased"] }
unicase = "2.4.0"
uncased = "0.9.7"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use phf::phf_map;
use uncased::UncasedStr;

static MAP: phf::Map<&'static UncasedStr, isize> = phf_map!(
UncasedStr::new("FOO") => 42,
UncasedStr::new("foo") => 42, //~ ERROR duplicate key UncasedStr("FOO")
);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: duplicate key
--> tests/compile-fail-uncased/equivalent-keys.rs:6:5
|
6 | UncasedStr::new("foo") => 42, //~ ERROR duplicate key UncasedStr("FOO")
| ^^^^^^^^^^^^^^^^^^^^^^
12 changes: 12 additions & 0 deletions phf_macros_tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ mod map {
assert!(Some(&11) == MAP.get(&UniCase::new("bar")));
assert_eq!(None, MAP.get(&UniCase::new("asdf")));
}

#[test]
fn test_uncased() {
use uncased::UncasedStr;
static MAP: phf::Map<&'static UncasedStr, isize> = phf_map!(
UncasedStr::new("FOO") => 10,
UncasedStr::new("Bar") => 11,
);
assert!(Some(&10) == MAP.get("FOo".into()));
assert!(Some(&11) == MAP.get("bar".into()));
assert_eq!(None, MAP.get("asdf".into()));
}
}

mod set {
Expand Down
7 changes: 7 additions & 0 deletions phf_macros_tests/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ fn compile_test_unicase() {
t.compile_fail("tests/compile-fail-unicase/*.rs");
}

#[test]
#[ignore]
fn compile_test_uncased() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile-fail-uncased/*.rs");
}

#[test]
#[ignore]
fn compile_fail() {
Expand Down

0 comments on commit 97be044

Please sign in to comment.