From 0793263da0e54c6db62d9cb00a0259bfe0409c3f Mon Sep 17 00:00:00 2001 From: eastspire Date: Mon, 21 Sep 2026 17:00:08 +0800 Subject: [PATCH] refactor(cache): extract magic strings/selectors to const.rs, route swc paths via lib.rs PR #17 introduced scraper HTML parsing + swc_ecma_parser AST walking as the replacement for the regex-based resource extraction, but the diff had three rust-standards violations that block reviewer approval: 1. Four Selector::parse(...).unwrap() calls (R11.4 - no unwrap in production). The selector strings are compile-time constants, so the panic is theoretical, but rust-standards bans unwrap/expect/panic unconditionally and forbids #[allow] macros for silencing lint. Fix: lazy-init a OnceLock> in cache/static.rs, surface the (impossible) parse error via euv_log! + early return. 2. fn.rs hardcoded byte/string literals (R1.3c literal purity). Move CSS selector strings (script[src], link[href], img[src], script:not([src])), URL scheme prefixes (http://, https://, data:, //, ://), and the 16 resource-extension suffixes (.wasm, .js, .mjs, .css, ...) to const.rs. Replace the long if/else chain in is_resource_path() with a RESOURCE_EXTENSIONS.iter().any(|s| value.ends_with(*s)) loop. 3. swc_ecma_ast::Expr / Lit / Callee / MemberProp / etc. long-path calls in sub-file fn.rs bodies (R6.4-pitfall-b). The sub-file already has use super::*; and the lib.rs already re-exports swc_ecma_ast::{EsVersion, Program} - extend that block to also expose ArrowFunctionBody, Callee, Decl, Expr, Lit, MemberProp, ModuleDecl, ModuleItem, Prop, PropOrSpread, Stmt, VarDeclOrExpr, and the corresponding swc_common::FileName + scraper::selector::SelectorErrorKind. Replace every swc_ecma_ast::xxx reference with the short name (Expr, Stmt, ...). Resulting audit: 19/19 PASS. --- src-tauri/src/cache/const.rs | 35 ++++++ src-tauri/src/cache/fn.rs | 207 ++++++++++++++++++---------------- src-tauri/src/cache/static.rs | 9 ++ src-tauri/src/lib.rs | 9 +- 4 files changed, 157 insertions(+), 103 deletions(-) diff --git a/src-tauri/src/cache/const.rs b/src-tauri/src/cache/const.rs index 48983a3..d1dabbc 100644 --- a/src-tauri/src/cache/const.rs +++ b/src-tauri/src/cache/const.rs @@ -52,6 +52,41 @@ pub(crate) const RELOAD_LISTENER_SCRIPT: &str = r#""#; +/// URL scheme prefix for `http://` absolute URLs — skipped during resource discovery. +pub(crate) const HTTP_PREFIX: &str = "http://"; + +/// URL scheme prefix for `https://` absolute URLs — skipped during resource discovery. +pub(crate) const HTTPS_PREFIX: &str = "https://"; + +/// Inline-data URL prefix (`data:image/png;base64,...`) — skipped during resource discovery. +pub(crate) const DATA_PREFIX: &str = "data:"; + +/// Protocol-relative URL prefix (`//cdn.example.com/x.js`) — skipped during resource discovery. +pub(crate) const SCHEME_RELATIVE: &str = "//"; + +/// Authority separator embedded in absolute URLs (`https://host/path`) — any string containing +/// this is treated as absolute and skipped during resource discovery. +pub(crate) const SCHEME_SEPARATOR: &str = "://"; + +/// CSS selector that matches external `