From 22e617eec8e2d8da36788ae40fb53c2ed2ebe734 Mon Sep 17 00:00:00 2001 From: Nathan Jaremko Date: Sun, 24 Feb 2019 16:52:01 -0500 Subject: Improve code --- src/utils.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 657ff5b..5c3b2db 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -12,6 +12,7 @@ use rss::Channel; pub const UNABLE_TO_PARSE_REGEX: &str = "unable to parse regex"; pub const UNABLE_TO_OPEN_FILE: &str = "unable to open file"; pub const UNABLE_TO_CREATE_FILE: &str = "unable to create file"; +pub const UNABLE_TO_READ_FILE: &str = "unable to read file"; pub const UNABLE_TO_WRITE_FILE: &str = "unable to write file"; pub const UNABLE_TO_READ_FILE_TO_STRING: &str = "unable to read file to string"; pub const UNABLE_TO_READ_DIRECTORY: &str = "unable to read directory"; @@ -25,6 +26,7 @@ pub const UNABLE_TO_CREATE_CHANNEL_FROM_RESPONSE: &str = "unable to create channel from http response"; pub const UNABLE_TO_CREATE_CHANNEL_FROM_FILE: &str = "unable to create channel from xml file"; pub const UNABLE_TO_RETRIEVE_PODCAST_BY_TITLE: &str = "unable to retrieve podcast by title"; + pub fn trim_extension(filename: &str) -> Option { let name = String::from(filename); let index = name.rfind('.')?; @@ -59,15 +61,32 @@ pub fn get_podcast_dir() -> Result { } } -pub fn create_directories() -> Result<()> { - let mut path = get_podcast_dir()?; - path.push(".rss"); +pub fn create_dir_if_not_exist(path: &PathBuf) -> Result<()> { DirBuilder::new() .recursive(true) .create(&path) - .chain_err(|| "unable to create directory") + .chain_err(|| UNABLE_TO_CREATE_DIRECTORY)?; + Ok(()) +} + +pub fn create_directories() -> Result<()> { + let mut path = get_podcast_dir()?; + path.push(".rss"); + create_dir_if_not_exist(&path) } +pub fn delete(title: &str) -> Result<()> { + let mut path = get_xml_dir()?; + let mut filename = String::from(title); + filename.push_str(".xml"); + path.push(filename); + fs::remove_file(path).chain_err(|| UNABLE_TO_REMOVE_FILE) + } + + pub fn delete_all() -> Result<()> { + fs::remove_dir_all(get_xml_dir()?).chain_err(|| UNABLE_TO_READ_DIRECTORY) + } + pub fn already_downloaded(dir: &str) -> Result> { let mut result = HashSet::new(); @@ -95,7 +114,7 @@ pub fn already_downloaded(dir: &str) -> Result> { pub fn get_sub_file() -> Result { let mut path = get_podcast_dir()?; - path.push(".subscriptions"); + path.push(".subscriptions.json"); Ok(path) } @@ -108,10 +127,7 @@ pub fn get_xml_dir() -> Result { pub fn download_rss_feed(url: &str) -> Result { let mut path = get_podcast_dir()?; path.push(".rss"); - DirBuilder::new() - .recursive(true) - .create(&path) - .chain_err(|| "unable to open directory")?; + create_dir_if_not_exist(&path)?; let mut resp = reqwest::get(url).chain_err(|| "unable to open url")?; let mut content: Vec = Vec::new(); resp.read_to_end(&mut content) -- cgit v1.2.3