diff options
author | Simon Garrelou <simon.garrelou@gmail.com> | 2023-06-18 20:50:52 +0200 |
---|---|---|
committer | Simon Garrelou <simon.garrelou@gmail.com> | 2023-06-18 20:50:52 +0200 |
commit | b54631d9c18d5b2c3fdefa954a30aca96da4f0dd (patch) | |
tree | 4672cb753932f3dfa328364547e494c83118fa0a | |
parent | f9ead75712304f1138dd46fc4cdaf07667e660ab (diff) | |
download | rss-b54631d9c18d5b2c3fdefa954a30aca96da4f0dd.tar.gz rss-b54631d9c18d5b2c3fdefa954a30aca96da4f0dd.zip |
add 'take'
-rw-r--r-- | src/main.rs | 28 | ||||
-rw-r--r-- | src/take.rs | 10 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 765c408..0bb6df1 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -5,6 +5,7 @@ use std::process::exit; | |||
5 | 5 | ||
6 | mod fetch; | 6 | mod fetch; |
7 | mod sort; | 7 | mod sort; |
8 | mod take; | ||
8 | 9 | ||
9 | #[derive(Parser)] | 10 | #[derive(Parser)] |
10 | struct Args { | 11 | struct Args { |
@@ -24,7 +25,10 @@ enum Commands { | |||
24 | 25 | ||
25 | #[arg(short, long)] | 26 | #[arg(short, long)] |
26 | order: Option<Order> | 27 | order: Option<Order> |
27 | } | 28 | }, |
29 | |||
30 | /// Gets the first N items | ||
31 | Take { n: usize } | ||
28 | } | 32 | } |
29 | 33 | ||
30 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] | 34 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] |
@@ -66,7 +70,7 @@ async fn main() { | |||
66 | if order == Order::Desc { | 70 | if order == Order::Desc { |
67 | sorted.items.reverse(); | 71 | sorted.items.reverse(); |
68 | } | 72 | } |
69 | 73 | ||
70 | sorted.pretty_write_to(std::io::stdout(), b' ', 2).unwrap(); | 74 | sorted.pretty_write_to(std::io::stdout(), b' ', 2).unwrap(); |
71 | }, | 75 | }, |
72 | Err(e) => { | 76 | Err(e) => { |
@@ -80,6 +84,26 @@ async fn main() { | |||
80 | exit(1); | 84 | exit(1); |
81 | } | 85 | } |
82 | } | 86 | } |
87 | }, | ||
88 | |||
89 | Commands::Take { n } => { | ||
90 | match Channel::read_from(std::io::stdin().lock()) { | ||
91 | Ok(c) => { | ||
92 | match take::run(c, n) { | ||
93 | Ok(c) => { | ||
94 | c.pretty_write_to(std::io::stdout(), b' ', 2).unwrap(); | ||
95 | }, | ||
96 | Err(e) => { | ||
97 | eprintln!("Error with 'sort': {:?}", e); | ||
98 | exit(1); | ||
99 | } | ||
100 | } | ||
101 | }, | ||
102 | Err(e) => { | ||
103 | eprintln!("Could not parse RSS: {:?}", e); | ||
104 | exit(1); | ||
105 | } | ||
106 | } | ||
83 | } | 107 | } |
84 | } | 108 | } |
85 | } | 109 | } |
diff --git a/src/take.rs b/src/take.rs new file mode 100644 index 0000000..eed7ad1 --- /dev/null +++ b/src/take.rs | |||
@@ -0,0 +1,10 @@ | |||
1 | use anyhow::Result; | ||
2 | use rss::Channel; | ||
3 | |||
4 | pub fn run(channel: Channel, n: usize) -> Result<Channel> { | ||
5 | let mut new_channel = channel.clone(); | ||
6 | |||
7 | new_channel.items.truncate(n); | ||
8 | |||
9 | Ok(new_channel) | ||
10 | } \ No newline at end of file | ||