summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 48617b2be647255a3ba35f3521cfb23980a415fd (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use chrono::{naive::Days, prelude::*};
use clap::{Args, Parser, Subcommand, ValueEnum};
use syn::manager::Manager;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Subcommand)]
enum Command {
    /// track a new feed
    Add(AddCommand),
    /// list all entries in reverse chronological order
    ListEntries(ListEntriesCommand),
    /// list all feeds in reverse chronological order
    ListFeeds,
    /// refresh feeds
    Pull(PullCommand),
}

impl Default for Command {
    fn default() -> Self {
        Self::ListEntries(ListEntriesCommand { cutoff: None })
    }
}

#[derive(Args)]
struct AddCommand {
    urls: Vec<String>,
}

#[derive(Args)]
struct ListEntriesCommand {
    #[arg(value_name = "CUTOFF")]
    cutoff: Option<u64>,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum ListTarget {
    Feeds,
    Entries,
}

#[derive(Args)]
struct PullCommand {
    target: Option<String>,
}

pub trait PrintResult {
    fn print(&self);
}

impl<T, E> PrintResult for Result<T, E>
where
    T: std::fmt::Display,
    E: std::fmt::Display,
{
    fn print(&self) {
        match self {
            Ok(ok) => println!("{ok}"),
            Err(err) => eprintln!("{err}"),
        }
    }
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let opts = Cli::parse();
    match &opts.command.unwrap_or_default() {
        Command::Add(AddCommand { urls }) => {
            let mut manager = Manager::load().unwrap_or_else(|e| {
                eprintln!("{e}");
                Manager::default()
            });

            let (status, errors): (Vec<_>, Vec<_>) = manager
                .add_feeds_and_pull(&urls)
                .await
                .into_iter()
                .partition(Result::is_ok);
            status.iter().for_each(PrintResult::print);
            errors.iter().for_each(PrintResult::print);
            manager.store().print();
        }
        Command::ListFeeds => {
            let manager = Manager::load().unwrap_or_else(|e| {
                eprintln!("{e}");
                Manager::default()
            });
            manager.list_feeds().for_each(|f| println!("{f}"));
        }
        Command::ListEntries(ListEntriesCommand { cutoff }) => {
            let manager = Manager::load().unwrap_or_else(|e| {
                eprintln!("{e}");
                Manager::default()
            });
            manager
                .list_entries()
                .filter(|entry| {
                    cutoff
                        .map(|c| Utc::now() - Days::new(c) <= entry.published)
                        .unwrap_or(true)
                })
                .for_each(|f| println!("{f}"));
        }
        Command::Pull(PullCommand { .. }) => {
            let mut manager = Manager::load().unwrap_or_else(|e| {
                eprintln!("{e}");
                Manager::default()
            });
            let (status, errors): (Vec<_>, Vec<_>) =
                manager.pull().await.into_iter().partition(Result::is_ok);
            status
                .into_iter()
                .map(Result::unwrap)
                .filter(|s| !s.is_empty())
                .for_each(|s| println!("{s}"));
            errors.iter().for_each(PrintResult::print);
            manager.store().print();
        }
    }
}