From 2810c86b9e05b44cd4dd03ba3f160b62f4ee384a Mon Sep 17 00:00:00 2001 From: "Vitaly D." Date: Tue, 9 Jun 2026 04:27:22 +0300 Subject: [PATCH] refactor(repo-graph): move helpers into module --- src/core/repo_graph.rs | 40 ++---------------------------- src/core/repo_graph/builder.rs | 2 +- src/core/repo_graph/generic.rs | 3 ++- src/core/repo_graph/helpers.rs | 45 ++++++++++++++++++++++++++++++++++ src/core/repo_graph/impact.rs | 2 +- src/core/repo_graph/node.rs | 3 ++- src/core/repo_graph/rust.rs | 3 ++- 7 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 src/core/repo_graph/helpers.rs diff --git a/src/core/repo_graph.rs b/src/core/repo_graph.rs index 84c2a9c..15b9584 100644 --- a/src/core/repo_graph.rs +++ b/src/core/repo_graph.rs @@ -5,6 +5,7 @@ use toml::Value as TomlValue; mod builder; mod generic; mod go; +mod helpers; mod impact; mod node; mod python; @@ -12,6 +13,7 @@ mod rust; mod types; use builder::RepoGraphBuilder; +use helpers::{display_path, normalize_path}; pub use impact::analyze_impact; pub use types::*; @@ -65,36 +67,6 @@ fn detect_ignored_paths(root: &Path, builder: &mut RepoGraphBuilder) { } } -fn stable_id(prefix: &str, value: &str) -> String { - format!("{prefix}-{}", sanitize_id(value)) -} - -fn stable_relationship_id(kind: &RelationshipKind, src_id: &str, dst_id: &str) -> String { - format!( - "relationship-{}-{}-{}", - sanitize_id(&format!("{kind:?}")), - sanitize_id(src_id), - sanitize_id(dst_id) - ) -} - -fn sanitize_id(value: &str) -> String { - value - .chars() - .map(|character| { - if character.is_ascii_alphanumeric() { - character.to_ascii_lowercase() - } else { - '-' - } - }) - .collect::() - .split('-') - .filter(|part| !part.is_empty()) - .collect::>() - .join("-") -} - fn manifest_warning_category(message: &str) -> DetectionCategory { if message.starts_with("Failed to read") { DetectionCategory::UnreadableManifest @@ -109,11 +81,3 @@ fn read_toml(path: &Path) -> Result { .parse::() .map_err(|error| format!("Failed to parse {}: {error}", normalize_path(path))) } - -fn normalize_path(path: &Path) -> String { - path.to_string_lossy().replace('\\', "/") -} - -fn display_path(path: &Path) -> String { - normalize_path(path) -} diff --git a/src/core/repo_graph/builder.rs b/src/core/repo_graph/builder.rs index 8af24b6..ee98a16 100644 --- a/src/core/repo_graph/builder.rs +++ b/src/core/repo_graph/builder.rs @@ -1,7 +1,7 @@ use std::path::Path; +use super::helpers::{normalize_path, stable_id, stable_relationship_id}; use super::types::*; -use super::{normalize_path, stable_id, stable_relationship_id}; pub(super) struct RepoGraphBuilder { repo_root: String, diff --git a/src/core/repo_graph/generic.rs b/src/core/repo_graph/generic.rs index 8b6114e..2bf4809 100644 --- a/src/core/repo_graph/generic.rs +++ b/src/core/repo_graph/generic.rs @@ -2,8 +2,9 @@ use std::collections::BTreeSet; use std::fs; use std::path::{Path, PathBuf}; +use super::helpers::stable_id; use super::types::*; -use super::{stable_id, RepoGraphBuilder}; +use super::RepoGraphBuilder; pub(super) fn detect_generic(root: &Path, builder: &mut RepoGraphBuilder) { let makefile = root.join("Makefile"); diff --git a/src/core/repo_graph/helpers.rs b/src/core/repo_graph/helpers.rs new file mode 100644 index 0000000..42ebac6 --- /dev/null +++ b/src/core/repo_graph/helpers.rs @@ -0,0 +1,45 @@ +use std::path::Path; + +use super::RelationshipKind; + +pub(super) fn stable_id(prefix: &str, value: &str) -> String { + format!("{prefix}-{}", sanitize_id(value)) +} + +pub(super) fn stable_relationship_id( + kind: &RelationshipKind, + src_id: &str, + dst_id: &str, +) -> String { + format!( + "relationship-{}-{}-{}", + sanitize_id(&format!("{kind:?}")), + sanitize_id(src_id), + sanitize_id(dst_id) + ) +} + +pub(super) fn sanitize_id(value: &str) -> String { + value + .chars() + .map(|character| { + if character.is_ascii_alphanumeric() { + character.to_ascii_lowercase() + } else { + '-' + } + }) + .collect::() + .split('-') + .filter(|part| !part.is_empty()) + .collect::>() + .join("-") +} + +pub(super) fn normalize_path(path: &Path) -> String { + path.to_string_lossy().replace('\\', "/") +} + +pub(super) fn display_path(path: &Path) -> String { + normalize_path(path) +} diff --git a/src/core/repo_graph/impact.rs b/src/core/repo_graph/impact.rs index eb37d24..2ae022d 100644 --- a/src/core/repo_graph/impact.rs +++ b/src/core/repo_graph/impact.rs @@ -1,6 +1,6 @@ use std::collections::{BTreeMap, BTreeSet, VecDeque}; -use super::sanitize_id; +use super::helpers::sanitize_id; use super::types::*; pub fn analyze_impact(repo_graph: &RepoInspection, changed_files: I) -> ImpactReport diff --git a/src/core/repo_graph/node.rs b/src/core/repo_graph/node.rs index fddbe75..7c8c918 100644 --- a/src/core/repo_graph/node.rs +++ b/src/core/repo_graph/node.rs @@ -2,8 +2,9 @@ use serde_json::Value as JsonValue; use std::fs; use std::path::Path; +use super::helpers::normalize_path; use super::types::*; -use super::{manifest_warning_category, normalize_path, RepoGraphBuilder}; +use super::{manifest_warning_category, RepoGraphBuilder}; pub(super) fn detect_node(root: &Path, builder: &mut RepoGraphBuilder) { let package_json = root.join("package.json"); diff --git a/src/core/repo_graph/rust.rs b/src/core/repo_graph/rust.rs index 44999c6..97e23f0 100644 --- a/src/core/repo_graph/rust.rs +++ b/src/core/repo_graph/rust.rs @@ -2,8 +2,9 @@ use std::collections::{BTreeMap, BTreeSet}; use std::path::{Path, PathBuf}; use toml::Value as TomlValue; +use super::helpers::{normalize_path, stable_id}; use super::types::*; -use super::{manifest_warning_category, normalize_path, read_toml, stable_id, RepoGraphBuilder}; +use super::{manifest_warning_category, read_toml, RepoGraphBuilder}; struct CargoWorkspaceMember { relative_manifest: PathBuf,