Skip to main content

validate_refactor

Function validate_refactor 

Source
pub fn validate_refactor(diff: &GraphDiffResult) -> RefactorValidation
Expand description

Validates whether a graph diff represents a safe refactor.

Applies validation heuristics to determine if a code change is safe:

  • No nodes removed: Nodes removed means breaking changes (existing code may reference them)
  • Similarity threshold: Score >= 0.5 (moderate), >= 0.8 (very similar)
  • Isomorphism check: Identical structure = safe
  • Edge changes: Warnings for removed edges (may affect control flow)

§Validation Rules

§Breaking Changes (is_safe = false)

  1. Nodes removed - always breaking (breaks existing references)
  2. Similarity < 0.5 - significant structural changes

§Warnings (not breaking)

  1. Similarity < 0.8 - moderate changes, review recommended
  2. Edges removed - may break control flow or dependencies
  3. Isomorphic - informational (structure preserved)

§Arguments

  • diff - GraphDiffResult from graph_diff() or graph_diff_with_progress()

§Returns

RefactorValidation with:

  • is_safe: True if no breaking changes detected
  • breaking_changes: List of breaking change descriptions
  • warnings: List of warnings (not breaking but noteworthy)

§Example

use sqlitegraph::{algo::graph_diff, algo::validate_refactor, SqliteGraph};

let graph_v1 = SqliteGraph::open_in_memory()?;
let graph_v2 = SqliteGraph::open_in_memory()?;
// ... build graphs ...

let diff = graph_diff(&graph_v1, &graph_v2)?;
let validation = validate_refactor(&diff);

if validation.is_safe {
    println!("✓ Refactor validated successfully");
    if !validation.warnings.is_empty() {
        println!("Warnings:");
        for warning in &validation.warnings {
            println!("  - {}", warning);
    }
}
} else {
    println!("✗ Refactor validation failed:");
    for change in &validation.breaking_changes {
        println!("  - {}", change);
    }
    println!("\nConsider reviewing these changes before deploying.");
}

§Use Cases

  • Pre-commit validation: Check if refactor is safe before committing
  • CI/CD gates: Automated validation in continuous integration
  • Code review assist: Highlight potential issues for reviewers
  • Refactor confidence: Verify optimizer equivalence