Compare commits
No commits in common. "1270576444e594085672419488a0f1b605c06608" and "5df428a364f1e6d26ec7ff2a54507c7d41ae6c41" have entirely different histories.
1270576444
...
5df428a364
4 changed files with 17 additions and 60 deletions
|
@ -19,4 +19,3 @@ I want to learn Rust and see how much effort I can put into making an interopera
|
||||||
- [ ] PS1
|
- [ ] PS1
|
||||||
- [ ] Execute code from a file
|
- [ ] Execute code from a file
|
||||||
- [ ] Clean error handling
|
- [ ] Clean error handling
|
||||||
- [ ] Override Ctrl-C
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::error;
|
use crate::{error::print_error, exit_code::ExitCode};
|
||||||
use crate::error::CdError;
|
|
||||||
use crate::exit_code::ExitCode;
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::{env::set_current_dir, path::PathBuf};
|
use std::{env::set_current_dir, path::PathBuf};
|
||||||
|
@ -19,18 +17,16 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
|
||||||
|
|
||||||
let exit_code: ExitCode;
|
let exit_code: ExitCode;
|
||||||
if path.is_some() {
|
if path.is_some() {
|
||||||
let path = path.unwrap();
|
exit_code = match set_current_dir(path.unwrap()) {
|
||||||
let result = set_current_dir(&path);
|
Ok(()) => ExitCode::success(),
|
||||||
|
Err(_e) => ExitCode::new(2),
|
||||||
if result.is_err() {
|
};
|
||||||
let error = CdError::new(&args[0]);
|
|
||||||
error::print(Box::new(error));
|
|
||||||
exit_code = ExitCode::new(2);
|
|
||||||
} else {
|
} else {
|
||||||
exit_code = ExitCode::success();
|
exit_code = ExitCode::success();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
exit_code = ExitCode::success();
|
if exit_code.get() != 0 {
|
||||||
|
print_error("lol");
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
|
|
|
@ -2,8 +2,7 @@ mod builtins;
|
||||||
pub mod command_builder;
|
pub mod command_builder;
|
||||||
pub mod command_sequence;
|
pub mod command_sequence;
|
||||||
|
|
||||||
use crate::error;
|
use crate::error::print_error;
|
||||||
use crate::error::CommandNotFoundError;
|
|
||||||
use crate::exit_code::ExitCode;
|
use crate::exit_code::ExitCode;
|
||||||
|
|
||||||
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
|
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
|
||||||
|
@ -82,8 +81,11 @@ impl Command for UnixProgram {
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
} else {
|
} else {
|
||||||
let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
|
let message = format!(
|
||||||
error::print(Box::new(error));
|
"{}: command not found",
|
||||||
|
self.command.get_program().to_str().unwrap()
|
||||||
|
);
|
||||||
|
print_error(message.as_str());
|
||||||
ExitCode::not_found()
|
ExitCode::not_found()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
44
src/error.rs
44
src/error.rs
|
@ -1,49 +1,9 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt;
|
|
||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
type Error = Box<dyn Display>;
|
pub fn print_error(message: &str) {
|
||||||
|
|
||||||
pub struct CdError {
|
|
||||||
entry_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CdError {
|
|
||||||
pub fn new(entry_name: &str) -> Self {
|
|
||||||
Self {
|
|
||||||
entry_name: String::from(entry_name),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for CdError {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(f, "cd: no such directory as '{}'", self.entry_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CommandNotFoundError {
|
|
||||||
command: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CommandNotFoundError {
|
|
||||||
pub fn new(command: &str) -> Self {
|
|
||||||
Self {
|
|
||||||
command: String::from(command),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for CommandNotFoundError {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}: command not found", self.command)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn print(error: Error) {
|
|
||||||
let name = env::args().next().unwrap_or(String::from("rash"));
|
let name = env::args().next().unwrap_or(String::from("rash"));
|
||||||
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
eprintln!("{}: {}", name, error);
|
eprintln!("{}: {}", name, message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue