refactor: Switch to using miette for errors instead of anyhow (#198)
Switch to a better error crate that will allow setting help texts for any error we want.
This commit is contained in:
parent
784be9869a
commit
065fa193e3
24 changed files with 364 additions and 143 deletions
|
|
@ -9,6 +9,7 @@ license.workspace = true
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
atty = "0.2"
|
||||
base64 = "0.22.1"
|
||||
blake2 = "0.10.6"
|
||||
|
|
@ -23,7 +24,6 @@ signal-hook = { version = "0.3.17", features = ["extended-siginfo"] }
|
|||
syntect = "5"
|
||||
which = "6"
|
||||
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
clap = { workspace = true, features = ["derive"] }
|
||||
colored.workspace = true
|
||||
|
|
@ -31,6 +31,7 @@ format_serde_error.workspace = true
|
|||
indicatif.workspace = true
|
||||
indicatif-log-bridge.workspace = true
|
||||
log.workspace = true
|
||||
miette.workspace = true
|
||||
once_cell.workspace = true
|
||||
tempdir.workspace = true
|
||||
serde.workspace = true
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use base64::prelude::*;
|
||||
use blake2::{
|
||||
digest::{Update, VariableOutput},
|
||||
|
|
@ -20,6 +19,7 @@ use blake2::{
|
|||
};
|
||||
use format_serde_error::SerdeError;
|
||||
use log::trace;
|
||||
use miette::{miette, IntoDiagnostic, Result};
|
||||
|
||||
use crate::constants::CONTAINER_FILE;
|
||||
|
||||
|
|
@ -35,14 +35,15 @@ pub fn check_command_exists(command: &str) -> Result<()> {
|
|||
trace!("which {command}");
|
||||
if Command::new("which")
|
||||
.arg(command)
|
||||
.output()?
|
||||
.output()
|
||||
.into_diagnostic()?
|
||||
.status
|
||||
.success()
|
||||
{
|
||||
trace!("Command {command} does exist");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
Err(miette!(
|
||||
"Command {command} doesn't exist and is required to build the image"
|
||||
))
|
||||
}
|
||||
|
|
@ -69,9 +70,9 @@ pub fn serde_yaml_err(contents: &str) -> impl Fn(serde_yaml::Error) -> SerdeErro
|
|||
///
|
||||
/// # Errors
|
||||
/// Will error when retries have been expended.
|
||||
pub fn retry<V, F>(attempts: u8, delay: u64, f: F) -> anyhow::Result<V>
|
||||
pub fn retry<V, F>(attempts: u8, delay: u64, f: F) -> miette::Result<V>
|
||||
where
|
||||
F: Fn() -> anyhow::Result<V>,
|
||||
F: Fn() -> miette::Result<V>,
|
||||
{
|
||||
let mut attempts = attempts;
|
||||
loop {
|
||||
|
|
@ -100,9 +101,9 @@ pub fn generate_containerfile_path<T: AsRef<Path>>(path: T) -> Result<PathBuf> {
|
|||
const HASH_SIZE: usize = 8;
|
||||
let mut buf = [0u8; HASH_SIZE];
|
||||
|
||||
let mut hasher = Blake2bVar::new(HASH_SIZE)?;
|
||||
let mut hasher = Blake2bVar::new(HASH_SIZE).into_diagnostic()?;
|
||||
hasher.update(path.as_ref().as_os_str().as_bytes());
|
||||
hasher.finalize_variable(&mut buf)?;
|
||||
hasher.finalize_variable(&mut buf).into_diagnostic()?;
|
||||
|
||||
Ok(PathBuf::from(format!(
|
||||
"{CONTAINER_FILE}.{}",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use clap::ValueEnum;
|
||||
use log::trace;
|
||||
use miette::{miette, IntoDiagnostic, Result};
|
||||
use serde::ser::Serialize;
|
||||
use syntect::{dumps, easy::HighlightLines, highlighting::ThemeSet, parsing::SyntaxSet};
|
||||
|
||||
|
|
@ -42,7 +42,8 @@ pub fn highlight(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> R
|
|||
dumps::from_uncompressed_data(include_bytes!(concat!(
|
||||
env!("OUT_DIR"),
|
||||
"/docker_syntax.bin"
|
||||
)))?
|
||||
)))
|
||||
.into_diagnostic()?
|
||||
} else {
|
||||
SyntaxSet::load_defaults_newlines()
|
||||
};
|
||||
|
|
@ -50,18 +51,18 @@ pub fn highlight(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> R
|
|||
|
||||
let syntax = ss
|
||||
.find_syntax_by_extension(file_type)
|
||||
.ok_or_else(|| anyhow!("Failed to get syntax"))?;
|
||||
.ok_or_else(|| miette!("Failed to get syntax"))?;
|
||||
let mut h = HighlightLines::new(
|
||||
syntax,
|
||||
ts.themes
|
||||
.get(theme.unwrap_or_default().to_string().as_str())
|
||||
.ok_or_else(|| anyhow!("Failed to get highlight theme"))?,
|
||||
.ok_or_else(|| miette!("Failed to get highlight theme"))?,
|
||||
);
|
||||
|
||||
let mut highlighted_lines: Vec<String> = vec![];
|
||||
for line in file.lines() {
|
||||
highlighted_lines.push(syntect::util::as_24_bit_terminal_escaped(
|
||||
&h.highlight_line(line, &ss)?,
|
||||
&h.highlight_line(line, &ss).into_diagnostic()?,
|
||||
false,
|
||||
));
|
||||
}
|
||||
|
|
@ -83,7 +84,11 @@ pub fn highlight_ser<T: Serialize + std::fmt::Debug>(
|
|||
theme: Option<DefaultThemes>,
|
||||
) -> Result<String> {
|
||||
trace!("syntax_highlighting::highlight_ser(file, {file_type}, {theme:?})");
|
||||
highlight(serde_yaml::to_string(file)?.as_str(), file_type, theme)
|
||||
highlight(
|
||||
serde_yaml::to_string(file).into_diagnostic()?.as_str(),
|
||||
file_type,
|
||||
theme,
|
||||
)
|
||||
}
|
||||
|
||||
/// Prints the file with syntax highlighting.
|
||||
|
|
@ -108,6 +113,10 @@ pub fn print_ser<T: Serialize + std::fmt::Debug>(
|
|||
theme: Option<DefaultThemes>,
|
||||
) -> Result<()> {
|
||||
trace!("syntax_highlighting::print_ser(file, {file_type}, {theme:?})");
|
||||
print(serde_yaml::to_string(file)?.as_str(), file_type, theme)?;
|
||||
print(
|
||||
serde_yaml::to_string(file).into_diagnostic()?.as_str(),
|
||||
file_type,
|
||||
theme,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue