eureka_manager_cli/commands/count/
cover.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use clap::Args;
use eureka_mmanager::{
    files_dirs::messages::pull::cover::CoverListDataPullMessage,
    prelude::{
        CoverListDataPullFilterParams, GetManagerStateData, IntoParamedFilteredStream,
        JoinPathAsyncTraits,
    },
};
use mangadex_api_types_rust::Language;
use tokio_stream::StreamExt;
use uuid::Uuid;

use crate::commands::AsyncRun;

#[derive(Debug, Args)]
pub struct CountCoverArgs {
    #[arg(long = "manga")]
    pub manga_ids: Vec<Uuid>,
    #[arg(long = "uploader")]
    pub uploader_ids: Vec<Uuid>,
    #[arg(long = "local")]
    pub locales: Vec<Language>,
    #[arg(short, long)]
    pub ids: bool,
    #[arg(short, long)]
    pub filename: bool,
}

impl CountCoverArgs {
    fn to_params(&self) -> CoverListDataPullFilterParams {
        CoverListDataPullFilterParams {
            manga_ids: self.manga_ids.clone(),
            uploader_ids: self.uploader_ids.clone(),
            locales: self.locales.clone(),
        }
    }
}

impl AsyncRun for CountCoverArgs {
    async fn run(
        &self,
        manager: actix::Addr<eureka_mmanager::DownloadManager>,
    ) -> anyhow::Result<()> {
        let dir_options = manager.get_dir_options().await?;
        let mut stream = dir_options
            .send(CoverListDataPullMessage)
            .await??
            .to_filtered(self.to_params());

        match (self.ids, self.filename) {
            (true, false) => {
                while let Some(cover) = stream.next().await {
                    println!("{}", cover.id);
                }
            }
            (true, true) => {
                while let Some(cover) = stream.next().await {
                    println!(
                        "{} [{}]",
                        cover.id,
                        dir_options
                            .join_covers_images(cover.attributes.file_name.clone())
                            .await
                            .ok()
                            .and_then(|p| p.to_str().map(String::from))
                            .unwrap_or(cover.attributes.file_name)
                    );
                }
            }
            (false, true) => {
                while let Some(cover) = stream.next().await {
                    println!(
                        "[{}]",
                        dir_options
                            .join_covers_images(cover.attributes.file_name.clone())
                            .await
                            .ok()
                            .and_then(|p| p.to_str().map(String::from))
                            .unwrap_or(cover.attributes.file_name)
                    );
                }
            }
            (false, false) => {
                println!(
                    "Number of cover art available: {}",
                    stream.fold(0usize, |count, _| count + 1).await
                );
            }
        }
        Ok(())
    }
}