Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions src/core/repo_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use toml::Value as TomlValue;
mod builder;
mod generic;
mod go;
mod helpers;
mod impact;
mod node;
mod python;
mod rust;
mod types;

use builder::RepoGraphBuilder;
use helpers::{display_path, normalize_path};

pub use impact::analyze_impact;
pub use types::*;
Expand Down Expand Up @@ -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::<String>()
.split('-')
.filter(|part| !part.is_empty())
.collect::<Vec<_>>()
.join("-")
}

fn manifest_warning_category(message: &str) -> DetectionCategory {
if message.starts_with("Failed to read") {
DetectionCategory::UnreadableManifest
Expand All @@ -109,11 +81,3 @@ fn read_toml(path: &Path) -> Result<TomlValue, String> {
.parse::<TomlValue>()
.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)
}
2 changes: 1 addition & 1 deletion src/core/repo_graph/builder.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/core/repo_graph/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
45 changes: 45 additions & 0 deletions src/core/repo_graph/helpers.rs
Original file line number Diff line number Diff line change
@@ -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::<String>()
.split('-')
.filter(|part| !part.is_empty())
.collect::<Vec<_>>()
.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)
}
2 changes: 1 addition & 1 deletion src/core/repo_graph/impact.rs
Original file line number Diff line number Diff line change
@@ -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<I, S>(repo_graph: &RepoInspection, changed_files: I) -> ImpactReport
Expand Down
3 changes: 2 additions & 1 deletion src/core/repo_graph/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/core/repo_graph/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down