ietf

CLI for reading IETF RFCs.
git clone https://gitlab.com/kwatafana/p2p/ietf.git
Log | Files | Refs | README | LICENSE

storage.rs (3892B)


      1 use std::fs::File;
      2 use std::fs::OpenOptions;
      3 use std::io::prelude::*;
      4 use std::path::Path;
      5 
      6 pub struct Storage {
      7     pub rfc_dir_path: String,
      8     pub index_file_path: String,
      9 }
     10 
     11 impl Storage {
     12     // Create new Storage instance
     13     pub fn new() -> Self {
     14         if let Some(home_path) = dirs_next::home_dir() {
     15             let rfc_dir_path = if cfg!(unix) || cfg!(macos) {
     16                 format!("{}/rfc/", home_path.to_str().unwrap())
     17             } else if cfg!(windows) {
     18                 format!("{}\\rfc\\", home_path.to_str().unwrap())
     19             } else {
     20                 panic!("Unsupported OS");
     21             };
     22 
     23             // Check if storage directory (~/rfc) has been created
     24             // otherwise create storage directory
     25             if !Path::new(&rfc_dir_path).exists() {
     26                 // create RFC storage directory
     27                 std::fs::create_dir(&rfc_dir_path).unwrap();
     28             }
     29 
     30             let index_file_path = format!("{}INDEX", &rfc_dir_path);
     31 
     32             // Check if RFC INDEX file is downloaded
     33             if !Path::new(&index_file_path).exists() {
     34                 let _create_index =
     35                     File::create(&index_file_path).expect("Unable to creat INDEX file");
     36 
     37                 // Fetch remote INDEX file data
     38                 let data = super::fetch::index().unwrap();
     39 
     40                 // Persist RFC INDEX
     41                 Storage::persist_index(&index_file_path, data);
     42             }
     43 
     44             Storage {
     45                 rfc_dir_path,
     46                 index_file_path,
     47             }
     48         } else {
     49             panic!("Error: 'Could not find home directory!'");
     50         }
     51     }
     52 
     53     // Save index localy
     54     pub fn persist_index(index_file_path: &str, data: Vec<String>) {
     55         let mut file = OpenOptions::new()
     56             .write(true)
     57             .append(true)
     58             .open(&index_file_path)
     59             .unwrap();
     60 
     61         for rfc in data.iter() {
     62             if let Err(e) = writeln!(file, "{}", rfc) {
     63                 eprintln!("Couldn't write to file: {}", e);
     64             }
     65         }
     66     }
     67 
     68     // Update index
     69     pub fn update_index(&self) {
     70         if Path::new(&self.index_file_path).exists() {
     71             std::fs::remove_file(&self.index_file_path).unwrap();
     72         }
     73 
     74         let _create_index =
     75             File::create(&self.index_file_path).expect("Unable to creat INDEX file");
     76 
     77         // Fetch remote INDEX file data
     78         let data = super::fetch::index().unwrap();
     79 
     80         // Persist RFC INDEX
     81         Storage::persist_index(&self.index_file_path, data);
     82     }
     83 
     84     // Check if an RFC file has been downloaded locally
     85     pub fn is_rfc_downloaded(&self, rfc_number: u32) -> Result<bool, ()> {
     86         let rfc_file_path = format!("{}{}", self.rfc_dir_path, rfc_number);
     87 
     88         if Path::new(&rfc_file_path).exists() {
     89             Ok(true)
     90         } else {
     91             Ok(false)
     92         }
     93     }
     94 
     95     // Save RFC localy
     96     pub fn persist_rfc(&self, rfc_number: u32, rfc_data: &str) {
     97         let rfc_file_path = format!("{}{}", self.rfc_dir_path, rfc_number);
     98 
     99         let _file = File::create(&rfc_file_path).expect("Unable to create file");
    100         let mut file = OpenOptions::new()
    101             .write(true)
    102             .append(true)
    103             .open(rfc_file_path)
    104             .unwrap();
    105 
    106         if let Err(e) = writeln!(file, "{}", rfc_data) {
    107             eprintln!("Couldn't write to file: {}", e);
    108         }
    109     }
    110 
    111     // Removes RFC by Serial Number
    112     pub fn remove(&self, rfc_number: u32) {
    113         let rfc_file_path = format!("{}{}", &self.rfc_dir_path, rfc_number);
    114 
    115         if Path::new(&rfc_file_path).exists() {
    116             std::fs::remove_file(&rfc_file_path).unwrap();
    117         }
    118     }
    119 
    120     // Removes the rfc directory
    121     pub fn clean(&self) {
    122         if Path::new(&self.rfc_dir_path).exists() {
    123             std::fs::remove_dir_all(&self.rfc_dir_path).unwrap();
    124         }
    125     }
    126 }