use crate::cgp_patterns::ProviderRelationship;
use crate::diagnostic_db::DiagnosticEntry;
pub fn rank_by_causal_priority(_entries: &mut [&DiagnosticEntry]) {
}
pub fn is_root_cause(entry: &DiagnosticEntry) -> bool {
if entry.field_info.is_some() {
return true;
}
if !entry.provider_relationships.is_empty() {
return true;
}
false
}
pub fn is_transitive_failure(entry: &DiagnosticEntry, all_entries: &[&DiagnosticEntry]) -> bool {
if entry.field_info.is_some() {
return false;
}
for other in all_entries {
if entry_locations_match(entry, other) && other.field_info.is_some() {
return true;
}
}
false
}
fn entry_locations_match(a: &DiagnosticEntry, b: &DiagnosticEntry) -> bool {
if a.primary_spans.is_empty() || b.primary_spans.is_empty() {
return false;
}
for span_a in &a.primary_spans {
for span_b in &b.primary_spans {
if span_a.file_name == span_b.file_name
&& span_a.line_start == span_b.line_start
&& span_a.column_start == span_b.column_start
{
return true;
}
}
}
false
}
pub fn deduplicate_provider_relationships(
relationships: &[ProviderRelationship],
) -> Vec<ProviderRelationship> {
let mut deduped = Vec::new();
for rel in relationships {
let is_redundant = relationships.iter().any(|other| {
if rel == other {
return false; }
if other.component != rel.component || other.context != rel.context {
return false;
}
is_contained_type_parameter(&rel.provider_type, &other.provider_type)
});
if !is_redundant {
deduped.push(rel.clone());
}
}
deduped
}
fn is_contained_type_parameter(inner_type: &str, outer_type: &str) -> bool {
let patterns = [
format!("<{}>", inner_type),
format!("<{},", inner_type),
format!(", {}>", inner_type),
format!(", {},", inner_type),
format!("< {}", inner_type), format!("{} >", inner_type),
];
patterns.iter().any(|pattern| outer_type.contains(pattern))
}
pub fn deduplicate_delegation_notes(notes: &[String]) -> Vec<String> {
let mut deduped = Vec::new();
for note in notes {
if !deduped.contains(note) {
deduped.push(note.clone());
}
}
deduped
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_contained_type_parameter() {
assert!(is_contained_type_parameter(
"RectangleArea",
"ScaledArea<RectangleArea>"
));
assert!(is_contained_type_parameter("Foo", "Wrapper<Foo, Bar>"));
assert!(is_contained_type_parameter("Bar", "Wrapper<Foo, Bar>"));
assert!(!is_contained_type_parameter("Baz", "Wrapper<Foo, Bar>"));
assert!(!is_contained_type_parameter(
"Area",
"ScaledArea<RectangleArea>"
));
}
#[test]
fn test_deduplicate_provider_relationships() {
let relationships = vec![
ProviderRelationship {
provider_type: "RectangleArea".to_string(),
component: "AreaCalculatorComponent".to_string(),
context: "Rectangle".to_string(),
},
ProviderRelationship {
provider_type: "ScaledArea<RectangleArea>".to_string(),
component: "AreaCalculatorComponent".to_string(),
context: "Rectangle".to_string(),
},
];
let deduped = deduplicate_provider_relationships(&relationships);
assert_eq!(deduped.len(), 1);
assert_eq!(deduped[0].provider_type, "ScaledArea<RectangleArea>");
}
}