use crate::db::queries::QueryBuilder;
use crate::types::{ExtractionError, ExtractionSeverity, FileRecord};
use sha2::{Digest, Sha256};
pub fn hash_content(content: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
format!("{:x}", hasher.finalize())
}
pub(super) fn extraction_error(
message: String,
file_path: Option<String>,
code: &str,
) -> ExtractionError {
ExtractionError {
message,
file_path,
line: None,
column: None,
severity: ExtractionSeverity::Error,
code: Some(code.to_owned()),
}
}
pub(super) fn normalize_slashes(path: &str) -> String {
path.replace('\\', "/")
}
pub(super) fn pattern_matches(pattern: &str, path: &str) -> bool {
let pattern = normalize_slashes(pattern)
.trim_start_matches('/')
.to_owned();
let path = normalize_slashes(path).trim_start_matches("./").to_owned();
if let Some(prefix) = pattern.strip_suffix("*/") {
return path.contains(prefix.trim_end_matches('*'));
}
if let Some(suffix) = pattern.strip_prefix('*') {
return path.ends_with(suffix.trim_end_matches('/'));
}
let pattern = pattern.trim_end_matches('/');
path == pattern
|| path.starts_with(&format!("{pattern}/"))
|| path.contains(&format!("/{pattern}/"))
}
pub(super) fn tracked_files_placeholder(_queries: &QueryBuilder<'_>) -> Vec<FileRecord> {
Vec::new()
}