From 4a031d01e99ad9013635dbd2832a8f39bce99e31 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Fri, 27 Dec 2024 22:35:42 -0500 Subject: [PATCH] use .to_owned instead of .to_string for &str's --- Cargo.lock | 20 ++++++++++---------- src/commands.rs | 2 +- src/ps.rs | 21 ++++++++------------- src/rc.rs | 4 ++++ src/shell.rs | 45 ++++++++++++++++++++------------------------- 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef2fd3a..c5efd79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,9 +32,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "cc" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" +checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333" dependencies = [ "shlex", ] @@ -269,9 +269,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" dependencies = [ "proc-macro2", ] @@ -299,18 +299,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.216" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.216" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", "quote", @@ -331,9 +331,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "syn" -version = "2.0.91" +version = "2.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035" +checksum = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126" dependencies = [ "proc-macro2", "quote", diff --git a/src/commands.rs b/src/commands.rs index 19e26e3..e9fa766 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -105,7 +105,7 @@ impl ChangeDirectory for Command { } fn change_directory(&self, args: SplitWhitespace) -> Option { - let vec_args: Vec = args.map(|arg| arg.to_string()).collect(); + let vec_args: Vec = args.map(|arg| arg.to_owned()).collect(); match vec_args.first() { Some(arg) => match arg.as_str() { "/" => self.set_current_dir(Path::new("/")), diff --git a/src/ps.rs b/src/ps.rs index 8acf18a..8c9cad1 100644 --- a/src/ps.rs +++ b/src/ps.rs @@ -4,23 +4,18 @@ use color_print::{cformat, cprint}; pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION")); pub fn working_dir_name() -> String { - match std::env::current_dir() { - Ok(pathbuf) => match pathbuf.file_name() { - Some(name) => { - let name_os_string = name.to_os_string(); - match name_os_string == whoami::username_os() && name_os_string != "root" { - true => "~".to_string(), - 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(), } - None => "?".to_string(), - }, - Err(_) => "?".to_string(), - } + }) + }) } pub fn display(ps1: &String) { - // let exit_status = shell_storage.command_exit_status.map(|s| format!(" [{s}] ")).unwrap_or(" ".to_string()); let working_dir_name = cformat!(" {} ", working_dir_name()); cprint!("{}{}λ ", ps1, working_dir_name); } diff --git a/src/rc.rs b/src/rc.rs index 20927e3..773b7ef 100644 --- a/src/rc.rs +++ b/src/rc.rs @@ -111,4 +111,8 @@ 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()) +} + +pub fn none() -> Option { + None } \ No newline at end of file diff --git a/src/shell.rs b/src/shell.rs index 04c7969..400e9d3 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,4 +1,4 @@ -use crate::{ps, commands}; +use crate::{ps, commands, rc}; use std::io; pub struct Config { @@ -18,10 +18,10 @@ struct Storage { impl LambdaShell { pub fn create(config: Config) -> Self { - Self { + Self { storage: Storage { command_exit_status: None, - ps1: ps::DEFAULT_PS.to_string(), + ps1: ps::DEFAULT_PS.to_owned(), }, terminating: false, config, @@ -30,34 +30,29 @@ impl LambdaShell { fn input(&mut self) { let mut input = String::new(); - match io::stdin().read_line(&mut input) { - Ok(_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_string()).exec() - }; - } - Err(read_error) => println!("{read_error}"), - }; + 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> { - match io::Write::flush(&mut io::stdout()) { - Ok(()) => { - self.input(); - Ok(()) - } - Err(flush_error) => { - println!("{flush_error}"); - Err(flush_error) - } - } + io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| { + self.input(); + Ok(()) + }) } pub fn start(&mut self) { - ps::display(&self.storage.ps1); + let rc_file = match self.config.norc { + true => rc::config_file(), + false => rc::none(), + }; + ps::display(&self.storage.ps1); loop { match self.terminating {