aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Garrelou <simon.garrelou@gmail.com>2023-07-11 20:46:17 +0200
committerSimon Garrelou <simon.garrelou@gmail.com>2023-07-11 20:46:17 +0200
commitf39c549d8b1530756b3cd70b022327074db1b57b (patch)
treee13089e19f546c07ca82d7ae2d3540081128f358
parent1d43cd353641825865f279d79179447c3e06be08 (diff)
downloadrss-f39c549d8b1530756b3cd70b022327074db1b57b.tar.gz
rss-f39c549d8b1530756b3cd70b022327074db1b57b.zip
Add "merge" function
-rw-r--r--src/main.rs22
-rw-r--r--src/merge.rs14
2 files changed, 36 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 2c53729..61c3baa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@ use rss::Channel;
3use std::process::exit; 3use std::process::exit;
4 4
5mod fetch; 5mod fetch;
6mod merge;
6mod sort; 7mod sort;
7mod take; 8mod take;
8mod filter; 9mod filter;
@@ -19,6 +20,9 @@ enum Commands {
19 /// Fetches a feed from a URL 20 /// Fetches a feed from a URL
20 Fetch { url: String }, 21 Fetch { url: String },
21 22
23 /// Merge merges the items of the feed contained in the given file with the feed in standard input
24 Merge { filename: String },
25
22 /// Sorts a feed 26 /// Sorts a feed
23 Sort { 27 Sort {
24 #[arg(short, long)] 28 #[arg(short, long)]
@@ -87,6 +91,24 @@ fn main() {
87 } 91 }
88 }, 92 },
89 93
94 Commands::Merge { filename } => {
95 match Channel::read_from(std::io::stdin().lock()) {
96 Ok(c) => {
97 match merge::run(c, filename) {
98 Ok(c) => {
99 c.pretty_write_to(std::io::stdout(), b' ', 2).unwrap();
100 },
101 Err(e) => {
102 crash_and_burn(&e);
103 }
104 }
105 },
106 Err(e) => {
107 crash_and_burn(&e);
108 }
109 }
110 },
111
90 Commands::Sort { by, order } => { 112 Commands::Sort { by, order } => {
91 match Channel::read_from(std::io::stdin().lock()) { 113 match Channel::read_from(std::io::stdin().lock()) {
92 Ok(c) => { 114 Ok(c) => {
diff --git a/src/merge.rs b/src/merge.rs
new file mode 100644
index 0000000..5567d29
--- /dev/null
+++ b/src/merge.rs
@@ -0,0 +1,14 @@
1use std::str::FromStr;
2
3use rss::Channel;
4use anyhow::{Result, Context};
5
6pub fn run(c: Channel, filename: String) -> Result<Channel> {
7 let contents = std::fs::read_to_string(&filename)?;
8 let mut other_channel = rss::Channel::from_str(&contents).context(format!("error parsing {}", filename))?;
9
10 let mut c = c.clone();
11 c.items.append(&mut other_channel.items);
12
13 Ok(c)
14} \ No newline at end of file