use crate::config::CallerCalleeConfig;
pub(crate) fn should_include_in_output(function_name: &str, config: &CallerCalleeConfig) -> bool {
if !config.show_std_lib && is_standard_library_call(function_name) {
return false;
}
if !config.show_external && is_external_crate_call(function_name) {
return false;
}
true
}
fn is_standard_library_call(function_name: &str) -> bool {
function_name.starts_with("std::")
|| function_name.starts_with("core::")
|| function_name.starts_with("alloc::")
|| function_name == "println"
|| function_name == "print"
|| function_name == "eprintln"
|| function_name == "eprint"
|| function_name == "write"
|| function_name == "writeln"
|| function_name == "format"
|| function_name == "panic"
|| function_name == "assert"
|| function_name == "debug_assert"
}
fn is_external_crate_call(function_name: &str) -> bool {
function_name.contains("::")
&& !function_name.starts_with("std::")
&& !function_name.starts_with("core::")
&& !function_name.starts_with("alloc::")
&& !function_name.starts_with("crate::")
}
pub(crate) fn filter_dependencies(names: &[String], config: &CallerCalleeConfig) -> Vec<String> {
names
.iter()
.filter(|name| should_include_in_output(name, config))
.cloned()
.collect()
}
pub(crate) fn format_function_reference(function_name: &str) -> String {
if function_name.contains("::") {
let parts: Vec<&str> = function_name.split("::").collect();
if parts.len() > 2 {
format!("{}::{}", parts[parts.len() - 2], parts[parts.len() - 1])
} else {
function_name.to_string()
}
} else {
function_name.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_dependencies() {
let config = CallerCalleeConfig {
max_callers: 5,
max_callees: 5,
show_std_lib: false,
show_external: false,
};
let names = vec!["my_func".to_string(), "std::vec::Vec".to_string()];
let filtered = filter_dependencies(&names, &config);
assert_eq!(filtered, vec!["my_func"]);
}
#[test]
fn test_format_function_reference() {
assert_eq!(format_function_reference("my_function"), "my_function");
assert_eq!(format_function_reference("crate::helper"), "crate::helper");
assert_eq!(
format_function_reference("crate::utils::io::helper::read_file"),
"helper::read_file"
);
assert_eq!(
format_function_reference("std::collections::HashMap"),
"collections::HashMap"
);
}
}