diff --git a/Cargo.toml b/Cargo.toml index e05a9c5..0edc1b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,6 @@ edition = "2021" reqwest = { version = "0.11.12", features = ["blocking"] } rss = "2.0.1" chrono = "0.4.23" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.40" +toml = "0.4.2" diff --git a/src/main.rs b/src/main.rs index aa39a7e..471abaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ use std::io::Write; use std::io::copy; use rss::Channel; -#[derive(Debug)] +use serde::{Serialize, Deserialize}; +use toml; + +#[derive(Debug, Serialize, Deserialize)] struct Config { url: String, author: String, @@ -15,6 +18,23 @@ struct Config { description: String, } +impl Config { + pub fn default() -> Config { + Config { + url: "https://mastodon.bofhers.es/tags/micropost.rss".to_string(), + author: "oscarmlage".to_string(), + path: "temp/".to_string(), + version: "0.0.1".to_string(), + description: "Just another tool to convert RSS from Mastodon to Markdown".to_string(), + } + } + pub fn parse(&mut self, config_file: String) -> Config { + let i = std::fs::read_to_string(config_file).expect("Error reading the file"); + let myconfig: Self = toml::from_str(&i).unwrap(); + myconfig + } +} + #[derive(Debug)] struct Toot { title: String, @@ -39,15 +59,14 @@ fn write_to_disk(toot: &Toot, path: String, title: String) { } fn main() { - // Configure RSS url, author and path where the .md will be stored - let config = Config { - // url: "https://mastodon.bofhers.es/@oscarmlage.rss", - url: "https://mastodon.bofhers.es/tags/micropost.rss".to_string(), - author: "oscarmlage".to_string(), - path: "temp/".to_string(), - version: "0.0.1".to_string(), - description: "Just another tool to convert RSS from Mastodon to Markdown".to_string(), - }; + // Default config file + let default_config = ".config/masto-rss/masto-rss.ini"; + let home = std::env::var("HOME").unwrap(); + let cfg = format!("{}/{}", home, default_config); + // Read the config file + let mut default_config = Config::default(); + let config = default_config.parse(cfg.to_string()); + println!("\n✨ masto-rss v.{} - {}\n", config.version, config.description); let rss = reqwest::blocking::get(config.url).unwrap().bytes(); let rss2 = rss.unwrap();