move lib to its own repo
This commit is contained in:
@ -3,4 +3,5 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
pub mod shell;
|
||||
mod commands;
|
||||
mod ps;
|
||||
mod rc;
|
||||
mod rc;
|
||||
mod tests;
|
94
src/rc.rs
94
src/rc.rs
@ -1,44 +1,82 @@
|
||||
use std::{io, path::PathBuf};
|
||||
use std::{io, fs, io::Write, path::PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
pub enum RcError {
|
||||
FolderMissing,
|
||||
FolderTryExists(io::Error)
|
||||
const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict
|
||||
|
||||
local Username = Shell.system.username
|
||||
local Hostname = Shell.system.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),
|
||||
}
|
||||
|
||||
trait is_valid {
|
||||
fn try_exists_handle(&self) -> bool;
|
||||
fn is_valid(&self) -> Option<PathBuf>;
|
||||
fn is_valid_silent(&self) -> Option<PathBuf>;
|
||||
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> {
|
||||
let try_exists = match self.try_exists() {
|
||||
Ok(config_exist) => match config_exist {
|
||||
true => Ok(self.to_path_buf()),
|
||||
false => Err(RcError::FolderMissing),
|
||||
},
|
||||
Err(trye_error) => Err(RcError::FolderTryExists(trye_error)),
|
||||
};
|
||||
match try_exists {
|
||||
Ok(file) => Some(file.to_path_buf()),
|
||||
Err(rc_error) => match rc_error {
|
||||
RcError::FolderMissing => todo!(),
|
||||
RcError::FolderTryExists(error) => {
|
||||
println!("{error}");
|
||||
None
|
||||
},
|
||||
},
|
||||
}
|
||||
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 dot_config_folder() -> Option<PathBuf> {
|
||||
fn config_dir() -> Option<PathBuf> {
|
||||
let mut config = home::home_dir()?;
|
||||
config.push(".config");
|
||||
config.is_valid()?;
|
||||
config.push("lambdashell");
|
||||
config.is_valid()
|
||||
}
|
||||
|
||||
fn rc_folder() -> Option<PathBuf> {
|
||||
let mut dot_config = dot_config_folder()?;
|
||||
dot_config.push("lambdashell");
|
||||
dot_config.is_valid()
|
||||
}
|
||||
fn config_file() -> Option<PathBuf> {
|
||||
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 => {
|
||||
|
||||
},
|
||||
false => println!("{:?} is either not a file or permission was denied.", file.as_path().display())
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
Reference in New Issue
Block a user