eureka_manager_cli/commands/delete/
chapter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};

use clap::Args;
use eureka_mmanager::prelude::{DeleteDataAsyncTrait, GetManagerStateData};
use log::info;
use uuid::Uuid;

use crate::commands::AsyncRun;

#[derive(Debug, Args)]
pub struct ChapterDeleteArgs {
    /// Chapter ids
    pub ids: Vec<Uuid>,
    #[arg(long)]
    pub id_text_file: Vec<PathBuf>,
}

impl ChapterDeleteArgs {
    pub fn get_ids(&self) -> Vec<Uuid> {
        let mut ids = self.ids.clone();
        self.id_text_file
            .iter()
            .map(|e| (e, File::open(e)))
            .flat_map(|(path, res)| match res {
                Ok(file) => Some(id_list_txt_reader::IdListTxtReader::new(BufReader::new(
                    file,
                ))),
                Err(err) => {
                    log::error!("Cannot open the {} file: {}", path.to_string_lossy(), err);
                    None
                }
            })
            .flat_map(|file| file.flat_map(|s| Uuid::from_str(&s)))
            .for_each(|id| {
                ids.push(id);
            });
        ids.dedup();
        ids
    }
}

impl AsyncRun for ChapterDeleteArgs {
    async fn run(
        &self,
        manager: actix::Addr<eureka_mmanager::DownloadManager>,
    ) -> anyhow::Result<()> {
        let ids = self.get_ids();
        info!("Deleting {} chapter", ids.len());
        let dir_option = manager.get_dir_options().await?;
        for id in &ids {
            info!("Deleting chapter {}", id);
            dir_option.delete_chapter(*id).await?;
            info!("Deleted chapter {}", id);
        }
        Ok(())
    }
}