eureka_manager_cli/commands/count/
cover.rs

1use clap::Args;
2use eureka_mmanager::{
3    files_dirs::messages::pull::cover::CoverListDataPullMessage,
4    prelude::{
5        CoverListDataPullFilterParams, GetManagerStateData, IntoParamedFilteredStream,
6        JoinPathAsyncTraits,
7    },
8};
9use mangadex_api_types_rust::Language;
10use tokio_stream::StreamExt;
11use uuid::Uuid;
12
13use crate::commands::{AsyncRun, AsyncRunContext};
14
15#[derive(Debug, Args)]
16pub struct CountCoverArgs {
17    #[arg(long = "manga")]
18    pub manga_ids: Vec<Uuid>,
19    #[arg(long = "uploader")]
20    pub uploader_ids: Vec<Uuid>,
21    #[arg(long = "local")]
22    pub locales: Vec<Language>,
23    #[arg(short, long)]
24    pub ids: bool,
25    #[arg(short, long)]
26    pub filename: bool,
27}
28
29impl CountCoverArgs {
30    fn to_params(&self) -> CoverListDataPullFilterParams {
31        CoverListDataPullFilterParams {
32            manga_ids: self.manga_ids.clone(),
33            uploader_ids: self.uploader_ids.clone(),
34            locales: self.locales.clone(),
35        }
36    }
37}
38
39impl AsyncRun for CountCoverArgs {
40    async fn run(&self, ctx: AsyncRunContext) -> anyhow::Result<()> {
41        let dir_options = ctx.manager.get_dir_options().await?;
42        let mut stream = dir_options
43            .send(CoverListDataPullMessage)
44            .await??
45            .to_filtered(self.to_params());
46
47        match (self.ids, self.filename) {
48            (true, false) => {
49                while let Some(cover) = stream.next().await {
50                    println!("{}", cover.id);
51                }
52            }
53            (true, true) => {
54                while let Some(cover) = stream.next().await {
55                    println!(
56                        "{} [{}]",
57                        cover.id,
58                        dir_options
59                            .join_covers_images(cover.attributes.file_name.clone())
60                            .await
61                            .ok()
62                            .and_then(|p| p.to_str().map(String::from))
63                            .unwrap_or(cover.attributes.file_name)
64                    );
65                }
66            }
67            (false, true) => {
68                while let Some(cover) = stream.next().await {
69                    println!(
70                        "[{}]",
71                        dir_options
72                            .join_covers_images(cover.attributes.file_name.clone())
73                            .await
74                            .ok()
75                            .and_then(|p| p.to_str().map(String::from))
76                            .unwrap_or(cover.attributes.file_name)
77                    );
78                }
79            }
80            (false, false) => {
81                println!(
82                    "Number of cover art available: {}",
83                    stream.fold(0usize, |count, _| count + 1).await
84                );
85            }
86        }
87        Ok(())
88    }
89}