From 72caf29ad8e305bc04ac206d0f823212722b5d49 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 1 Oct 2024 12:37:14 -0700 Subject: [PATCH] parse as url rather than regex --- Cargo.lock | 129 ++++++++++++++++++++++--------------------- Cargo.toml | 4 +- src/roblox_legacy.rs | 32 +++++++---- 3 files changed, 90 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87fd2a6..f194721 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,15 +15,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - [[package]] name = "approx" version = "0.4.0" @@ -172,6 +163,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835a3dc7d1ec9e75e2b5fb4ba75396837112d2060b03f7d43bc1897c7f7211da" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "getrandom" version = "0.2.15" @@ -206,6 +206,16 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "2.3.0" @@ -225,29 +235,6 @@ dependencies = [ "either", ] -[[package]] -name = "lazy-regex" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576c8060ecfdf2e56995cf3274b4f2d71fa5e4fa3607c1c0b63c10180ee58741" -dependencies = [ - "lazy-regex-proc_macros", - "once_cell", - "regex", -] - -[[package]] -name = "lazy-regex-proc_macros" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9efb9e65d4503df81c615dc33ff07042a9408ac7f26b45abee25566f7fbfd12c" -dependencies = [ - "proc-macro2", - "quote", - "regex", - "syn 2.0.72", -] - [[package]] name = "libc" version = "0.2.155" @@ -321,6 +308,12 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "proc-macro-crate" version = "3.1.0" @@ -348,35 +341,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" - [[package]] name = "static_assertions" version = "1.1.0" @@ -399,8 +363,8 @@ dependencies = [ name = "strafesnet_deferred_loader" version = "0.3.3" dependencies = [ - "lazy-regex", "strafesnet_common", + "url", "vbsp", ] @@ -457,6 +421,21 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "toml_datetime" version = "0.6.8" @@ -474,12 +453,38 @@ dependencies = [ "winnow", ] +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "vbsp" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index f3f8e82..d14377d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ authors = ["Rhys Lloyd "] [features] default = ["legacy"] -legacy = ["dep:lazy-regex", "dep:vbsp"] +legacy = ["dep:url","dep:vbsp"] #roblox = ["dep:lazy-regex"] #source = ["dep:vbsp"] [dependencies] -lazy-regex = { version = "3.1.0", optional = true } strafesnet_common = { version = "0.4.0", registry = "strafesnet" } +url = { version = "2.5.2", optional = true } vbsp = { version = "0.5.0", optional = true } diff --git a/src/roblox_legacy.rs b/src/roblox_legacy.rs index 3cfdbdb..18263c7 100644 --- a/src/roblox_legacy.rs +++ b/src/roblox_legacy.rs @@ -7,19 +7,29 @@ use strafesnet_common::model::{MeshId,RenderConfig,RenderConfigId,TextureId}; #[derive(Hash,Eq,PartialEq)] struct RobloxAssetId(u64); #[derive(Debug)] -pub struct RobloxAssetIdParseErr(String); +#[allow(dead_code)] +pub enum RobloxAssetIdParseErr{ + Url(url::ParseError), + UnknownScheme(String), + ParseInt(std::num::ParseIntError), + MissingAssetId(String), +} impl std::str::FromStr for RobloxAssetId{ type Err=RobloxAssetIdParseErr; fn from_str(s:&str)->Result{ - let regman=lazy_regex::regex!(r"(\d+)$"); - if let Some(captures)=regman.captures(s){ - if captures.len()==2{//captures[0] is all captures concatenated, and then each individual capture - if let Ok(id)=captures[0].parse::(){ - return Ok(Self(id)); - } - } - } - Err(RobloxAssetIdParseErr(s.to_owned())) + let url=url::Url::parse(s).map_err(RobloxAssetIdParseErr::Url)?; + let parsed_asset_id=match url.scheme(){ + "rbxassetid"=>url.domain().ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?.parse(), + "http"|"https"=>{ + let (_,asset_id)=url.query_pairs() + .find(|(id,_)| + id.to_lowercase()=="id" + ).ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?; + asset_id.parse() + }, + _=>Err(RobloxAssetIdParseErr::UnknownScheme(s.to_owned()))?, + }; + Ok(Self(parsed_asset_id.map_err(RobloxAssetIdParseErr::ParseInt)?)) } } impl std::fmt::Display for RobloxAssetIdParseErr{ @@ -133,4 +143,4 @@ impl Loader{ self.render_config_loader.render_configs, )) } -} \ No newline at end of file +}