use std::fs;
use std::fs::File;
use std::error::Error;
use std::io::Read;
#[derive(Debug)]
pub struct Settings <'a> {
pnavrc_file_path: &'a str,
pub path_to_input_database: &'a str,
pub current_project_code: &'a str,
}
impl<'a> Settings<'a> {
pub fn new (project_code: &'a str) -> Settings<'a> {
Self {
pnavrc_file_path: "/root/.pnavrc",
path_to_input_database: "/root/pnav_project_root_for_testing",
current_project_code: project_code
}
}
fn create_empty_pnavrc(&self) -> Result<File, &'a str>{
match fs::File::create(self.pnavrc_file_path) {
Ok(file) => Ok(file),
Err(_) => Err("failed to create .pnavrc")
}
}
pub fn read_pnavrc(&mut self) -> Result<(), Box<dyn Error>>{
let mut file = match File::open(self.pnavrc_file_path){
Ok(file) => file,
Err(_) => {
self.create_empty_pnavrc()?;
File::open(self.pnavrc_file_path)?
}
};
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lines: Vec<&str> = contents.lines().collect();
println!("{}",lines[0]);
Ok(())
}
pub fn write_pnavrc(Settings: Settings) {}
}
pub fn test_program() {
let mut Settings = Settings::new("002001");
Settings.read_pnavrc();
}