commit 3455abb6b1bdbd92230f463a7e5452ef8a1171ca
parent 35b3fbcb83522a3a63bd020dd95609fe23f91c05
Author: cy6erlion <dev@merely.tech>
Date: Tue, 12 Jan 2021 11:46:12 +0200
move lib.rs into main.rs
Diffstat:
D | src/lib.rs | | | 78 | ------------------------------------------------------------------------------ |
M | src/main.rs | | | 68 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
2 files changed, 59 insertions(+), 87 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,78 +0,0 @@
-extern crate dirs_next;
-extern crate pager;
-
-use pager::Pager;
-use std::fs::File;
-use std::io::{BufReader, Read};
-use std::path::Path;
-
-mod fetch;
-pub mod storage;
-
-pub fn list_view() {
- if !storage::index_exists().unwrap() {
- // Download all RFCs
- fetch::index().unwrap();
- }
-
- let home_path = if let Some(p) = storage::get_home_path() {
- p
- } else {
- 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);
- let mut dots = "";
-
- br.read_to_string(&mut index).expect("Unable to read INDEX");
-
- Pager::with_pager("less -r").setup();
-
- for line in index.lines() {
- let line_words: Vec<&str> = line.split(' ').collect();
- let summerize: String = line.chars().skip(line_words[0].len()).take(77).collect();
-
- if line.len() >= 77 {
- dots = "...";
- }
-
- println!("{} | {}{}", line_words[0], summerize, dots);
-
- dots = "";
- }
-}
-
-// Read RFC by serial number
-pub fn read_rfc(rfc_number: u32) {
- // check if RFC is downloaded
- if !storage::is_rfc_downloaded(rfc_number).unwrap() {
- // download RFC
- fetch::rfc(rfc_number).unwrap();
- }
-
- let home_path = if let Some(p) = storage::get_home_path() {
- p
- } else {
- 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);
- br.read_to_string(&mut rfc).expect("Unable to read RFC");
-
- // Read RFC
- Pager::with_pager("less -r").setup();
- println!("{}", &rfc);
-}
-
-// Update RFC
-pub fn update() {
- fetch::index().unwrap();
-}
diff --git a/src/main.rs b/src/main.rs
@@ -1,4 +1,12 @@
use clap::{App, Arg, SubCommand};
+extern crate pager;
+
+mod fetch;
+pub mod storage;
+
+use pager::Pager;
+use std::fs::File;
+use std::io::{BufReader, Read};
fn main() {
let matches = App::new("ietf")
@@ -25,18 +33,38 @@ fn main() {
.subcommand(SubCommand::with_name("clean").about("Remove the rfc directory"))
.get_matches();
+ let storage = storage::Storage::new();
+
// Read RFC by serial number
if let Some(n) = matches.value_of("Number") {
- ietf::read_rfc(
- n.parse::<u32>()
- .expect("RFC Serial Number should be a numeric value!"),
- );
+ let rfc_number = n.parse::<u32>().unwrap();
+
+ // check if RFC is downloaded
+ if !storage.is_rfc_downloaded(rfc_number).unwrap() {
+ // download RFC
+ let rfc_data = fetch::rfc(rfc_number).unwrap();
+
+ // persist RFC
+ storage.persist_rfc(rfc_number, &rfc_data);
+ }
+
+ let rfc_file_path = format!("{}{}", storage.rfc_dir_path, rfc_number);
+
+ let mut rfc_data = String::new();
+ let index_file = File::open(&rfc_file_path).expect("Unable to open file");
+ let mut buffer_reader = BufReader::new(index_file);
+ buffer_reader
+ .read_to_string(&mut rfc_data)
+ .expect("Unable to read RFC");
+
+ Pager::with_pager("less -r").setup();
+ println!("{}", &rfc_data);
return;
}
// Removes RFC by serial number
if let Some(n) = matches.value_of("Remove") {
- ietf::storage::remove(
+ storage.remove(
n.parse::<u32>()
.expect("RFC Serial Number should be a numeric value!"),
);
@@ -45,16 +73,38 @@ fn main() {
// Update RFC index
if let Some(_matches) = matches.subcommand_matches("update") {
- ietf::update();
+ storage.update_index();
return;
}
// Remove the ietf directory
if let Some(_matches) = matches.subcommand_matches("clean") {
- ietf::storage::clean();
+ storage.clean();
return;
}
- // Display RFC list view
- ietf::list_view();
+ // ---------- Display RFC list view ------------
+ let mut index_data = String::new();
+ let index_file = File::open(&storage.index_file_path).expect("Unable to open file");
+ let mut buffer_reader = BufReader::new(index_file);
+ let mut read_more_dots = "";
+
+ buffer_reader
+ .read_to_string(&mut index_data)
+ .expect("Unable to read INDEX");
+
+ Pager::with_pager("less -r").setup();
+
+ for line in index_data.lines() {
+ let line_words: Vec<&str> = line.split(' ').collect();
+ let summerize: String = line.chars().skip(line_words[0].len()).take(77).collect();
+
+ if line.len() >= 77 {
+ read_more_dots = "...";
+ }
+
+ println!("{} | {}{}", line_words[0], summerize, read_more_dots);
+
+ read_more_dots = "";
+ }
}