commit 15a3d511c2cb061c0c8d206816bfe34153523c7a
parent 50710aec900d0c6e5297dd596cbcb5e2aba9e7aa
Author: cy6erlion <50733658+cy6erlion@users.noreply.github.com>
Date: Thu, 31 Dec 2020 22:48:18 +0200
Merge pull request #5 from cy6erlion/storage
move all storage related code to storage.rs
Diffstat:
M | src/fetch.rs | | | 62 | ++------------------------------------------------------------ |
M | src/lib.rs | | | 143 | +++++++++---------------------------------------------------------------------- |
M | src/main.rs | | | 4 | ++-- |
A | src/storage.rs | | | 149 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 168 insertions(+), 190 deletions(-)
diff --git a/src/fetch.rs b/src/fetch.rs
@@ -1,13 +1,9 @@
-use std::fs::File;
-use std::fs::OpenOptions;
-use std::io::prelude::*;
-
// Download RFC index file
pub fn index() -> Result<(), minreq::Error> {
println!("Fetching RFC index");
let response = minreq::get("https://www.rfc-editor.org/rfc-index.txt").send()?;
let data = scrape(response.as_str()?);
- persist_index(data);
+ super::storage::persist_index(data);
Ok(())
}
@@ -18,64 +14,10 @@ pub fn rfc(sn: u32) -> Result<(), minreq::Error> {
println!("{}", address);
let response = minreq::get(&address).send()?;
- persist_rfc(sn, response.as_str()?);
+ super::storage::persist_rfc(sn, response.as_str()?);
Ok(())
}
-// Save index localy
-pub fn persist_index(index: Vec<String>) {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/INDEX", home_path.to_str().unwrap())
- } else if cfg!(windows) {
- format!("{}\\rfc\\INDEX", home_path.to_str().unwrap())
- } else {
- panic!("Unsupported OS");
- };
-
- let _file = File::create(&path).expect("Unable to create file");
- let mut file = OpenOptions::new()
- .write(true)
- .append(true)
- .open(&path)
- .unwrap();
-
- for rfc in index.iter() {
- if let Err(e) = writeln!(file, "{}", rfc) {
- eprintln!("Couldn't write to file: {}", e);
- }
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
-// Save RFC localy
-pub fn persist_rfc(sn: u32, rfc: &str) {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/{}", home_path.to_str().unwrap(), sn)
- } else if cfg!(windows) {
- format!("{}\\rfc\\{}", home_path.to_str().unwrap(), sn)
- } else {
- panic!("Unsupported OS");
- };
-
- let _file = File::create(&path).expect("Unable to create file");
- let mut file = OpenOptions::new()
- .write(true)
- .append(true)
- .open(path)
- .unwrap();
-
- if let Err(e) = writeln!(file, "{}", rfc) {
- eprintln!("Couldn't write to file: {}", e);
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
// TODO: fix bug causing not to return the last RFC
pub fn scrape(data: &str) -> Vec<String> {
let mut count = 0;
diff --git a/src/lib.rs b/src/lib.rs
@@ -7,25 +7,22 @@ use std::io::{BufReader, Read};
use std::path::Path;
mod fetch;
+pub mod storage;
pub fn list_view() {
- if !index_exists().unwrap() {
+ if !storage::index_exists().unwrap() {
// Download all RFCs
fetch::index().unwrap();
}
- let path = if let Some(home_path) = dirs_next::home_dir() {
- if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/INDEX", home_path.to_str().unwrap())
- } else if cfg!(windows) {
- format!("{}\\rfc\\INDEX", home_path.to_str().unwrap())
- } else {
- panic!("Unsupported OS");
- }
+ let home_path = if let Some(p) = storage::get_home_path() {
+ p
} else {
- panic!("No home directory");
+ panic!("Error: 'Could not find home directory!'");
};
+ let path = format!("{}INDEX", home_path);
+
let mut index = String::new();
let f = File::open(&path).expect("Unable to open file");
let mut br = BufReader::new(f);
@@ -50,26 +47,21 @@ pub fn list_view() {
}
// Read RFC by serial number
-pub fn read_rfc(sn: u32) {
+pub fn read_rfc(rfc_number: u32) {
// check if RFC is downloaded
- if !is_rfc_downloaded(sn).unwrap() {
+ if !storage::is_rfc_downloaded(rfc_number).unwrap() {
// download RFC
- println!("Downloading rfc");
- fetch::rfc(sn).unwrap();
+ fetch::rfc(rfc_number).unwrap();
}
- let path = if let Some(home_path) = dirs_next::home_dir() {
- if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/{}", home_path.to_str().unwrap(), sn)
- } else if cfg!(windows) {
- format!("{}\\rfc\\{}", home_path.to_str().unwrap(), sn)
- } else {
- panic!("Unsupported OS");
- }
+ let home_path = if let Some(p) = storage::get_home_path() {
+ p
} else {
- panic!("No home directory");
+ panic!("Error: 'Could not find home directory!'");
};
+ let path = format!("{}{}", home_path, rfc_number);
+
let mut rfc = String::new();
let f = File::open(&path).expect("Unable to open file");
let mut br = BufReader::new(f);
@@ -84,108 +76,3 @@ pub fn read_rfc(sn: u32) {
pub fn update() {
fetch::index().unwrap();
}
-
-// Removes RFC by Serial Number
-pub fn remove(sn: u32) {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/{}", home_path.to_str().unwrap(), sn)
- } else if cfg!(windows) {
- format!("{}\\rfc\\{}", home_path.to_str().unwrap(), sn)
- } else {
- panic!("Unsupported OS");
- };
-
- if Path::new(&path).exists() {
- std::fs::remove_file(&path).unwrap();
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
-// Removes the rfc directory
-pub fn clean() -> () {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc", home_path.to_str().unwrap())
- } else if cfg!(windows) {
- format!("{}\\rfc", home_path.to_str().unwrap())
- } else {
- panic!("Unsupported OS");
- };
-
- if Path::new(&path).exists() {
- std::fs::remove_dir_all(&path).unwrap();
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
-// Check if it is first time running by
-// checking if config files exist
-fn index_exists() -> Result<bool, ()> {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/INDEX", home_path.to_str().unwrap())
- } else if cfg!(windows) {
- format!("{}\\rfc\\INDEX", home_path.to_str().unwrap())
- } else {
- panic!("Unsupported OS");
- };
-
- if Path::new(&path).exists() {
- Ok(true)
- } else {
- init_storage_sir().unwrap();
- Ok(false)
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
-// Check if an RFC has been downloaded locally
-fn is_rfc_downloaded(sn: u32) -> Result<bool, ()> {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc/{}", home_path.to_str().unwrap(), sn)
- } else if cfg!(windows) {
- format!("{}\\rfc\\{}", home_path.to_str().unwrap(), sn)
- } else {
- panic!("Unsupported OS");
- };
-
- if Path::new(&path).exists() {
- Ok(true)
- } else {
- init_storage_sir().unwrap();
- Ok(false)
- }
- } else {
- panic!("Could not find home directory");
- }
-}
-
-// Check and create storage directory
-fn init_storage_sir() -> std::io::Result<()> {
- if let Some(home_path) = dirs_next::home_dir() {
- let path = if cfg!(unix) || cfg!(macos) {
- format!("{}/rfc", home_path.to_str().unwrap())
- } else if cfg!(windows) {
- format!("{}\\rfc", home_path.to_str().unwrap())
- } else {
- panic!("Unsupported OS");
- };
-
- if Path::new(&path).exists() {
- Ok(())
- } else {
- std::fs::create_dir(path)?;
- Ok(())
- }
- } else {
- panic!("Could not find home directory");
- }
-}
diff --git a/src/main.rs b/src/main.rs
@@ -36,7 +36,7 @@ fn main() {
// Removes RFC by serial number
if let Some(n) = matches.value_of("Remove") {
- ietf::remove(
+ ietf::storage::remove(
n.parse::<u32>()
.expect("RFC Serial Number should be a numeric value!"),
);
@@ -51,7 +51,7 @@ fn main() {
// Remove the ietf directory
if let Some(_matches) = matches.subcommand_matches("clean") {
- ietf::clean();
+ ietf::storage::clean();
return;
}
diff --git a/src/storage.rs b/src/storage.rs
@@ -0,0 +1,149 @@
+use std::fs::File;
+use std::fs::OpenOptions;
+use std::io::prelude::*;
+use std::path::Path;
+
+// Save index localy
+pub fn persist_index(index: Vec<String>) {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ let index_file_path = format!("{}INDEX", home_path);
+
+ let _file = File::create(&index_file_path).expect("Unable to create file");
+ let mut file = OpenOptions::new()
+ .write(true)
+ .append(true)
+ .open(&index_file_path)
+ .unwrap();
+
+ for rfc in index.iter() {
+ if let Err(e) = writeln!(file, "{}", rfc) {
+ eprintln!("Couldn't write to file: {}", e);
+ }
+ }
+}
+
+// Save RFC localy
+pub fn persist_rfc(rfc_number: u32, rfc: &str) {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ let rfc_file_path = format!("{}{}", home_path, rfc_number);
+
+ let _file = File::create(&rfc_file_path).expect("Unable to create file");
+ let mut file = OpenOptions::new()
+ .write(true)
+ .append(true)
+ .open(rfc_file_path)
+ .unwrap();
+
+ if let Err(e) = writeln!(file, "{}", rfc) {
+ eprintln!("Couldn't write to file: {}", e);
+ }
+}
+
+// Removes RFC by Serial Number
+pub fn remove(rfc_number: u32) {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ let rfc_file_path = format!("{}{}", home_path, rfc_number);
+
+ if Path::new(&rfc_file_path).exists() {
+ std::fs::remove_file(&rfc_file_path).unwrap();
+ }
+}
+
+// Removes the rfc directory
+pub fn clean() -> () {
+ let rfc_directory_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ if Path::new(&rfc_directory_path).exists() {
+ std::fs::remove_dir_all(&rfc_directory_path).unwrap();
+ }
+}
+
+// Check if an RFC file has been downloaded locally
+pub fn is_rfc_downloaded(rfc_number: u32) -> Result<bool, ()> {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ let rfc_file_path = format!("{}{}", &home_path, rfc_number);
+
+ if Path::new(&rfc_file_path).exists() {
+ Ok(true)
+ } else {
+ initialize_storage().unwrap();
+ Ok(false)
+ }
+}
+
+// Check if storage directory (~/rfc) has been created
+// if it does not, create the directory
+fn initialize_storage() -> std::io::Result<()> {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ if Path::new(&home_path).exists() {
+ Ok(())
+ } else {
+ std::fs::create_dir(&home_path)?;
+ Ok(())
+ }
+}
+
+// Check if the rfc INDEX file has been downloaded
+pub fn index_exists() -> Result<bool, ()> {
+ let home_path = if let Some(p) = get_home_path() {
+ p
+ } else {
+ panic!("Error: 'Could not find home directory!'");
+ };
+
+ let index_file_path = format!("{}INDEX", home_path);
+
+ if Path::new(&index_file_path).exists() {
+ Ok(true)
+ } else {
+ initialize_storage().unwrap();
+ Ok(false)
+ }
+}
+
+// Get path of home directory
+// (`~/rfc/` on unix systems and `C:\Users\{NAME}` on windows)
+pub fn get_home_path() -> Option<String> {
+ if let Some(home_path) = dirs_next::home_dir() {
+ let path = if cfg!(unix) || cfg!(macos) {
+ format!("{}/rfc/", home_path.to_str().unwrap())
+ } else if cfg!(windows) {
+ format!("{}\\rfc\\", home_path.to_str().unwrap())
+ } else {
+ panic!("Unsupported OS");
+ };
+
+ Some(path)
+ } else {
+ None
+ }
+}