Skip to content

Commit

Permalink
refactor: Switch to using miette for errors instead of anyhow (#198)
Browse files Browse the repository at this point in the history
Switch to a better error crate that will allow setting help texts for
any error we want.
  • Loading branch information
gmpinder authored Jul 6, 2024
1 parent 784be98 commit 065fa19
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 143 deletions.
146 changes: 143 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ categories = ["command-line-utilities"]
version = "0.8.11"

[workspace.dependencies]
anyhow = "1"
chrono = "0.4"
clap = "4"
colored = "2"
Expand All @@ -19,6 +18,7 @@ indexmap = { version = "2", features = ["serde"] }
indicatif = { version = "0.17", features = ["improved_unicode"] }
indicatif-log-bridge = "0.2"
log = "0.4"
miette = "7"
once_cell = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down Expand Up @@ -73,14 +73,13 @@ shadow-rs = "0.26"
urlencoding = "2"
users = "0.11"

# Workspace dependencies
anyhow.workspace = true
chrono.workspace = true
clap = { workspace = true, features = ["derive", "cargo", "unicode", "env"] }
colored.workspace = true
indexmap.workspace = true
indicatif.workspace = true
log.workspace = true
miette = { workspace = true, features = ["fancy"] }
once_cell.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion recipe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ license.workspace = true
[dependencies]
blue-build-utils = { version = "=0.8.11", path = "../utils" }

anyhow.workspace = true
chrono.workspace = true
colored.workspace = true
log.workspace = true
miette.workspace = true
indexmap.workspace = true
serde.workspace = true
serde_yaml.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion recipe/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{borrow::Cow, path::PathBuf, process};

use anyhow::{bail, Result};
use blue_build_utils::syntax_highlighting::highlight_ser;
use colored::Colorize;
use indexmap::IndexMap;
use log::{error, trace, warn};
use miette::{bail, Result};
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use typed_builder::TypedBuilder;
Expand Down
8 changes: 5 additions & 3 deletions recipe/src/module_ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{borrow::Cow, collections::HashSet, fs, path::Path};

use anyhow::{Context, Result};
use blue_build_utils::constants::{CONFIG_PATH, RECIPE_PATH};
use log::{trace, warn};
use miette::{Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -32,12 +32,14 @@ impl ModuleExt<'_> {
};

let file = fs::read_to_string(&file_path)
.context(format!("Failed to open {}", file_path.display()))?;
.into_diagnostic()
.with_context(|| format!("Failed to open {}", file_path.display()))?;

serde_yaml::from_str::<Self>(&file).map_or_else(
|_| -> Result<Self> {
let module = serde_yaml::from_str::<Module>(&file)
.map_err(blue_build_utils::serde_yaml_err(&file))?;
.map_err(blue_build_utils::serde_yaml_err(&file))
.into_diagnostic()?;
Ok(Self::builder().modules(vec![module]).build())
},
Ok,
Expand Down
12 changes: 8 additions & 4 deletions recipe/src/recipe.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{borrow::Cow, env, fs, path::Path};

use anyhow::{Context, Result};
use blue_build_utils::constants::{
CI_COMMIT_REF_NAME, CI_COMMIT_SHORT_SHA, CI_DEFAULT_BRANCH, CI_MERGE_REQUEST_IID,
CI_PIPELINE_SOURCE, GITHUB_EVENT_NAME, GITHUB_REF_NAME, GITHUB_SHA, PR_EVENT_NUMBER,
};
use chrono::Local;
use indexmap::IndexMap;
use log::{debug, trace, warn};
use miette::{Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -204,16 +204,20 @@ impl<'a> Recipe<'a> {
let file_path = if Path::new(path.as_ref()).is_absolute() {
path.as_ref().to_path_buf()
} else {
std::env::current_dir()?.join(path.as_ref())
std::env::current_dir()
.into_diagnostic()?
.join(path.as_ref())
};

let file = fs::read_to_string(&file_path)
.context(format!("Failed to read {}", file_path.display()))?;
.into_diagnostic()
.with_context(|| format!("Failed to read {}", file_path.display()))?;

debug!("Recipe contents: {file}");

let mut recipe = serde_yaml::from_str::<Recipe>(&file)
.map_err(blue_build_utils::serde_yaml_err(&file))?;
.map_err(blue_build_utils::serde_yaml_err(&file))
.into_diagnostic()?;

recipe.modules_ext.modules = Module::get_modules(&recipe.modules_ext.modules, None)?.into();

Expand Down
2 changes: 1 addition & 1 deletion recipe/src/stage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{borrow::Cow, path::PathBuf};

use anyhow::{bail, Result};
use blue_build_utils::syntax_highlighting::highlight_ser;
use colored::Colorize;
use miette::{bail, Result};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

Expand Down
Loading

0 comments on commit 065fa19

Please sign in to comment.