fn delete_files(vector: &[FileEntry], delete_method: &DeleteMethod, text_messages: &mut Messages, dryrun: bool) -> (u64, usize, usize) {
    assert!(vector.len() > 1, "Vector length must be bigger than 1(This should be done in previous steps).");
    let mut gained_space: u64 = 0;
    let mut removed_files: usize = 0;
    let mut failed_to_remove_files: usize = 0;
    let mut values = vector.iter().enumerate();
    let q_index = match delete_method {
        DeleteMethod::OneOldest | DeleteMethod::AllExceptNewest => values.max_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
        DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::HardLink => values.min_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
        DeleteMethod::None => values.next(),
    };
    let q_index = q_index.map(|t| t.0).unwrap_or(0);
    let n = match delete_method {
        DeleteMethod::OneNewest | DeleteMethod::OneOldest => 1,
        DeleteMethod::AllExceptNewest | DeleteMethod::AllExceptOldest | DeleteMethod::None | DeleteMethod::HardLink => usize::MAX,
    };
    for (index, file) in vector.iter().enumerate() {
        if q_index == index {
            continue;
        } else if removed_files + failed_to_remove_files >= n {
            break;
        }

        let r = match delete_method {
            DeleteMethod::OneOldest | DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::AllExceptNewest => {
                if dryrun {
                    Ok(Some(format!("Delete {}", file.path.display())))
                } else {
                    fs::remove_file(&file.path).map(|_| None)
                }
            }
            DeleteMethod::HardLink => {
                let src = &vector[q_index].path;
                if dryrun {
                    Ok(Some(format!("Replace file {} with hard link to {}", file.path.display(), src.display())))
                } else {
                    make_hard_link(src, &file.path).map(|_| None)
                }
            }
            DeleteMethod::None => Ok(None),
        };

        match r {
            Err(e) => {
                failed_to_remove_files += 1;
                text_messages.warnings.push(format!("Failed to remove {} ({})", file.path.display(), e));
            }
            Ok(Some(msg)) => {
                text_messages.messages.push(msg);
                removed_files += 1;
                gained_space += file.size;
            }
            Ok(None) => {
                removed_files += 1;
                gained_space += file.size;
            }
        }
    }
    (gained_space, removed_files, failed_to_remove_files)
}
/// Functions to remove slice(vector) of files with provided method
/// Returns size of removed elements, number of deleted and failed to delete files and modified warning list
Functions to remove slice(vector) of files with provided method
Returns size of removed elements, number of deleted and failed to delete files and modified warning list
Functions to remove slice ( vector ) of files with provided method Returns size of removed elements number of deleted and failed to delete files and modified warning list
fn delete_files(vector: &[FileEntry], delete_method: &DeleteMethod, text_messages: &mut Messages, dryrun: bool) -> (u64, usize, usize) {
    assert!(vector.len() > 1, "Vector length must be bigger than 1(This should be done in previous steps).");
    let mut gained_space: u64 = 0;
    let mut removed_files: usize = 0;
    let mut failed_to_remove_files: usize = 0;
    let mut values = vector.iter().enumerate();
    let q_index = match delete_method {
        DeleteMethod::OneOldest | DeleteMethod::AllExceptNewest => values.max_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
        DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::HardLink => values.min_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
        DeleteMethod::None => values.next(),
    };
    let q_index = q_index.map(|t| t.0).unwrap_or(0);
    let n = match delete_method {
        DeleteMethod::OneNewest | DeleteMethod::OneOldest => 1,
        DeleteMethod::AllExceptNewest | DeleteMethod::AllExceptOldest | DeleteMethod::None | DeleteMethod::HardLink => usize::MAX,
    };
    for (index, file) in vector.iter().enumerate() {
        if q_index == index {
            continue;
        } else if removed_files + failed_to_remove_files >= n {
            break;
        }
        let r = match delete_method {
            DeleteMethod::OneOldest | DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::AllExceptNewest => {
                if dryrun {
                    Ok(Some(format!("Delete {}", file.path.display())))
                } else {
                    fs::remove_file(&file.path).map(|_| None)
                }
            }
            DeleteMethod::HardLink => {
                let src = &vector[q_index].path;
                if dryrun {
                    Ok(Some(format!("Replace file {} with hard link to {}", file.path.display(), src.display())))
                } else {
                    make_hard_link(src, &file.path).map(|_| None)
                }
            }
            DeleteMethod::None => Ok(None),
        };
        match r {
            Err(e) => {
                failed_to_remove_files += 1;
                text_messages.warnings.push(format!("Failed to remove {} ({})", file.path.display(), e));
            }
            Ok(Some(msg)) => {
                text_messages.messages.push(msg);
                removed_files += 1;
                gained_space += file.size;
            }
            Ok(None) => {
                removed_files += 1;
                gained_space += file.size;
            }
        }
    }
    (gained_space, removed_files, failed_to_remove_files)
}
fn delete_files ( vector : & [ FileEntry ] , delete_method : & DeleteMethod , text_messages : & mut Messages , dryrun : bool ) -> ( u64 , usize , usize ) { assert ! ( vector len ( ) 1 " " ) ; let mut gained_space : u64 = 0 ; let mut removed_files : usize = 0 ; let mut failed_to_remove_files : usize = 0 ; let mut values = vector . iter ( ) . enumerate ( ) ; let q_index = match delete_method { DeleteMethod :: OneOldest | DeleteMethod :: AllExceptNewest => values . max_by ( | ( _ , l ) , ( _ , r ) | l . modified_date . cmp ( & r . modified_date ) ) , DeleteMethod :: OneNewest | DeleteMethod :: AllExceptOldest | DeleteMethod :: HardLink => values . min_by ( | ( _ , l ) , ( _ , r ) | l . modified_date . cmp ( & r . modified_date ) ) , DeleteMethod :: None => values . next ( ) , } ; let q_index = q_index . map ( | t | t . 0 ) . unwrap_or ( 0 ) ; let n = match delete_method { DeleteMethod :: OneNewest | DeleteMethod :: OneOldest => 1 , DeleteMethod :: AllExceptNewest | DeleteMethod :: AllExceptOldest | DeleteMethod :: None | DeleteMethod :: HardLink => usize :: MAX , } ; for ( index , file ) in vector . iter ( ) . enumerate ( ) { if q_index == index { continue ; } else if removed_files + failed_to_remove_files >= n { break ; } let r = match delete_method { DeleteMethod :: OneOldest | DeleteMethod :: OneNewest | DeleteMethod :: AllExceptOldest | DeleteMethod :: AllExceptNewest => { if dryrun { Ok ( Some ( format ! ( " " file path display ( ) ) ) ) } else { fs :: remove_file ( & file . path ) . map ( | _ | None ) } } DeleteMethod :: HardLink => { let src = & vector [ q_index ] . path ; if dryrun { Ok ( Some ( format ! ( " " file path display ( ) src display ( ) ) ) ) } else { make_hard_link ( src , & file . path ) . map ( | _ | None ) } } DeleteMethod :: None => Ok ( None ) , } ; match r { Err ( e ) => { failed_to_remove_files += 1 ; text_messages . warnings . push ( format ! ( " " file path display ( ) e ) ) ; } Ok ( Some ( msg ) ) => { text_messages . messages . push ( msg ) ; removed_files += 1 ; gained_space += file . size ; } Ok ( None ) => { removed_files += 1 ; gained_space += file . size ; } } } ( gained_space , removed_files , failed_to_remove_files ) }
Functions to remove slice(vector) of files with provided method
Returns size of removed elements, number of deleted and failed to delete files and modified warning list
Functions to remove slice ( vector ) of files with provided method Returns size of removed elements number of deleted and failed to delete files and modified warning list
vector
&[FileEntry]
delete_method
&DeleteMethod
text_messages
&mut Messages
dryrun
bool
vector
&[FileEntry]
delete_method
&DeleteMethod
text_messages
&mut Messages
dryrun
bool