rc restructure
This commit is contained in:
parent
398f97c716
commit
4c26ed1b9f
@ -3,5 +3,4 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||||||
pub mod shell;
|
pub mod shell;
|
||||||
mod commands;
|
mod commands;
|
||||||
mod ps;
|
mod ps;
|
||||||
mod rc;
|
mod rc;
|
||||||
mod tests;
|
|
164
src/rc.rs
164
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;
|
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 {
|
|
||||||
self.try_exists().map_or_else(|e| {RcError::FolderTryExists(e)}, |exists| match exists {
|
|
||||||
true => todo!(),
|
|
||||||
false => todo!()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_valid(&self) -> Option<PathBuf> {
|
|
||||||
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<PathBuf> {
|
|
||||||
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<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> {
|
#[allow(dead_code)]
|
||||||
let mut config = home::home_dir()?;
|
trait IsValid {
|
||||||
config.push(".config");
|
fn is_valid(&self, is_dir_or_file: bool) -> Result<PathBuf, IsValidDirErr>;
|
||||||
config.is_valid()?;
|
fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf>;
|
||||||
config.push("lambdashell");
|
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf>;
|
||||||
config.is_valid()
|
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 config_file() -> Option<PathBuf> {
|
fn is_valid(&self, is_content: bool) -> Result<PathBuf, IsValidDirErr> {
|
||||||
let mut config_file = config_dir()?;
|
match self.try_exists() {
|
||||||
config_file.push("config.luau");
|
Ok(true) => match is_content {
|
||||||
|
true => Ok(self.to_path_buf()),
|
||||||
if let Some(file) = config_file.is_valid_silent() {
|
false => Err(IsValidDirErr::NotAnEntry)
|
||||||
match file.is_file() {
|
|
||||||
true => {
|
|
||||||
|
|
||||||
},
|
},
|
||||||
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<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
|
||||||
|
where
|
||||||
|
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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
},
|
||||||
|
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<PathBuf> {
|
||||||
|
self.is_valid(is_dir_or_file).map_or(None, |p| Some(p))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn config_dir() -> Option<PathBuf> {
|
||||||
|
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<PathBuf> {
|
||||||
|
let mut config_file = config_dir()?;
|
||||||
|
config_file.push("config.luau");
|
||||||
|
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user