Skip to content

Commit

Permalink
feat: make SymbolToRoute table serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlin1228 committed Oct 5, 2024
1 parent f21993d commit 070515f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/api_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
struct AppState {
project_root: String,
i18n_to_symbol: HashMap<String, HashMap<String, HashSet<String>>>,
symbol_to_route: HashMap<String, HashMap<String, Vec<String>>>,
used_by_graph: UsedByGraph,
}

Expand All @@ -32,6 +33,8 @@ async fn search(
) -> Result<web::Json<SearchResponse>> {
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) => {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion crates/dt_portable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
16 changes: 9 additions & 7 deletions crates/dt_portable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use dt_graph::used_by_graph::UsedByGraph;
use dt_tracker::ModuleSymbol;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

Expand All @@ -19,11 +18,14 @@ pub struct Portable {
pub i18n_to_symbol: HashMap<String, HashMap<String, HashSet<String>>>,

// {
// ("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<ModuleSymbol, Vec<String>>,
// "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<String, HashMap<String, Vec<String>>>,

// {
// "module path 1" => {
Expand All @@ -39,7 +41,7 @@ impl Portable {
pub fn new(
project_root: String,
i18n_to_symbol: HashMap<String, HashMap<String, HashSet<String>>>,
symbol_to_route: HashMap<ModuleSymbol, Vec<String>>,
symbol_to_route: HashMap<String, HashMap<String, Vec<String>>>,
used_by_graph: UsedByGraph,
) -> Self {
Self {
Expand Down
2 changes: 0 additions & 2 deletions crates/dt_route/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
24 changes: 8 additions & 16 deletions crates/dt_route/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*,
Expand All @@ -11,7 +10,7 @@ use swc_core::ecma::{
#[derive(Debug)]
pub struct SymbolToRoutes {
// one symbol can be used in multiple routes
pub table: HashMap<ModuleSymbol, Vec<String>>,
pub table: HashMap<String, HashMap<String, Vec<String>>>,
}

impl SymbolToRoutes {
Expand Down Expand Up @@ -73,21 +72,17 @@ impl SymbolToRoutes {
}

fn aggregate(&mut self, module_path: &str, routes: Vec<Route>) {
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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/dt_scheduler/tests/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 070515f

Please sign in to comment.