pub(crate) fn fix(failures: Vec<AssertEqFailure>) -> anyhow::Result<usize> {
    let mut assert_eqs_by_path = HashMap::<PathBuf, Vec<AssertEqLocation>>::new();
    let mut content_by_path = HashMap::<PathBuf, String>::new();
    let mut fixes_by_path = HashMap::<PathBuf, Vec<Fix>>::new();

    let crate_root = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
    let crate_root = Path::new(&crate_root);

    for failure in failures {
        let path = crate_root.join(&failure.path);
        if !content_by_path.contains_key(&path) {
            let content =
                fs::read_to_string(&path).with_context(|| format!("reading {:?}", path))?;
            content_by_path.insert(path.clone(), content);
        }
        let assert_eqs = assert_eqs_by_path.entry(path.clone()).or_insert_with(|| {
            let code = &content_by_path[&path];
            parse_code::find_assert_eqs(&code)
        });
        if let Some(assert_eq) = assert_eqs
            .iter()
            .find(|a| a.assert.overlaps_line(failure.line))
        {
            let fix = Fix {
                location: assert_eq.rhs.clone(),
                content: failure.actual,
            };
            fixes_by_path.entry(path.clone()).or_default().push(fix);
        }
    }

    let mut count = 0;
    for (path, fixes) in fixes_by_path.into_iter() {
        count += fixes.len();
        let content = &content_by_path[&path];
        let new_content = apply_fixes(content, fixes);
        fs::write(&path, new_content).with_context(|| format!("writing {:?}", path))?;
    }

    Ok(count)
}
/// Attempt to fix failures. Return count of fixes applied.
Attempt to fix failures. Return count of fixes applied.
Attempt to fix failures . Return count of fixes applied .
pub(crate) fn fix(failures: Vec<AssertEqFailure>) -> anyhow::Result<usize> {
    let mut assert_eqs_by_path = HashMap::<PathBuf, Vec<AssertEqLocation>>::new();
    let mut content_by_path = HashMap::<PathBuf, String>::new();
    let mut fixes_by_path = HashMap::<PathBuf, Vec<Fix>>::new();
    let crate_root = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
    let crate_root = Path::new(&crate_root);
    for failure in failures {
        let path = crate_root.join(&failure.path);
        if !content_by_path.contains_key(&path) {
            let content =
                fs::read_to_string(&path).with_context(|| format!("reading {:?}", path))?;
            content_by_path.insert(path.clone(), content);
        }
        let assert_eqs = assert_eqs_by_path.entry(path.clone()).or_insert_with(|| {
            let code = &content_by_path[&path];
            parse_code::find_assert_eqs(&code)
        });
        if let Some(assert_eq) = assert_eqs
            .iter()
            .find(|a| a.assert.overlaps_line(failure.line))
        {
            let fix = Fix {
                location: assert_eq.rhs.clone(),
                content: failure.actual,
            };
            fixes_by_path.entry(path.clone()).or_default().push(fix);
        }
    }
    let mut count = 0;
    for (path, fixes) in fixes_by_path.into_iter() {
        count += fixes.len();
        let content = &content_by_path[&path];
        let new_content = apply_fixes(content, fixes);
        fs::write(&path, new_content).with_context(|| format!("writing {:?}", path))?;
    }
    Ok(count)
}
pub ( crate ) fn fix ( failures : Vec < AssertEqFailure > ) -> anyhow :: Result < usize > { let mut assert_eqs_by_path = HashMap :: < PathBuf , Vec < AssertEqLocation > > :: new ( ) ; let mut content_by_path = HashMap :: < PathBuf , String > :: new ( ) ; let mut fixes_by_path = HashMap :: < PathBuf , Vec < Fix > > :: new ( ) ; let crate_root = env :: var ( " " ) . unwrap_or_default ( ) ; let crate_root = Path :: new ( & crate_root ) ; for failure in failures { let path = crate_root . join ( & failure . path ) ; if ! content_by_path . contains_key ( & path ) { let content = fs :: read_to_string ( & path ) . with_context ( | | format ! ( " " path ) ) ? ; content_by_path . insert ( path . clone ( ) , content ) ; } let assert_eqs = assert_eqs_by_path . entry ( path . clone ( ) ) . or_insert_with ( | | { let code = & content_by_path [ & path ] ; parse_code :: find_assert_eqs ( & code ) } ) ; if let Some ( assert_eq ) = assert_eqs . iter ( ) . find ( | a | a . assert . overlaps_line ( failure . line ) ) { let fix = Fix { location : assert_eq . rhs . clone ( ) , content : failure . actual , } ; fixes_by_path . entry ( path . clone ( ) ) . or_default ( ) . push ( fix ) ; } } let mut count = 0 ; for ( path , fixes ) in fixes_by_path . into_iter ( ) { count += fixes . len ( ) ; let content = & content_by_path [ & path ] ; let new_content = apply_fixes ( content , fixes ) ; fs :: write ( & path , new_content ) . with_context ( | | format ! ( " " path ) ) ? ; } Ok ( count ) }
Attempt to fix failures.
Attempt to fix failures .
failures
Vec<AssertEqFailure>
failures
Vec<AssertEqFailure>