diff --git a/src/lib.rs b/src/lib.rs index 1213bd7..eb276bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,5 +3,4 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod shell; mod commands; mod ps; -mod rc; -mod tests; \ No newline at end of file +mod rc; \ No newline at end of file diff --git a/src/rc.rs b/src/rc.rs index c144daa..20927e3 100644 --- a/src/rc.rs +++ b/src/rc.rs @@ -1,82 +1,114 @@ -use std::{io, fs, io::Write, path::PathBuf}; +use std::{fs::{self, File}, io::{self, Write}, path::PathBuf}; use thiserror::Error; const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict -local Username = Shell.system.username -local Hostname = Shell.system.hostname +local username = SHELL.SYSTEM.USERNAME +local hostname = SHELL.SYSTEM.HOSTNAME -Shell.prompt = `{Username}@{Hostname} λ `"#; +SHELL.PROMPT = `{username}@{hostname} λ `"#; #[derive(Debug, Error)] -enum RcError { - #[error("Folder is missing")] - FolderMissing, - #[error("Failed to check folder existence: {0}")] - FolderTryExists(#[from] io::Error), +#[allow(dead_code)] +enum IsValidDirErr { + #[error("Failed to see if a file exists: {0}")] + TryExists(#[from] io::Error), + #[error("Not a valid entry")] + NotAnEntry, + #[error("Directory missing")] + Missing } -trait is_valid { - fn try_exists_handle(&self) -> bool; - fn is_valid(&self) -> Option; - fn is_valid_silent(&self) -> Option; - fn is_valid_or_create(&self) -> Option; -} -impl is_valid for PathBuf { - fn try_exists_handle(&self) -> bool { - self.try_exists().map_or_else(|e| {RcError::FolderTryExists(e)}, |exists| match exists { - true => todo!(), - false => todo!() - }) - } - - fn is_valid(&self) -> Option { - self.try_exists().map_or_else(|e| { - println!("{}", RcError::FolderTryExists(e)); - None - }, |exists| match exists { - true => Some(self.to_path_buf()), - false => { - println!("{}", RcError::FolderMissing); - None - } - }) - } - - fn is_valid_silent(&self) -> Option { - self.try_exists().ok().map_or(None, |exists| match exists { - true => Some(self.to_path_buf()), - false => None, - }) - } - - fn is_valid_or_create(&self) -> Option { - self.is_valid().map_or_else(|| { - let new_dir = fs::create_dir(self).map_err(|e| println!("{e}")); - return None - }, |p_buf| Some(p_buf)) - } +#[allow(dead_code)] +enum CreateErr { + TryExists(io::Error), + Passable } -fn config_dir() -> Option { - let mut config = home::home_dir()?; - config.push(".config"); - config.is_valid()?; - config.push("lambdashell"); - config.is_valid() +#[allow(dead_code)] +trait IsValid { + fn is_valid(&self, is_dir_or_file: bool) -> Result; + fn is_valid_option(&self, is_dir_or_file: bool) -> Option; + fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option; + fn is_valid_dir_or_create(&self) -> Option; + fn is_valid_or(&self, is_content: bool, f: F) -> Option + where + F: FnOnce() -> Option; } - -fn config_file() -> Option { - let mut config_file = config_dir()?; - config_file.push("config.luau"); - - if let Some(file) = config_file.is_valid_silent() { - match file.is_file() { - true => { - +impl IsValid for PathBuf { + fn is_valid(&self, is_content: bool) -> Result { + match self.try_exists() { + Ok(true) => match is_content { + true => Ok(self.to_path_buf()), + false => Err(IsValidDirErr::NotAnEntry) }, - false => println!("{:?} is either not a file or permission was denied.", file.as_path().display()) + Ok(false) => Err(IsValidDirErr::Missing), + Err(try_e) => Err(IsValidDirErr::TryExists(try_e)) } } - None + + fn is_valid_or(&self, is_content: bool, f: F) -> Option + where + F: FnOnce() -> Option + { + let possible_content = self.is_valid(is_content).map_err(|e| match e { + IsValidDirErr::TryExists(try_e) => CreateErr::TryExists(try_e), + IsValidDirErr::NotAnEntry | IsValidDirErr::Missing => CreateErr::Passable + }); + match possible_content { + Ok(p) => Some(p), + Err(e) => match e { + CreateErr::TryExists(_) => None, + CreateErr::Passable => f() + }, + } + } + + fn is_valid_dir_or_create(&self) -> Option { + self.is_valid_or(self.is_dir(), || { + match fs::create_dir(self) { + Ok(()) => Some(self.to_path_buf()), + Err(create_e) => { + println!("{create_e}"); + None + }, + } + }) + } + + fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option { + self.is_valid_or(self.is_file(), || { + match File::create(self) { + Err(create_e) => { + println!("{create_e}"); + None + }, + Ok(mut file) => match file.write_all(default_file_bytes) { + Ok(()) => Some(self.to_path_buf()), + Err(write_e) => { + println!("{write_e}"); + None + }, + }, + } + }) + } + + fn is_valid_option(&self, is_dir_or_file: bool) -> Option { + self.is_valid(is_dir_or_file).map_or(None, |p| Some(p)) + } +} + +pub fn config_dir() -> Option { + let mut config = home::home_dir()?; + config.push(".config"); + config.is_valid_option(config.is_dir())?; + config.push("lambdashell"); + config.is_valid_dir_or_create() +} + +pub fn config_file() -> Option { + let mut config_file = config_dir()?; + config_file.push("config.luau"); + config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes()) } \ No newline at end of file