rc restructure

This commit is contained in:
rhpidfyre 2024-12-27 17:52:25 -05:00
parent 398f97c716
commit 4c26ed1b9f
2 changed files with 99 additions and 68 deletions

View File

@ -4,4 +4,3 @@ pub mod shell;
mod commands; mod commands;
mod ps; mod ps;
mod rc; mod rc;
mod tests;

138
src/rc.rs
View File

@ -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; use thiserror::Error;
const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict
local Username = Shell.system.username local username = SHELL.SYSTEM.USERNAME
local Hostname = Shell.system.hostname local hostname = SHELL.SYSTEM.HOSTNAME
Shell.prompt = `{Username}@{Hostname} λ `"#; SHELL.PROMPT = `{username}@{hostname} λ `"#;
#[derive(Debug, Error)] #[derive(Debug, Error)]
enum RcError { #[allow(dead_code)]
#[error("Folder is missing")] enum IsValidDirErr {
FolderMissing, #[error("Failed to see if a file exists: {0}")]
#[error("Failed to check folder existence: {0}")] TryExists(#[from] io::Error),
FolderTryExists(#[from] io::Error), #[error("Not a valid entry")]
NotAnEntry,
#[error("Directory missing")]
Missing
} }
trait is_valid { #[allow(dead_code)]
fn try_exists_handle(&self) -> bool; enum CreateErr {
fn is_valid(&self) -> Option<PathBuf>; TryExists(io::Error),
fn is_valid_silent(&self) -> Option<PathBuf>; Passable
fn is_valid_or_create(&self) -> Option<PathBuf>;
} }
impl is_valid for PathBuf {
fn try_exists_handle(&self) -> bool { #[allow(dead_code)]
self.try_exists().map_or_else(|e| {RcError::FolderTryExists(e)}, |exists| match exists { trait IsValid {
true => todo!(), fn is_valid(&self, is_dir_or_file: bool) -> Result<PathBuf, IsValidDirErr>;
false => todo!() fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf>;
}) fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf>;
fn is_valid_dir_or_create(&self) -> Option<PathBuf>;
fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
where
F: FnOnce() -> Option<PathBuf>;
}
impl IsValid for PathBuf {
fn is_valid(&self, is_content: bool) -> Result<PathBuf, IsValidDirErr> {
match self.try_exists() {
Ok(true) => match is_content {
true => Ok(self.to_path_buf()),
false => Err(IsValidDirErr::NotAnEntry)
},
Ok(false) => Err(IsValidDirErr::Missing),
Err(try_e) => Err(IsValidDirErr::TryExists(try_e))
}
} }
fn is_valid(&self) -> Option<PathBuf> { fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
self.try_exists().map_or_else(|e| { where
println!("{}", RcError::FolderTryExists(e)); F: FnOnce() -> Option<PathBuf>
{
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<PathBuf> {
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 None
}, |exists| match exists { },
true => Some(self.to_path_buf()), }
false => { })
println!("{}", RcError::FolderMissing); }
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf> {
self.is_valid_or(self.is_file(), || {
match File::create(self) {
Err(create_e) => {
println!("{create_e}");
None 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_silent(&self) -> Option<PathBuf> { fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf> {
self.try_exists().ok().map_or(None, |exists| match exists { self.is_valid(is_dir_or_file).map_or(None, |p| Some(p))
true => Some(self.to_path_buf()),
false => None,
})
}
fn is_valid_or_create(&self) -> Option<PathBuf> {
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))
} }
} }
fn config_dir() -> Option<PathBuf> { pub fn config_dir() -> Option<PathBuf> {
let mut config = home::home_dir()?; let mut config = home::home_dir()?;
config.push(".config"); config.push(".config");
config.is_valid()?; config.is_valid_option(config.is_dir())?;
config.push("lambdashell"); config.push("lambdashell");
config.is_valid() config.is_valid_dir_or_create()
} }
fn config_file() -> Option<PathBuf> { pub fn config_file() -> Option<PathBuf> {
let mut config_file = config_dir()?; let mut config_file = config_dir()?;
config_file.push("config.luau"); config_file.push("config.luau");
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
if let Some(file) = config_file.is_valid_silent() {
match file.is_file() {
true => {
},
false => println!("{:?} is either not a file or permission was denied.", file.as_path().display())
}
}
None
} }