Use rust-phf to map strings to property IDs

This commit is contained in:
Simon Sapin 2016-12-08 14:19:27 -10:00
parent 137e30b825
commit fdc40592de
7 changed files with 50 additions and 36 deletions

View file

@ -40,6 +40,7 @@ num-traits = "0.1.32"
ordered-float = "0.2.2"
owning_ref = "0.2.2"
parking_lot = "0.3.3"
phf = "0.7.20"
quickersort = "2.0.0"
rand = "0.3"
rayon = "0.5"
@ -64,4 +65,5 @@ version = "1.0"
kernel32-sys = "0.2"
[build-dependencies]
phf_codegen = "0.7.20"
walkdir = "0.1"

View file

@ -3,8 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate walkdir;
extern crate phf_codegen;
use std::env;
use std::fs::File;
use std::io::{BufWriter, BufReader, BufRead, Write};
use std::path::Path;
use std::process::{Command, exit};
use walkdir::WalkDir;
@ -61,4 +64,21 @@ fn main() {
if !status.success() {
exit(1)
}
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.rs");
let static_ids = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.txt");
let mut file = BufWriter::new(File::create(&path).unwrap());
let static_ids = BufReader::new(File::open(&static_ids).unwrap());
write!(&mut file, "static STATIC_IDS: ::phf::Map<&'static str, StaticId> = ").unwrap();
let mut map = phf_codegen::Map::new();
for result in static_ids.lines() {
let line = result.unwrap();
let mut split = line.split('\t');
let key = split.next().unwrap().to_owned();
let value = split.next().unwrap();
map.entry(key, value);
}
map.build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
}

View file

@ -71,6 +71,7 @@ extern crate num_traits;
extern crate ordered_float;
extern crate owning_ref;
extern crate parking_lot;
extern crate phf;
extern crate quickersort;
extern crate rayon;
extern crate rustc_serialize;

View file

@ -33,6 +33,7 @@ def main():
rust = render(template, product=product, data=properties, __file__=template)
if output == "style-crate":
write(os.environ["OUT_DIR"], "properties.rs", rust)
write(os.environ["OUT_DIR"], "static_ids.txt", static_ids(properties))
if product == "gecko":
template = os.path.join(BASE, "gecko.mako.rs")
rust = render(template, data=properties)
@ -69,6 +70,15 @@ def write(directory, filename, content):
open(os.path.join(directory, filename), "wb").write(content)
def static_ids(properties):
return '\n'.join(
"%s\tStaticId::%s(%sId::%s)" % (p.name, kind, kind, p.camel_case)
for kind, props in [("Longhand", properties.longhands),
("Shorthand", properties.shorthands)]
for p in props
)
def write_html(properties):
properties = dict(
(p.name, {

View file

@ -391,16 +391,6 @@ pub enum LonghandId {
}
impl LonghandId {
pub fn from_ascii_lowercase_name(name: &str) -> Result<Self, ()> {
// FIXME: use rust-phf?
match name {
% for property in data.longhands:
"${property.name}" => Ok(LonghandId::${property.camel_case}),
% endfor
_ => Err(())
}
}
pub fn name(&self) -> &'static str {
match *self {
% for property in data.longhands:
@ -425,16 +415,6 @@ impl ToCss for ShorthandId {
}
impl ShorthandId {
pub fn from_ascii_lowercase_name(name: &str) -> Result<Self, ()> {
// FIXME: use rust-phf?
match name {
% for property in data.shorthands:
"${property.name}" => Ok(ShorthandId::${property.camel_case}),
% endfor
_ => Err(())
}
}
pub fn name(&self) -> &'static str {
match *self {
% for property in data.shorthands:
@ -629,19 +609,26 @@ impl ToCss for PropertyId {
}
}
// FIXME(https://github.com/rust-lang/rust/issues/33156): remove this enum and use PropertyId
// when stable Rust allows destructors in statics.
enum StaticId {
Longhand(LonghandId),
Shorthand(ShorthandId),
}
include!(concat!(env!("OUT_DIR"), "/static_ids.rs"));
impl PropertyId {
/// Returns Err(()) for unknown non-custom properties
pub fn parse(s: Cow<str>) -> Result<Self, ()> {
if let Ok(name) = ::custom_properties::parse_name(&s) {
return Ok(PropertyId::Custom(::custom_properties::Name::from(name)))
}
let lower_case = ::str::cow_into_ascii_lowercase(s);
if let Ok(id) = LonghandId::from_ascii_lowercase_name(&lower_case) {
Ok(PropertyId::Longhand(id))
} else if let Ok(id) = ShorthandId::from_ascii_lowercase_name(&lower_case) {
Ok(PropertyId::Shorthand(id))
} else {
Err(())
match STATIC_IDS.get(&*lower_case) {
Some(&StaticId::Longhand(id)) => Ok(PropertyId::Longhand(id)),
Some(&StaticId::Shorthand(id)) => Ok(PropertyId::Shorthand(id)),
None => Err(()),
}
}