Skip to content

Commit

Permalink
drop string_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jun 5, 2024
1 parent 0ce31af commit f2d9bd3
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 84 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ nom = "7.1"
once_cell = "1.19"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
string_cache = "0.8"
thiserror = "1.0"

[dev-dependencies]
Expand Down
2 changes: 0 additions & 2 deletions generate-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ publish = false

[dependencies]
anyhow = "1.0"
flate2 = "1.0"
indexmap = { version = "2.1", features = ["serde"] }
quote = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
string_cache_codegen = "0.5.2"
30 changes: 7 additions & 23 deletions generate-data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ struct Feature {
}

fn main() -> Result<()> {
generate_browser_names_cache()?;
build_electron_to_chromium()?;
build_node_versions()?;
build_node_release_schedule()?;
Expand All @@ -70,21 +69,6 @@ fn main() -> Result<()> {
Ok(())
}

fn generate_browser_names_cache() -> Result<()> {
string_cache_codegen::AtomType::new(
"data::browser_name::BrowserNameAtom",
"browser_name_atom!",
)
.atoms(&[
"ie", "edge", "firefox", "chrome", "safari", "opera", "ios_saf", "op_mini", "android",
"bb", "op_mob", "and_chr", "and_ff", "ie_mob", "and_uc", "samsung", "and_qq", "baidu",
"kaios",
])
.write_to_file(&Path::new(OUT_DIR).join("browser_name_atom.rs"))?;

Ok(())
}

fn build_electron_to_chromium() -> Result<()> {
let path = format!("{OUT_DIR}/electron-to-chromium.rs");

Expand Down Expand Up @@ -199,8 +183,8 @@ fn build_caniuse_global() -> Result<()> {
}
});
quote! {
map.insert(BrowserNameAtom::from(#name), BrowserStat {
name: BrowserNameAtom::from(#name),
map.insert(#name, BrowserStat {
name: #name,
version_list: vec![#(#detail),*],
});
}
Expand All @@ -222,7 +206,7 @@ fn build_caniuse_global() -> Result<()> {
(
usage,
quote! {
(BrowserNameAtom::from(#name), #version, #usage)
(#name, #version, #usage)
},
)
})
Expand Down Expand Up @@ -277,9 +261,9 @@ fn build_caniuse_global() -> Result<()> {
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use serde_json::from_str;
use crate::data::browser_name::{BrowserNameAtom, decode_browser_name};
use crate::data::decode_browser_name;

type Stat = Lazy<AHashMap<BrowserNameAtom, IndexMap<&'static str, u8>>>;
type Stat = Lazy<AHashMap<&'static str, IndexMap<&'static str, u8>>>;
type Json = AHashMap::<u8, IndexMap<&'static str, u8>>;

match name {
Expand Down Expand Up @@ -364,9 +348,9 @@ fn build_caniuse_region() -> Result<()> {
let tokens = quote! {{
use once_cell::sync::Lazy;
use serde_json::from_str;
use crate::data::browser_name::{BrowserNameAtom, decode_browser_name};
use crate::data::decode_browser_name;

type Usage = Lazy<Vec<(BrowserNameAtom, &'static str, f32)>>;
type Usage = Lazy<Vec<(&'static str, &'static str, f32)>>;
type Json = Vec<(u8, &'static str, f32)>;

match region {
Expand Down
31 changes: 13 additions & 18 deletions src/data/caniuse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::browser_name::BrowserNameAtom;
use ahash::AHashMap;
use once_cell::sync::Lazy;
use std::borrow::Cow;
Expand All @@ -11,7 +10,7 @@ pub const OP_MOB_BLINK_FIRST: u32 = 14;

#[derive(Clone, Debug)]
pub struct BrowserStat {
name: BrowserNameAtom,
name: &'static str,
pub version_list: Vec<VersionDetail>,
}

Expand All @@ -22,16 +21,16 @@ pub struct VersionDetail {
pub release_date: Option<i64>,
}

pub type CaniuseData = AHashMap<BrowserNameAtom, BrowserStat>;
pub type CaniuseData = AHashMap<&'static str, BrowserStat>;

pub static CANIUSE_BROWSERS: Lazy<CaniuseData> =
Lazy::new(|| include!("../generated/caniuse-browsers.rs"));

pub static CANIUSE_GLOBAL_USAGE: Lazy<Vec<(BrowserNameAtom, &'static str, f32)>> =
pub static CANIUSE_GLOBAL_USAGE: Lazy<Vec<(&'static str, &'static str, f32)>> =
Lazy::new(|| include!("../generated/caniuse-global-usage.rs"));

pub static BROWSER_VERSION_ALIASES: Lazy<
AHashMap<BrowserNameAtom, AHashMap<&'static str, &'static str>>,
AHashMap<&'static str, AHashMap<&'static str, &'static str>>,
> = Lazy::new(|| {
let mut aliases = CANIUSE_BROWSERS
.iter()
Expand All @@ -56,11 +55,11 @@ pub static BROWSER_VERSION_ALIASES: Lazy<
if aliases.is_empty() {
None
} else {
Some((name.clone(), aliases))
Some((*name, aliases))
}
})
.collect::<AHashMap<BrowserNameAtom, _>>();
let _ = aliases.insert("op_mob".into(), {
.collect::<AHashMap<&'static str, _>>();
let _ = aliases.insert("op_mob", {
let mut aliases = AHashMap::new();
let _ = aliases.insert("59", "58");
aliases
Expand All @@ -69,8 +68,8 @@ pub static BROWSER_VERSION_ALIASES: Lazy<
});

static ANDROID_TO_DESKTOP: Lazy<BrowserStat> = Lazy::new(|| {
let chrome = CANIUSE_BROWSERS.get(&"chrome".into()).unwrap();
let mut android = CANIUSE_BROWSERS.get(&"android".into()).unwrap().clone();
let chrome = CANIUSE_BROWSERS.get("chrome").unwrap();
let mut android = CANIUSE_BROWSERS.get("android").unwrap().clone();

android.version_list = android
.version_list
Expand Down Expand Up @@ -105,7 +104,7 @@ static ANDROID_TO_DESKTOP: Lazy<BrowserStat> = Lazy::new(|| {
});

static OPERA_MOBILE_TO_DESKTOP: Lazy<BrowserStat> =
Lazy::new(|| CANIUSE_BROWSERS.get(&"opera".into()).unwrap().clone());
Lazy::new(|| CANIUSE_BROWSERS.get("opera").unwrap().clone());

pub fn get_browser_stat(
name: &str,
Expand All @@ -124,18 +123,14 @@ pub fn get_browser_stat(
"android" => Some(("android", &ANDROID_TO_DESKTOP)),
"op_mob" => Some(("op_mob", &OPERA_MOBILE_TO_DESKTOP)),
_ => CANIUSE_BROWSERS
.get(&desktop_name.into())
.get(desktop_name)
.map(|stat| (get_mobile_by_desktop_name(desktop_name), stat)),
}
} else {
CANIUSE_BROWSERS
.get(&name.into())
.map(|stat| (&*stat.name, stat))
CANIUSE_BROWSERS.get(name).map(|stat| (stat.name, stat))
}
} else {
CANIUSE_BROWSERS
.get(&name.into())
.map(|stat| (&*stat.name, stat))
CANIUSE_BROWSERS.get(name).map(|stat| (stat.name, stat))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/data/caniuse/features.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::BrowserNameAtom;
use ahash::AHashMap;
use indexmap::IndexMap;

type Feature = AHashMap<BrowserNameAtom, IndexMap<&'static str, u8>>;
type Feature = AHashMap<&'static str, IndexMap<&'static str, u8>>;

pub(crate) fn get_feature_stat(name: &str) -> Option<&'static Feature> {
include!("../../generated/caniuse-feature-matching.rs")
Expand Down
4 changes: 1 addition & 3 deletions src/data/caniuse/region.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::BrowserNameAtom;

type RegionData = Vec<(BrowserNameAtom, &'static str, f32)>;
type RegionData = Vec<(&'static str, &'static str, f32)>;

pub(crate) fn get_usage_by_region(region: &str) -> Option<&'static RegionData> {
include!("../../generated/caniuse-region-matching.rs")
Expand Down
49 changes: 22 additions & 27 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,27 @@ pub(crate) mod electron;
pub(crate) mod node;

#[doc(hidden)]
#[allow(unused)]
pub(crate) mod browser_name {
include!("../generated/browser_name_atom.rs");

pub fn decode_browser_name(id: u8) -> BrowserNameAtom {
match id {
1 => browser_name_atom!("ie"),
2 => browser_name_atom!("edge"),
3 => browser_name_atom!("firefox"),
4 => browser_name_atom!("chrome"),
5 => browser_name_atom!("safari"),
6 => browser_name_atom!("opera"),
7 => browser_name_atom!("ios_saf"),
8 => browser_name_atom!("op_mini"),
9 => browser_name_atom!("android"),
10 => browser_name_atom!("bb"),
11 => browser_name_atom!("op_mob"),
12 => browser_name_atom!("and_chr"),
13 => browser_name_atom!("and_ff"),
14 => browser_name_atom!("ie_mob"),
15 => browser_name_atom!("and_uc"),
16 => browser_name_atom!("samsung"),
17 => browser_name_atom!("and_qq"),
18 => browser_name_atom!("baidu"),
19 => browser_name_atom!("kaios"),
_ => unreachable!("cannot recognize browser id"),
}
pub fn decode_browser_name(id: u8) -> &'static str {
match id {
1 => "ie",
2 => "edge",
3 => "firefox",
4 => "chrome",
5 => "safari",
6 => "opera",
7 => "ios_saf",
8 => "op_mini",
9 => "android",
10 => "bb",
11 => "op_mob",
12 => "and_chr",
13 => "and_ff",
14 => "ie_mob",
15 => "and_uc",
16 => "samsung",
17 => "and_qq",
18 => "baidu",
19 => "kaios",
_ => unreachable!("cannot recognize browser id"),
}
}
2 changes: 1 addition & 1 deletion src/queries/browser_unbounded_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(super) fn browser_unbounded_range(
let (name, stat) = get_browser_stat(name, opts.mobile_to_desktop)
.ok_or_else(|| Error::BrowserNotFound(name.to_string()))?;
let version: Version = BROWSER_VERSION_ALIASES
.get(&name.into())
.get(name)
.and_then(|alias| alias.get(version).copied())
.unwrap_or(version)
.parse()
Expand Down
11 changes: 4 additions & 7 deletions src/queries/supports.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{Distrib, QueryResult};
use crate::{
data::{
browser_name::BrowserNameAtom,
caniuse::{features::get_feature_stat, get_browser_stat, to_desktop_name, VersionDetail},
},
data::caniuse::{features::get_feature_stat, get_browser_stat, to_desktop_name, VersionDetail},
error::Error,
parser::SupportKind,
Opts,
Expand Down Expand Up @@ -41,11 +38,11 @@ pub(super) fn supports(name: &str, kind: Option<SupportKind>, opts: &Opts) -> Qu
.iter()
.filter_map(move |VersionDetail { version, .. }| {
versions
.get(&**version)
.get(version)
.or_else(|| match desktop_name {
Some(desktop_name) if check_desktop => feature
.get(&BrowserNameAtom::from(desktop_name))
.and_then(|versions| versions.get(&**version)),
.get(desktop_name)
.and_then(|versions| versions.get(version)),
_ => None,
})
.and_then(|flags| {
Expand Down

0 comments on commit f2d9bd3

Please sign in to comment.