Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Sep 14, 2023
1 parent 2f2d8fd commit 5f7a1be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
34 changes: 19 additions & 15 deletions src/analytics/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ use crate::{file::webx::WXModule, reporting::error::{exit_error, ERROR_CIRCULAR_

type DependencyTree = HashMap<PathBuf, Vec<PathBuf>>;

fn detect_circular_dependencies(tree: &DependencyTree) -> Vec<PathBuf> {
let mut circular_dependencies = Vec::new();
for (_, dependents) in tree {
for dependent in dependents {
if tree.contains_key(dependent) {
circular_dependencies.push(dependent.clone());
}
}
}
circular_dependencies
}

/// Construct a dependency tree from a list of WebX files.
/// The tree is a hashmap where the keys are the dependencies and the values are the files that
/// depend on them.
Expand All @@ -41,9 +29,19 @@ fn construct_dependency_tree(files: &Vec<WXModule>) -> DependencyTree {
tree
}

/// Analyse the dependencies of a list of WebX modules.
/// If a circular dependency is detected, an error is reported and the program exits.
pub fn analyse_module_deps(modules: &Vec<WXModule>) {
fn detect_circular_dependencies(tree: &DependencyTree) -> Vec<PathBuf> {
let mut circular_dependencies = Vec::new();
for (_, dependents) in tree {
for dependent in dependents {
if tree.contains_key(dependent) {
circular_dependencies.push(dependent.clone());
}
}
}
circular_dependencies
}

fn analyse_circle_dependencies(modules: &Vec<WXModule>) {
let dependency_tree = construct_dependency_tree(modules);
let circular_dependencies = detect_circular_dependencies(&dependency_tree);
if !circular_dependencies.is_empty() {
Expand All @@ -55,4 +53,10 @@ pub fn analyse_module_deps(modules: &Vec<WXModule>) {
ERROR_CIRCULAR_DEPENDENCY,
);
}
}

/// Analyse the dependencies of a list of WebX modules.
/// If a circular dependency is detected, an error is reported and the program exits.
pub fn analyse_module_deps(modules: &Vec<WXModule>) {
analyse_circle_dependencies(modules);
}
11 changes: 9 additions & 2 deletions src/analytics/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn extract_duplicate_routes(routes: &FlatRoutes) -> Vec<String> {
.collect()
}

pub fn analyse_module_routes(modules: &Vec<WXModule>) {
fn analyse_duplicate_routes(modules: &Vec<WXModule>) {
let routes = extract_flat_routes(modules);
let duplicate_routes = extract_duplicate_routes(&routes);
if !duplicate_routes.is_empty() {
Expand All @@ -64,7 +64,14 @@ pub fn analyse_module_routes(modules: &Vec<WXModule>) {
}
}

fn analyse_invalid_routes(modules: &Vec<WXModule>) {
}

pub fn analyse_module_routes(modules: &Vec<WXModule>) {
analyse_duplicate_routes(modules);
analyse_invalid_routes(modules);
}

// Route verification, check for:
// - duplicate route (paths and methods)
// - invalid route combinations (e.g. GET + body)
// - return type for each route (HTML, JSON, or unknown)

0 comments on commit 5f7a1be

Please sign in to comment.