From 070515f20b831097b0492efde788ec98515a6410 Mon Sep 17 00:00:00 2001 From: wtlin1228 Date: Sat, 5 Oct 2024 21:12:13 +0800 Subject: [PATCH] feat: make SymbolToRoute table serializable --- Cargo.lock | 3 --- crates/api_server/src/main.rs | 4 ++++ crates/dt_portable/Cargo.toml | 1 - crates/dt_portable/src/lib.rs | 16 +++++++++------- crates/dt_route/Cargo.toml | 2 -- crates/dt_route/src/lib.rs | 24 ++++++++---------------- crates/dt_scheduler/tests/scheduler.rs | 2 +- 7 files changed, 22 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b5d703..74204f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1048,7 +1048,6 @@ version = "0.1.0" dependencies = [ "anyhow", "dt_graph", - "dt_tracker", "serde", "serde_json", ] @@ -1060,8 +1059,6 @@ dependencies = [ "anyhow", "dt_parser", "dt_path_resolver", - "dt_test_utils", - "dt_tracker", "swc_core", "swc_ecma_parser", ] diff --git a/crates/api_server/src/main.rs b/crates/api_server/src/main.rs index b830f8a..925249d 100644 --- a/crates/api_server/src/main.rs +++ b/crates/api_server/src/main.rs @@ -15,6 +15,7 @@ use std::{ struct AppState { project_root: String, i18n_to_symbol: HashMap>>, + symbol_to_route: HashMap>>, used_by_graph: UsedByGraph, } @@ -32,6 +33,8 @@ async fn search( ) -> Result> { let search = path.into_inner(); + // TODO: deal with search mode + match data.i18n_to_symbol.get(&search) { None => Err(error::ErrorNotFound(format!("{} not found", search))), Some(ts) => { @@ -77,6 +80,7 @@ async fn main() -> std::io::Result<()> { .app_data(web::Data::new(AppState { project_root: portable.project_root.clone(), i18n_to_symbol: portable.i18n_to_symbol.clone(), + symbol_to_route: portable.symbol_to_route.clone(), used_by_graph: portable.used_by_graph.clone(), })) .service(search) diff --git a/crates/dt_portable/Cargo.toml b/crates/dt_portable/Cargo.toml index 9f85f20..5bfbeca 100644 --- a/crates/dt_portable/Cargo.toml +++ b/crates/dt_portable/Cargo.toml @@ -12,4 +12,3 @@ serde = { workspace = true } serde_json = { workspace = true } dt_graph = { version = "0.1.0", path = "../dt_graph" } -dt_tracker = { version = "0.1.0", path = "../dt_tracker" } \ No newline at end of file diff --git a/crates/dt_portable/src/lib.rs b/crates/dt_portable/src/lib.rs index 24258d6..e46bb8f 100644 --- a/crates/dt_portable/src/lib.rs +++ b/crates/dt_portable/src/lib.rs @@ -1,5 +1,4 @@ use dt_graph::used_by_graph::UsedByGraph; -use dt_tracker::ModuleSymbol; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; @@ -19,11 +18,14 @@ pub struct Portable { pub i18n_to_symbol: HashMap>>, // { - // ("module path 1", LocalVar("A")) => ["/route/path/x", "/route/path/y"] - // ("module path 1", LocalVar("B")) => ["/route/path/x"] - // ("module path 2", LocalVar("A")) => ["/route/path/z"] - // } - pub symbol_to_route: HashMap>, + // "module path 1" => { + // "Foo" => ["/route/path/x", "/route/path/y"] + // "Bar" => ["/route/path/x"] + // } + // "module path 22" => { + // "Baz" => ["/route/path/z"] + // } + pub symbol_to_route: HashMap>>, // { // "module path 1" => { @@ -39,7 +41,7 @@ impl Portable { pub fn new( project_root: String, i18n_to_symbol: HashMap>>, - symbol_to_route: HashMap>, + symbol_to_route: HashMap>>, used_by_graph: UsedByGraph, ) -> Self { Self { diff --git a/crates/dt_route/Cargo.toml b/crates/dt_route/Cargo.toml index 845b7af..ee0cafe 100644 --- a/crates/dt_route/Cargo.toml +++ b/crates/dt_route/Cargo.toml @@ -13,5 +13,3 @@ swc_ecma_parser = { workspace = true } dt_parser = { version = "0.1.0", path = "../dt_parser" } dt_path_resolver = { version = "0.1.0", path = "../dt_path_resolver" } -dt_test_utils = { version = "0.1.0", path = "../dt_test_utils" } -dt_tracker = { version = "0.1.0", path = "../dt_tracker" } \ No newline at end of file diff --git a/crates/dt_route/src/lib.rs b/crates/dt_route/src/lib.rs index c131b50..3c0a0da 100644 --- a/crates/dt_route/src/lib.rs +++ b/crates/dt_route/src/lib.rs @@ -1,7 +1,6 @@ use dt_parser::{ anonymous_default_export::SYMBOL_NAME_FOR_ANONYMOUS_DEFAULT_EXPORT, types::SymbolDependency, }; -use dt_tracker::{ModuleSymbol, TraceTarget}; use std::collections::{HashMap, HashSet}; use swc_core::ecma::{ ast::*, @@ -11,7 +10,7 @@ use swc_core::ecma::{ #[derive(Debug)] pub struct SymbolToRoutes { // one symbol can be used in multiple routes - pub table: HashMap>, + pub table: HashMap>>, } impl SymbolToRoutes { @@ -73,21 +72,17 @@ impl SymbolToRoutes { } fn aggregate(&mut self, module_path: &str, routes: Vec) { + let mut map = HashMap::new(); for route in routes { for symbol in route.depend_on { - let module_symbol: ModuleSymbol = - (module_path.to_owned(), TraceTarget::LocalVar(symbol)); - if !self.table.contains_key(&module_symbol) { - self.table - .insert(module_symbol, vec![route.path.to_owned()]); + if !map.contains_key(&symbol) { + map.insert(symbol, vec![route.path.to_owned()]); } else { - self.table - .get_mut(&module_symbol) - .unwrap() - .push(route.path.to_owned()); + map.get_mut(&symbol).unwrap().push(route.path.to_owned()); } } } + self.table.insert(module_path.to_owned(), map); } } @@ -210,12 +205,9 @@ mod tests { macro_rules! assert_symbol_to_routes_table { ($table:expr, $($symbol_name:expr => $expected_route_paths:expr),* $(,)?) => {{ + let map = $table.get(MOCK_MODULE_PATH).unwrap(); $( - let route_paths = $table.get( - &( - MOCK_MODULE_PATH.to_string(), - TraceTarget::LocalVar($symbol_name.to_string()) - )).unwrap(); + let route_paths = map.get($symbol_name).unwrap(); assert_eq!(route_paths.len(), $expected_route_paths.len()); for (i, expected) in $expected_route_paths.into_iter().enumerate() { assert_eq!(route_paths[i], expected); diff --git a/crates/dt_scheduler/tests/scheduler.rs b/crates/dt_scheduler/tests/scheduler.rs index 710e22a..4312ccd 100644 --- a/crates/dt_scheduler/tests/scheduler.rs +++ b/crates/dt_scheduler/tests/scheduler.rs @@ -54,7 +54,7 @@ fn topological_order() { assert!(not_parsed.contains(&namespace_import)); } - not_parsed.remove(&candidate); + assert!(not_parsed.remove(&candidate)); scheduler.mark_candidate_as_parsed(candidate); } assert_eq!(scheduler.get_total_remaining_candidate_count(), 0);