diff options
| author | Nathan Jaremko | 2017-11-30 09:58:51 -0500 | 
|---|---|---|
| committer | Nathan Jaremko | 2017-11-30 09:58:51 -0500 | 
| commit | 752190225f31f559cb4288442755b7c0868f5b27 (patch) | |
| tree | beae6f8ade17b8758aea2fc352643c79a4697cfd | |
| parent | bdcd9a9e0739796bc568de9c4296026a9ea147a6 (diff) | |
| download | podcast-752190225f31f559cb4288442755b7c0868f5b27.tar.bz2 | |
Some changes to config file format
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/actions.rs | 22 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/structs.rs | 29 | 
4 files changed, 31 insertions, 24 deletions
| @@ -1,6 +1,6 @@  [package]  name = "podcast" -version = "0.4.0" +version = "0.4.1"  authors = ["njaremko <njaremko@gmail.com>"]  description = "A command line podcast player"  license = "GPL-3.0" diff --git a/src/actions.rs b/src/actions.rs index 31e0e6c..108d3e9 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -59,7 +59,7 @@ pub fn download_rss(url: &str, config: &Config) {  pub fn update_rss(state: &mut State) {      println!("Checking for new episodes..."); -    &state.subs.par_iter_mut().for_each(|sub| { +    &state.subscriptions.par_iter_mut().for_each(|sub| {          let mut path = get_podcast_dir();          path.push(&sub.title);          DirBuilder::new().recursive(true).create(&path).unwrap(); @@ -105,7 +105,7 @@ pub fn list_subscriptions(state: &State) {  pub fn download_range(state: &State, p_search: &str, e_search: &str) {      let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex"); -    for subscription in &state.subs { +    for subscription in &state.subscriptions {          if re_pod.is_match(&subscription.title) {              match Podcast::from_title(&subscription.title) {                  Ok(podcast) => match parse_download_episodes(e_search) { @@ -126,7 +126,7 @@ pub fn download_episode(state: &State, p_search: &str, e_search: &str) {      let re_pod = Regex::new(p_search).unwrap();      let ep_num = e_search.parse::<usize>().unwrap(); -    for subscription in &state.subs { +    for subscription in &state.subscriptions {          if re_pod.is_match(&subscription.title) {              match Podcast::from_title(&subscription.title) {                  Ok(podcast) => { @@ -144,7 +144,7 @@ pub fn download_episode(state: &State, p_search: &str, e_search: &str) {  pub fn download_all(state: &State, p_search: &str) {      let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex"); -    for subscription in &state.subs { +    for subscription in &state.subscriptions {          if re_pod.is_match(&subscription.title) {              match Podcast::from_title(&subscription.title) {                  Ok(podcast) => if let Err(err) = podcast.download() { @@ -168,7 +168,7 @@ pub fn play_episode(state: &State, p_search: &str, ep_num_string: &str) {          );          return;      } -    for subscription in &state.subs { +    for subscription in &state.subscriptions {          if re_pod.is_match(&subscription.title) {              let mut filename = subscription.title.clone();              filename.push_str(".xml"); @@ -209,10 +209,10 @@ pub fn check_for_update(version: &str) {      match resp.parse::<toml::Value>() {          Ok(config) => {              let latest = config["package"]["version"].as_str().unwrap(); -             if version != latest { +            if version != latest {                  println!("New version avaliable: {}", latest); -             } -            }, +            } +        }          Err(err) => eprintln!("{}", err),      }  } @@ -262,10 +262,10 @@ pub fn remove_podcast(state: &mut State, p_search: &str) {      let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex"); -    for subscription in 0..state.subs.len() { -        let title = state.subs[subscription].title.clone(); +    for subscription in 0..state.subscriptions.len() { +        let title = state.subscriptions[subscription].title.clone();          if re_pod.is_match(&title) { -            state.subs.remove(subscription); +            state.subscriptions.remove(subscription);              match Podcast::delete(&title) {                  Ok(_) => println!("Success"),                  Err(err) => eprintln!("Error: {}", err), diff --git a/src/main.rs b/src/main.rs index fe5cabe..efd92ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ fn main() {          eprintln!("{}", err);          return;      } -    let mut state = match State::new() { +    let mut state = match State::new(VERSION) {          Ok(val) => val,          Err(err) => {              eprintln!("{}", err); diff --git a/src/structs.rs b/src/structs.rs index 885d6cd..be5c6d5 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -56,11 +56,12 @@ pub struct Subscription {  #[derive(Serialize, Deserialize, Clone)]  pub struct State {      pub last_run_time: DateTime<Utc>, -    pub subs: Vec<Subscription>, +    pub subscriptions: Vec<Subscription>, +    pub version: String,  }  impl State { -    pub fn new() -> Result<State, String> { +    pub fn new(version: &str) -> Result<State, String> {          let mut path = get_podcast_dir();          path.push(".subscriptions");          if path.exists() { @@ -74,12 +75,16 @@ impl State {              };              let mut state: State = match serde_json::from_str(&s) {                  Ok(val) => val, -                Err(err) => { -                    return Err(format!( -                        "Could not parse: {}\nReason: {}", -                        &path.to_str().unwrap(), -                        err -                    )) +                Err(_) => { +                    let v: serde_json::Value = serde_json::from_str(&s).unwrap(); +                    State { +                        last_run_time: Utc::now(), +                        subscriptions: match serde_json::from_value(v["subscriptions"].clone()) { +                            Ok(val) => val, +                            Err(_) => serde_json::from_value(v["subs"].clone()).unwrap(), +                        }, +                        version: String::from(version), +                    }                  }              };              // Check if a day has passed (86400 seconds) @@ -89,13 +94,15 @@ impl State {                  .num_seconds() < -86400              {                  update_rss(&mut state); +                check_for_update(&state.version);              }              state.last_run_time = Utc::now();              Ok(state)          } else {              Ok(State {                  last_run_time: Utc::now(), -                subs: Vec::new(), +                subscriptions: Vec::new(), +                version: String::from(version),              })          }      } @@ -107,7 +114,7 @@ impl State {          }          let podcast = Podcast::from(Channel::from_url(url).unwrap());          if !set.contains(podcast.title()) { -            self.subs.push(Subscription { +            self.subscriptions.push(Subscription {                  title: String::from(podcast.title()),                  url: String::from(url),                  num_episodes: podcast.episodes().len(), @@ -120,7 +127,7 @@ impl State {      }      pub fn subscriptions(&self) -> Vec<Subscription> { -        self.subs.clone() +        self.subscriptions.clone()      }      pub fn save(&self) -> Result<(), io::Error> { | 
