mirror of
https://git.sr.ht/~nixgoat/vento
synced 2025-11-28 15:35:43 +00:00
src: Universal error handling
This commit should unify the way Vento handles errors. Solves #7.
This commit is contained in:
parent
ef5205ce3a
commit
8fe5bca167
|
|
@ -31,10 +31,10 @@ fn main() -> Result<()> {
|
|||
4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf())?,
|
||||
3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("❌ {}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
})?,
|
||||
2 => bail!("❌ {}", "You need to specify a file".red()),
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
2 => bail!("{}", "You need to specify a file".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
|
|
@ -43,19 +43,19 @@ fn main() -> Result<()> {
|
|||
5 => item::drop(&args[3], &args[2], Path::new(&args[4]).to_path_buf())?,
|
||||
4 => item::drop(&args[3], &args[2], match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("❌ {}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
})?,
|
||||
3 => bail!("❌ {}", "You need to specify a file".red()),
|
||||
2 => bail!("❌ {}", "You need to specify a slot".red()),
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
3 => bail!("{}", "You need to specify a file".red()),
|
||||
2 => bail!("{}", "You need to specify a slot".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
_ => match args.len() {
|
||||
3 => item::drop(&args[1], &String::from("active"), Path::new(&args[2]).to_path_buf())?,
|
||||
2 => item::drop(&args[1], &String::from("active"), match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("❌ {}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
})?,
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,21 +28,21 @@ fn main() -> Result<()> {
|
|||
if args[1].contains("--slot=") {
|
||||
match args.len() {
|
||||
3 => item::take(&args[2], &args[1].replace("--slot=", ""))?,
|
||||
2 => bail!("❌ {}", "You need to specify a file".red()),
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
2 => bail!("{}", "You need to specify a file".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
"--help" | "-h" => help::take()?,
|
||||
"-s" => match args.len() {
|
||||
4 => item::take(&args[3], &args[2])?,
|
||||
3 => bail!("❌ {}", "You need to specify a file".red()),
|
||||
2 => bail!("❌ {}", "You need to specify a slot".red()),
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
3 => bail!("{}", "You need to specify a file".red()),
|
||||
2 => bail!("{}", "You need to specify a slot".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
_ => match args.len() {
|
||||
2 => item::take(&args[1], &String::from("active"))?,
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ fn main() -> Result<()> {
|
|||
match args.len() {
|
||||
3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?,
|
||||
2 => inv::list(&args[1].replace("--slot=", ""), "")?,
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
|
|
@ -40,8 +40,8 @@ fn main() -> Result<()> {
|
|||
"-s" => match args.len() {
|
||||
4 => inv::list(&args[2], &args[3])?,
|
||||
3 => inv::list(&args[2], "")?,
|
||||
2 => bail!("❌ {}", "You need to specify a slot.".red()),
|
||||
_ => bail!("❌ {}", "Too many arguments".red()),
|
||||
2 => bail!("{}", "You need to specify a slot.".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
_ => inv::list("active", args[1].as_str())?,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
|
|||
_ => PathBuf::new(),
|
||||
};
|
||||
if home == PathBuf::new() {
|
||||
bail!("❌ {}", "Vento was unable to detect your home folder. Have you configured your environment correctly?".red());
|
||||
bail!("{}", "Vento was unable to detect your home folder. Have you configured your environment correctly?".red());
|
||||
};
|
||||
let vento_dir: PathBuf;
|
||||
let custom_dir = Path::new(&dir_config()?).to_path_buf();
|
||||
|
|
|
|||
12
src/inv.rs
12
src/inv.rs
|
|
@ -59,7 +59,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
|
||||
if !ventodir.is_dir() {
|
||||
bail!(
|
||||
"❌ {}",
|
||||
"{}",
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
|
||||
);
|
||||
}
|
||||
|
|
@ -69,7 +69,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
if dir.to_string().contains("..") {
|
||||
bail!("❌ {}", "Cannot access parent.".red());
|
||||
bail!("{}", "Cannot access parent.".red());
|
||||
}
|
||||
|
||||
if slotdir.is_dir() {
|
||||
|
|
@ -149,8 +149,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
println!(
|
||||
"❌ {}",
|
||||
bail!(
|
||||
"{}",
|
||||
format!(
|
||||
"No such slot or directory. Valid slots are {} and {}.",
|
||||
"active".green().bold(),
|
||||
|
|
@ -171,7 +171,7 @@ pub fn switch() -> Result<()> {
|
|||
.iter()
|
||||
.collect();
|
||||
|
||||
let rename_error = "❌ Vento was unable to switch slots. Try running vento init and try again";
|
||||
let rename_error = "Vento was unable to switch slots. Try running vento init and try again";
|
||||
|
||||
fs::rename(&active, &temp).context(rename_error)?;
|
||||
fs::rename(&inactive, &active).context(rename_error)?;
|
||||
|
|
@ -186,7 +186,7 @@ fn create_slots() -> Result<()> {
|
|||
let active = &common::env_config()?[1];
|
||||
let inactive = &common::env_config()?[2];
|
||||
|
||||
let initialize_error = "❌ Vento was unable to initalize. Do you have the correct permissions";
|
||||
let initialize_error = "Vento was unable to initalize. Do you have the correct permissions?";
|
||||
|
||||
fs::create_dir_all(active).context(initialize_error)?;
|
||||
fs::create_dir_all(inactive).context(initialize_error)?;
|
||||
|
|
|
|||
20
src/item.rs
20
src/item.rs
|
|
@ -36,17 +36,17 @@ pub fn take(file: &String, slot: &String) -> Result<()> {
|
|||
|
||||
if Path::exists(&destpath) {
|
||||
bail!(
|
||||
"❌ {}",
|
||||
"{}",
|
||||
"A file with the same name already exists in your inventory!".red()
|
||||
);
|
||||
} else if sourcepath.is_file() | sourcepath.is_symlink() {
|
||||
fs::copy(&file, &destpath).expect("❌ Vento was unable to copy the file.");
|
||||
fs::remove_file(&file).expect("❌ Vento was unable to remove the file.");
|
||||
fs::copy(&file, &destpath).context("Vento was unable to copy the file.")?;
|
||||
fs::remove_file(&file).context("Vento was unable to remove the file.")?;
|
||||
} else if sourcepath.is_dir() {
|
||||
let options = CopyOptions::new();
|
||||
move_dir(&file, &slotdir, &options).expect("❌ Vento was unable to move the directory.");
|
||||
move_dir(&file, &slotdir, &options).context("Vento was unable to move the directory.")?;
|
||||
} else {
|
||||
println!("❌ {}", "No such file or directory.".red());
|
||||
bail!("{}", "No such file or directory.".red());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -69,17 +69,17 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> {
|
|||
|
||||
if Path::exists(&destpath) {
|
||||
// HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked
|
||||
bail!("❌ {}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.".red());
|
||||
bail!("{}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.".red());
|
||||
} else if sourcepath.is_file() | sourcepath.is_symlink() {
|
||||
fs::copy(&sourcepath, &destpath).context("❌ Vento was unable to copy the file.")?;
|
||||
fs::remove_file(&sourcepath).context("❌ Vento was unable to remove the file.")?;
|
||||
fs::copy(&sourcepath, &destpath).context("Vento was unable to copy the file.")?;
|
||||
fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?;
|
||||
} else if sourcepath.is_dir() {
|
||||
let destpath: PathBuf = Path::new(&dest).to_path_buf();
|
||||
let options = CopyOptions::new();
|
||||
move_dir(&sourcepath, &destpath, &options)
|
||||
.expect("❌ Vento was unable to move the directory.");
|
||||
.context("Vento was unable to move the directory.")?;
|
||||
} else {
|
||||
bail!("❌ {}", "No such file or directory.".red());
|
||||
bail!("{}", "No such file or directory.".red());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue