From c08d0a20e28748d96913b51a80f441825c7f2c3d Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Sun, 29 Dec 2024 01:46:30 -0500 Subject: [PATCH] convert indentation to tabs --- src/commands.rs | 184 ++++++++++++++++++++++++------------------------ src/luau/vm.rs | 12 ++-- src/ps.rs | 22 +++--- src/rc.rs | 26 +++---- src/shell.rs | 128 ++++++++++++++++----------------- 5 files changed, 179 insertions(+), 193 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index e79563f..37d96b1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,9 +1,9 @@ use uzers::User; use std::{ - io, - process, - str::SplitWhitespace, - path::{Path, PathBuf}, + io, + process, + str::SplitWhitespace, + path::{Path, PathBuf}, }; enum ValidStatus { @@ -22,31 +22,31 @@ trait PathBufIsValid { } trait ChangeDirectory { - fn change_directory(&self, args: SplitWhitespace) -> Option; - fn set_current_dir(&self, new_path: &Path) -> Option; - fn specific_user_dir(&self, user: String) -> Option; - fn cd_args(&self, vec_args: Vec) -> Option; - fn previous_dir(&self) -> Option; - fn home_dir(&self) -> Option; + fn change_directory(&self, args: SplitWhitespace) -> Option; + fn set_current_dir(&self, new_path: &Path) -> Option; + fn specific_user_dir(&self, user: String) -> Option; + fn cd_args(&self, vec_args: Vec) -> Option; + fn previous_dir(&self) -> Option; + fn home_dir(&self) -> Option; } impl PathBufIsValid for PathBuf { fn is_valid(&self) -> Result { match self.try_exists() { - Ok(true) => Ok(self.to_path_buf()), - Ok(false) => Err(ValidStatus::NoRootFolder), - Err(trye_error) => Err(ValidStatus::TryExists(trye_error)) - } + Ok(true) => Ok(self.to_path_buf()), + Ok(false) => Err(ValidStatus::NoRootFolder), + Err(trye_error) => Err(ValidStatus::TryExists(trye_error)) + } } fn is_valid_or_home(&self) -> Option { match self.is_valid() { - Ok(valid) => Some(valid), - Err(valid_status) => { + Ok(valid) => Some(valid), + Err(valid_status) => { match valid_status { - ValidStatus::NoRootFolder => println!("cd: /root: No such file or directory"), - ValidStatus::TryExists(error) => println!("cd: {error}"), - }; + ValidStatus::NoRootFolder => println!("cd: /root: No such file or directory"), + ValidStatus::TryExists(error) => println!("cd: {error}"), + }; None }, } @@ -55,99 +55,99 @@ impl PathBufIsValid for PathBuf { impl ChangeDirectory for Command { fn set_current_dir(&self, new_path: &Path) -> Option { - std::env::set_current_dir(new_path).map_or_else(|cd_err| display_none(cd_err), |()| Some(new_path.to_path_buf())) + std::env::set_current_dir(new_path).map_or_else(|cd_err| display_none(cd_err), |()| Some(new_path.to_path_buf())) } fn home_dir(&self) -> Option { - home::home_dir().map_or(self.set_current_dir(Path::new("/")), |home_pathbuf| self.set_current_dir(&home_pathbuf)) + home::home_dir().map_or(self.set_current_dir(Path::new("/")), |home_pathbuf| self.set_current_dir(&home_pathbuf)) } fn previous_dir(&self) -> Option { unimplemented!() } - fn specific_user_dir(&self, requested_user: String) -> Option { - match requested_user.as_str() { - "root" => PathBuf::from("/root").is_valid_or_home(), + fn specific_user_dir(&self, requested_user: String) -> Option { + match requested_user.as_str() { + "root" => PathBuf::from("/root").is_valid_or_home(), _ => { for user in unsafe { uzers::all_users().collect::>() } { - let user_name = user.name(); - if *requested_user == *user_name { - let mut user_dir = PathBuf::from("/home"); - user_dir.push(user_name); - return user_dir.is_valid_or_home(); - } - } - None + let user_name = user.name(); + if *requested_user == *user_name { + let mut user_dir = PathBuf::from("/home"); + user_dir.push(user_name); + return user_dir.is_valid_or_home(); + } + } + None } } - } + } - fn cd_args(&self, vec_args: Vec) -> Option { - let string_path = vec_args.concat(); - let new_path = Path::new(string_path.as_str()); - match new_path.is_dir() { - true => self.set_current_dir(new_path), - false => { - match new_path.file_name() { - Some(file_name) => println!("cd: {:?} is not a directory.", file_name), - None => println!("cd: Failed to resolve the file name of a file that is not a directory."), - } - None - } - } - } + fn cd_args(&self, vec_args: Vec) -> Option { + let string_path = vec_args.concat(); + let new_path = Path::new(string_path.as_str()); + match new_path.is_dir() { + true => self.set_current_dir(new_path), + false => { + match new_path.file_name() { + Some(file_name) => println!("cd: {:?} is not a directory.", file_name), + None => println!("cd: Failed to resolve the file name of a file that is not a directory."), + } + None + } + } + } - fn change_directory(&self, args: SplitWhitespace) -> Option { - let vec_args: Vec = args.map(|arg| arg.to_owned()).collect(); - match vec_args.first() { - None => self.home_dir(), - Some(arg) => match arg.as_str() { - "/" => self.set_current_dir(Path::new("/")), - "-" => self.previous_dir(), - _ => { - let mut arg_chars = arg.chars(); - match arg_chars.next() { - Some(char) => match char == '~' { - true => self.specific_user_dir(arg_chars.collect::()), - false => self.cd_args(vec_args), - }, - None => self.home_dir(), - } - } - }, - } - } + fn change_directory(&self, args: SplitWhitespace) -> Option { + let vec_args: Vec = args.map(|arg| arg.to_owned()).collect(); + match vec_args.first() { + None => self.home_dir(), + Some(arg) => match arg.as_str() { + "/" => self.set_current_dir(Path::new("/")), + "-" => self.previous_dir(), + _ => { + let mut arg_chars = arg.chars(); + match arg_chars.next() { + Some(char) => match char == '~' { + true => self.specific_user_dir(arg_chars.collect::()), + false => self.cd_args(vec_args), + }, + None => self.home_dir(), + } + } + }, + } + } } pub type ProcessExitStatus = Option; pub struct Command(String); impl Command { - pub fn new(input: String) -> Self { - Self(input) - } + pub fn new(input: String) -> Self { + Self(input) + } - pub fn spawn(&self, command_process: io::Result) -> ProcessExitStatus { - match command_process { - Err(e) => display_none(e), - Ok(mut child) => Some(match child.wait() { - Ok(exit_status) => exit_status, - Err(exit_status_err) => { - println!("{exit_status_err}"); - return None - } - }) - } - } + pub fn spawn(&self, command_process: io::Result) -> ProcessExitStatus { + match command_process { + Err(e) => display_none(e), + Ok(mut child) => Some(match child.wait() { + Ok(exit_status) => exit_status, + Err(exit_status_err) => { + println!("{exit_status_err}"); + return None + } + }) + } + } - pub fn exec(&self) -> ProcessExitStatus { - let mut args = self.0.split_whitespace(); - args.next().and_then(|command| match command { - "cd" => { - self.change_directory(args); - None - } - command => self.spawn(process::Command::new(command).args(args).spawn()), - }) - } + pub fn exec(&self) -> ProcessExitStatus { + let mut args = self.0.split_whitespace(); + args.next().and_then(|command| match command { + "cd" => { + self.change_directory(args); + None + } + command => self.spawn(process::Command::new(command).args(args).spawn()), + }) + } } diff --git a/src/luau/vm.rs b/src/luau/vm.rs index 1b3e359..d942132 100644 --- a/src/luau/vm.rs +++ b/src/luau/vm.rs @@ -24,10 +24,10 @@ fn luau_out(luau_args: MultiValue) -> String { luau_args.iter() .map(|arg| arg.to_string().unwrap_or("".to_owned())) .for_each(|arg| { - if !print.is_empty() { - print.push('\u{0009}'.to_string()); - }; - print.push(arg); + if !print.is_empty() { + print.push('\u{0009}'.to_string()); + }; + print.push(arg); } ); print.concat() @@ -71,9 +71,9 @@ impl Vm { Ok(()) } - pub fn exec(&self, source: String) -> Option<()> { + pub fn exec(&self, source: String) { self.set_shell_globals().map_or_else(|e| display_none(e), |()| { self.0.load(source).exec().map_or_else(|e| luau_error(e), |()| Some(())) - }) + }); } } \ No newline at end of file diff --git a/src/ps.rs b/src/ps.rs index 8c9cad1..d63e653 100644 --- a/src/ps.rs +++ b/src/ps.rs @@ -4,18 +4,18 @@ use color_print::{cformat, cprint}; pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION")); pub fn working_dir_name() -> String { - std::env::current_dir().map_or("?".to_owned(), |path| { - path.file_name().map_or("?".to_owned(), |name| { - let name_os_string = name.to_os_string(); - match name_os_string == whoami::username_os() && name_os_string != "root" { - true => "~".to_owned(), - false => name.to_string_lossy().to_string(), - } - }) - }) + std::env::current_dir().map_or("?".to_owned(), |path| { + path.file_name().map_or("?".to_owned(), |name| { + let name_os_string = name.to_os_string(); + match name_os_string == whoami::username_os() && name_os_string != "root" { + true => "~".to_owned(), + false => name.to_string_lossy().to_string(), + } + }) + }) } pub fn display(ps1: &String) { - let working_dir_name = cformat!(" {} ", working_dir_name()); - cprint!("{}{}λ ", ps1, working_dir_name); + let working_dir_name = cformat!(" {} ", working_dir_name()); + cprint!("{}{}λ ", ps1, working_dir_name); } diff --git a/src/rc.rs b/src/rc.rs index d8a4eef..6836339 100644 --- a/src/rc.rs +++ b/src/rc.rs @@ -1,7 +1,7 @@ use std::{fs::{self, File}, io::{self, Write}, path::PathBuf}; use thiserror::Error; -const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict +pub const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict local username = SHELL.SYSTEM.USERNAME local hostname = SHELL.SYSTEM.HOSTNAME @@ -61,10 +61,10 @@ impl IsValid for PathBuf { IsValidDirErr::NotAnEntry | IsValidDirErr::Missing => CreateErr::Passable }); match possible_content { - Ok(p) => Some(p), - Err(e) => match e { - CreateErr::TryExists(_) => None, - CreateErr::Passable => f() + Ok(p) => Some(p), + Err(e) => match e { + CreateErr::TryExists(_) => None, + CreateErr::Passable => f() }, } } @@ -72,8 +72,8 @@ impl IsValid for PathBuf { 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) => display_none(create_e), + Ok(()) => Some(self.to_path_buf()), + Err(create_e) => display_none(create_e), } }) } @@ -81,11 +81,11 @@ impl IsValid for PathBuf { fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option { self.is_valid_or(self.is_file(), || { match File::create(self) { - Ok(mut file) => match file.write_all(default_file_bytes) { - Ok(()) => Some(self.to_path_buf()), - Err(write_e) => display_none(write_e), + Ok(mut file) => match file.write_all(default_file_bytes) { + Ok(()) => Some(self.to_path_buf()), + Err(write_e) => display_none(write_e), }, - Err(create_e) => display_none(create_e) + Err(create_e) => display_none(create_e) } }) } @@ -114,8 +114,4 @@ pub fn history_file() -> Option { let mut config_file = config_dir()?; config_file.push(".history"); config_file.is_valid_file_or_create("".as_bytes()) -} - -pub fn none() -> Option { - None } \ No newline at end of file diff --git a/src/shell.rs b/src/shell.rs index f4050a4..b278d74 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,95 +1,85 @@ use crate::{ps, commands, rc, vm}; use std::{fs, io::{self}}; -use core::fmt; - -fn display_none(err: E) -> Option -where - E: fmt::Display -{ - println!("{err}"); - None -} pub struct Config { - pub norc: bool + pub norc: bool } struct Storage { - pub command_exit_status: commands::ProcessExitStatus, - pub ps1: String, + pub command_exit_status: commands::ProcessExitStatus, + pub ps1: String, } trait ShellLuauVm { - fn shell_vm_exec(&self, source: String) -> Option<()>; + fn shell_vm_exec(&self, source: String); } impl ShellLuauVm for LambdaShell { - fn shell_vm_exec(&self, source: String) -> Option<()> { - vm::Vm::new().map_or(None, |vm| vm.exec(source)) + fn shell_vm_exec(&self, source: String) { + vm::Vm::new().map(|vm| vm.exec(source)); } } pub struct LambdaShell { - terminating: bool, - storage: Storage, - config: Config, + terminating: bool, + storage: Storage, + config: Config, } impl LambdaShell { - pub fn create(config: Config) -> Self { - Self { - storage: Storage { - command_exit_status: None, - ps1: ps::DEFAULT_PS.to_owned(), - }, - terminating: false, - config, - } - } + pub fn create(config: Config) -> Self { + Self { + storage: Storage { + command_exit_status: None, + ps1: ps::DEFAULT_PS.to_owned(), + }, + terminating: false, + config, + } + } - fn input(&mut self) { - let mut input = String::new(); - io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| { - let trimmed_input = input.trim(); - match trimmed_input { - //special casey - "exit" => self.terminating = true, - _ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec() - }; - }) - } + fn input(&mut self) { + let mut input = String::new(); + io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| { + let trimmed_input = input.trim(); + match trimmed_input { + //special casey + "exit" => self.terminating = true, + _ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec() + }; + }) + } - pub fn wait(&mut self) -> Result<(), io::Error> { - io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| { - self.input(); + pub fn wait(&mut self) -> Result<(), io::Error> { + io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| { + self.input(); Ok(()) - }) - } + }) + } - fn rc_parse(&self) { - let rc_file = match self.config.norc { - true => rc::none(), - false => rc::config_file(), - }; - rc_file.map(|conf_file| fs::read_to_string(conf_file) - .map_or_else(|read_err| display_none(read_err), |conf| self.shell_vm_exec(conf)) - ); - } + fn rc_parse(&self) { + if !self.config.norc { + rc::config_file().map(|conf_file| fs::read_to_string(conf_file).map_or_else( + |read_err| println!("{read_err}"), + |luau_conf| self.shell_vm_exec(luau_conf) + )); + } + } - pub fn start(&mut self) { - self.rc_parse(); + pub fn start(&mut self) { + self.rc_parse(); - ps::display(&self.storage.ps1); + ps::display(&self.storage.ps1); - loop { - match self.terminating { - true => break, - false => match self.wait() { - Ok(()) => ps::display(&self.storage.ps1), - Err(flush_error) => { - println!("{flush_error}"); - break; - } - }, - } - } - } + loop { + match self.terminating { + true => break, + false => match self.wait() { + Ok(()) => ps::display(&self.storage.ps1), + Err(flush_error) => { + println!("{flush_error}"); + break; + } + }, + } + } + } }