use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntegrityFile {
pub path: PathBuf,
pub length: u64,
}
impl IntegrityFile {
pub fn new(path: PathBuf, length: u64) -> Self {
Self { path, length }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntegrityIncompleteAction {
pub reset_piece_storage: bool,
pub file_allocation: Option<Vec<IntegrityFile>>,
}
impl IntegrityIncompleteAction {
pub fn new(
reset_piece_storage: bool,
hash_check_only: bool,
files: Vec<IntegrityFile>,
) -> Self {
Self {
reset_piece_storage,
file_allocation: (!hash_check_only).then_some(files),
}
}
pub fn apply_piece_storage(
&self,
piece_storage: &mut dyn crate::segment::piece_storage::PieceStorage,
) {
if self.reset_piece_storage {
piece_storage.on_download_incomplete();
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntegrityTrailingGarbageAction {
pub files: Vec<IntegrityFile>,
}
impl IntegrityTrailingGarbageAction {
pub fn new(files: Vec<IntegrityFile>) -> Self {
Self { files }
}
pub fn single_file(path: PathBuf, length: u64) -> Self {
Self::new(vec![IntegrityFile::new(path, length)])
}
pub async fn apply(&self) -> crate::error::Result<()> {
let files: Vec<_> = self
.files
.iter()
.map(|file| (file.path.clone(), file.length))
.collect();
super::man::cut_multi_file_trailing_garbage(&files).await
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct IntegrityFinishedAction {
pub file_allocation: Option<Vec<IntegrityFile>>,
pub run_completion_hook: bool,
}
impl IntegrityFinishedAction {
pub fn for_bt(
files: Vec<IntegrityFile>,
hash_check_only: bool,
hash_check_seed: bool,
run_completion_hook: bool,
) -> Self {
Self {
file_allocation: (!hash_check_only && hash_check_seed).then_some(files),
run_completion_hook,
}
}
}