From 0fc1e9c4be574fd36b73f9a969de7eb4f0048605 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Mon, 22 Jul 2024 23:43:12 +0300 Subject: [PATCH] Use eyre for error handling Miette is very good but for our use cases, wrapping lib errors with .into_diagnostic() calls is very verbose. --- Cargo.lock | 192 ++++++++++++++++++---------------- Cargo.toml | 7 +- src/app/generator.rs | 4 +- src/app/versions.rs | 8 +- src/cli.rs | 38 ++++--- src/filer/mod.rs | 17 +-- src/filer/native.rs | 17 +-- src/filer/web.rs | 4 +- src/main.rs | 2 +- src/mod_ids.rs | 8 +- src/templates/mod.rs | 49 ++++----- src/web.rs | 10 +- version_resolver/Cargo.toml | 3 +- version_resolver/src/index.rs | 2 +- version_resolver/src/main.rs | 9 +- version_resolver/src/maven.rs | 12 +-- version_resolver/src/web.rs | 8 +- version_resolver/src/xml.rs | 10 +- 18 files changed, 203 insertions(+), 197 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ec1e9f..5e2cac8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.22.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" dependencies = [ "gimli", ] @@ -271,9 +271,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.73" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", @@ -284,15 +284,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "backtrace-ext" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50" -dependencies = [ - "backtrace", -] - [[package]] name = "base64" version = "0.22.1" @@ -423,6 +414,33 @@ dependencies = [ "zeroize", ] +[[package]] +name = "color-eyre" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "colorchoice" version = "1.0.1" @@ -618,6 +636,16 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fastrand" version = "2.1.0" @@ -795,9 +823,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.29.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" @@ -971,6 +999,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.2.6" @@ -1009,12 +1043,6 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" -[[package]] -name = "is_ci" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" - [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -1100,37 +1128,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "miette" -version = "7.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4edc8853320c2a0dab800fbda86253c8938f6ea88510dc92c5f1ed20e794afc1" -dependencies = [ - "backtrace", - "backtrace-ext", - "cfg-if", - "miette-derive", - "owo-colors", - "supports-color", - "supports-hyperlinks", - "supports-unicode", - "terminal_size", - "textwrap", - "thiserror", - "unicode-width", -] - -[[package]] -name = "miette-derive" -version = "7.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcf09caffaac8068c346b6df2a7fc27a177fd20b39421a39ce0a211bde679a6c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "mime" version = "0.3.17" @@ -1234,9 +1231,9 @@ dependencies = [ [[package]] name = "object" -version = "0.36.1" +version = "0.32.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" dependencies = [ "memchr", ] @@ -1303,9 +1300,9 @@ dependencies = [ [[package]] name = "owo-colors" -version = "4.0.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" [[package]] name = "parking" @@ -1800,6 +1797,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -1892,27 +1898,6 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "supports-color" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9829b314621dfc575df4e409e79f9d6a66a3bd707ab73f23cb4aa3a854ac854f" -dependencies = [ - "is_ci", -] - -[[package]] -name = "supports-hyperlinks" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c0a1e5168041f5f3ff68ff7d95dcb9c8749df29f6e7e89ada40dd4c9de404ee" - -[[package]] -name = "supports-unicode" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" - [[package]] name = "syn" version = "2.0.71" @@ -1970,9 +1955,10 @@ dependencies = [ "bytes", "clap", "cliclack", + "color-eyre", + "eyre", "futures", "js-sys", - "miette", "reqwest", "rfd", "serde", @@ -1987,16 +1973,6 @@ dependencies = [ "zip", ] -[[package]] -name = "terminal_size" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" -dependencies = [ - "rustix", - "windows-sys 0.48.0", -] - [[package]] name = "textwrap" version = "0.16.1" @@ -2028,6 +2004,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.8.0" @@ -2180,6 +2166,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", ] [[package]] @@ -2268,6 +2276,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" @@ -2285,9 +2299,9 @@ name = "version_resolver" version = "1.0.0" dependencies = [ "clap", + "eyre", "flexver-rs", "js-sys", - "miette", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 004b8d2..c636500 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" members = ["version_resolver"] [workspace.dependencies] -miette = "7.1" reqwest = "0.12" xml_dom = "0.2.6" wasm-bindgen = "0.2.92" @@ -20,6 +19,8 @@ tokio = { version = "1", features = ["full"] } rfd = "0.14" cliclack = "0.3.2" clap = { version = "4.5", features = ["derive"] } +eyre = { version = "0.6" } +color-eyre = { version = "0.6" } [workspace.dependencies.web-sys] version = "0.3.64" @@ -42,7 +43,7 @@ features = [ crate-type = ["cdylib", "rlib"] [dependencies] -miette = { workspace = true } +eyre = { workspace = true } reqwest = { workspace = true } futures = "0.3" zip = { version = "2.1.3", default-features = false, features = ["deflate"] } @@ -53,7 +54,7 @@ version_resolver = { path = "version_resolver" } bytes = "1.6" [target.'cfg(not(target_family = "wasm"))'.dependencies] -miette = { workspace = true, features = ["fancy"] } +color-eyre = { workspace = true } tokio = { workspace = true } cliclack = { workspace = true } clap = { workspace = true } diff --git a/src/app/generator.rs b/src/app/generator.rs index 19da425..31c0d77 100644 --- a/src/app/generator.rs +++ b/src/app/generator.rs @@ -6,9 +6,9 @@ use crate::tap::Tap; use crate::templates::*; use crate::{MappingSet, ProjectType}; use bytes::Bytes; +use eyre::Result; use futures::future::join_all; use futures::{join, FutureExt}; -use miette::{IntoDiagnostic, Result}; use std::future::Future; use std::pin::Pin; use std::sync::Arc; @@ -63,7 +63,7 @@ pub async fn generate(app: &super::GeneratorApp, filer_provider: &impl crate::fi context.put("PLUGIN_VERSION", crate::versions::PLUGIN_VERSION); // Setup version resolving - let client = Arc::new(reqwest::ClientBuilder::new().build().into_diagnostic()?); + let client = Arc::new(reqwest::ClientBuilder::new().build()?); let versions = crate::app::versions::get_version_index(client.clone(), &game_version).await?; let mut files: Vec>>>>> = vec![Box::pin(shared::shared_files(client.clone()))]; diff --git a/src/app/versions.rs b/src/app/versions.rs index a7f94bb..f58cdb2 100644 --- a/src/app/versions.rs +++ b/src/app/versions.rs @@ -5,7 +5,7 @@ pub const LOOM_VERSION: &'static str = "1.6-SNAPSHOT"; pub const PLUGIN_VERSION: &'static str = "3.4-SNAPSHOT"; -use miette::Result; +use eyre::Result; use reqwest::Client; use version_resolver::index::Versions; use version_resolver::minecraft::MinecraftVersion; @@ -15,16 +15,16 @@ pub async fn get_version_index( client: std::sync::Arc, game_version: &MinecraftVersion, ) -> Result { - use miette::{miette, IntoDiagnostic}; + use eyre::eyre; use version_resolver::index::VersionIndex; let json = crate::templates::download_relative_text(client, "version_index.json").await?; - let index: VersionIndex = serde_json::from_str(&json).into_diagnostic()?; + let index: VersionIndex = serde_json::from_str(&json)?; index .versions .get(game_version) .ok_or_else(|| { - miette!( + eyre!( "Could not find version index for version {}", game_version.version() ) diff --git a/src/cli.rs b/src/cli.rs index 25d3c32..9d3a8f5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use clap::Parser; use cliclack::{confirm, input, intro, multiselect, outro, select, spinner}; -use miette::{miette, Context, IntoDiagnostic, Result}; +use eyre::{eyre, Context, Result}; use strum::IntoEnumIterator; use version_resolver::minecraft::MinecraftVersion; use std::path::PathBuf; @@ -24,6 +24,7 @@ struct Args { } pub async fn main() -> Result<()> { + color_eyre::install()?; let args = Args::parse(); if args.zip { let (file, default_name) = if let Some(output) = &args.output { @@ -55,16 +56,15 @@ pub async fn main() -> Result<()> { if dir.exists() { if !dir.is_dir() { - return Err(miette!("File {} is not a directory", dir.to_string_lossy())); + return Err(eyre!("File {} is not a directory", dir.to_string_lossy())); } // Check that the directory is empty. let mut iter = tokio::fs::read_dir(&dir) .await - .into_diagnostic() .wrap_err("Could not check if the output directory is empty")?; - if iter.next_entry().await.into_diagnostic()?.is_some() { - return Err(miette!("Output directory {} is not empty", dir.to_string_lossy())); + if iter.next_entry().await?.is_some() { + return Err(eyre!("Output directory {} is not empty", dir.to_string_lossy())); } } @@ -88,26 +88,26 @@ where spinner.start("Generating..."); crate::generator::generate(&app, &filer_provider).await?; spinner.stop("Done!"); - outro(format!("Generated into {}!", output_name_provider(&app))).into_diagnostic()?; + outro(format!("Generated into {}!", output_name_provider(&app)))?; Ok(()) } fn prompt(default_name: Option<&str>) -> Result { - intro("Architectury Template Generator").into_diagnostic()?; + intro("Architectury Template Generator")?; let mut mod_name = input("Mod name"); if let Some(name) = default_name { mod_name = mod_name.default_input(name); } - let mod_name: String = mod_name.interact().into_diagnostic()?; + let mod_name: String = mod_name.interact()?; let mod_id: String = input("Mod ID") .default_input(&crate::mod_ids::to_mod_id(&mod_name)) .validate_interactively(ModIdValidate) - .interact().into_diagnostic()?; + .interact()?; let package_name: String = input("Package name") - .interact().into_diagnostic()?; + .interact()?; let mut versions: Vec<_> = MinecraftVersion::iter() .map(|version| { @@ -117,7 +117,7 @@ fn prompt(default_name: Option<&str>) -> Result { versions.reverse(); // newest first let game_version = select("Minecraft version") .items(&versions) - .interact().into_diagnostic()?; + .interact()?; let mapping_sets: Vec<_> = MappingSet::iter() .map(|set| { @@ -126,7 +126,7 @@ fn prompt(default_name: Option<&str>) -> Result { .collect(); let mapping_set = select("Mappings") .items(&mapping_sets) - .interact().into_diagnostic()?; + .interact()?; let mut project_types = vec![ (ProjectType::Multiplatform, "Multiplatform", ""), @@ -139,7 +139,7 @@ fn prompt(default_name: Option<&str>) -> Result { } let project_type: ProjectType = select("Project type") .items(&project_types) - .interact().into_diagnostic()?; + .interact()?; let mut subprojects = Subprojects::default(); let mut dependencies = Dependencies::default(); @@ -154,7 +154,7 @@ fn prompt(default_name: Option<&str>) -> Result { subproject_options.retain(|(s, _, _)| s.is_available_on(&game_version)); let chosen_subprojects = multiselect("Mod loaders") .items(&subproject_options) - .interact().into_diagnostic()?; + .interact()?; for subproject in chosen_subprojects { subproject.apply_to(&mut subprojects); @@ -163,12 +163,12 @@ fn prompt(default_name: Option<&str>) -> Result { if subprojects.fabric && subprojects.quilt { subprojects.fabric_likes = confirm("Fabric-like subproject (shared code between Fabric and Quilt)?") .initial_value(subprojects.fabric_likes) - .interact().into_diagnostic()?; + .interact()?; } dependencies.architectury_api = confirm("Architectury API?") .initial_value(dependencies.architectury_api) - .interact().into_diagnostic()?; + .interact()?; } let generator = GeneratorApp { @@ -185,16 +185,14 @@ fn prompt(default_name: Option<&str>) -> Result { } fn get_current_dir() -> Result { - std::env::current_dir() - .into_diagnostic() - .wrap_err("Couldn't get current directory") + std::env::current_dir().wrap_err("Couldn't get current directory") } // TODO: Can this be a function ref? struct ModIdValidate; impl cliclack::Validate for ModIdValidate { - type Err = miette::Error; + type Err = eyre::Report; fn validate(&self, input: &String) -> Result<()> { crate::mod_ids::validate_mod_id(input) diff --git a/src/filer/mod.rs b/src/filer/mod.rs index 3cfd3b7..47b397e 100644 --- a/src/filer/mod.rs +++ b/src/filer/mod.rs @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use miette::{IntoDiagnostic, Result}; +use eyre::Result; use std::collections::HashSet; use std::io::{Cursor, Seek, Write}; use zip::write::SimpleFileOptions; @@ -72,19 +72,12 @@ where let directory = format!("{}/", parts[0..=i].join("/")); if self.directories.insert(directory.clone()) { - self.writer - .add_directory(directory, SimpleFileOptions::default()) - .into_diagnostic()?; + self.writer.add_directory(directory, SimpleFileOptions::default())?; } } - self.writer - .start_file( - path, - SimpleFileOptions::default().unix_permissions(permissions.unix()), - ) - .into_diagnostic()?; - self.writer.write_all(content).into_diagnostic()?; + self.writer.start_file(path, SimpleFileOptions::default().unix_permissions(permissions.unix()))?; + self.writer.write_all(content)?; Ok(()) } @@ -114,7 +107,7 @@ impl FilerProvider for ZipFilerProvider { file_name = custom_name + ".zip"; } } - writer.finish().into_diagnostic()?; + writer.finish()?; } self.0.write(file_name, cursor.get_ref()).await diff --git a/src/filer/native.rs b/src/filer/native.rs index dc72d6e..4b97880 100644 --- a/src/filer/native.rs +++ b/src/filer/native.rs @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use miette::{miette, Context, IntoDiagnostic, Result}; +use eyre::{eyre, Context, Result}; use std::{fs, path}; pub struct DirectoryFilerProvider<'a>(pub &'a path::Path); @@ -23,10 +23,11 @@ struct DirectoryFiler<'a> { #[cfg(target_family = "unix")] fn update_permissions(path: &path::Path, permissions: &super::FilePermissions) -> Result<()> { use std::os::unix::fs::PermissionsExt; - let file_permissions = fs::metadata(path).into_diagnostic()?.permissions(); + let file_permissions = fs::metadata(path)?.permissions(); let new_mode = file_permissions.mode() | permissions.unix(); let new_permissions = fs::Permissions::from_mode(new_mode); - fs::set_permissions(path, new_permissions).into_diagnostic() + fs::set_permissions(path, new_permissions)?; + Ok(()) } #[cfg(not(target_family = "unix"))] @@ -46,10 +47,10 @@ impl<'a> super::Filer for DirectoryFiler<'a> { full_path.push(path); if let Some(parent) = full_path.parent() { - fs::create_dir_all(parent).into_diagnostic()?; + fs::create_dir_all(parent)?; } - fs::write(&full_path, content).into_diagnostic()?; + fs::write(&full_path, content)?; update_permissions(&full_path, permissions)?; Ok(()) } @@ -73,12 +74,12 @@ impl super::ZipWriteTarget for FsZipWriteTarget { let exists = tokio::fs::try_exists(path) .await - .into_diagnostic() .wrap_err_with(|| format!("Could not check if file {} exists", path.to_string_lossy()))?; if exists { - return Err(miette!("Output file {} already exists!", path.to_string_lossy())); + return Err(eyre!("Output file {} already exists!", path.to_string_lossy())); } - tokio::fs::write(path, data).await.into_diagnostic() + tokio::fs::write(path, data).await?; + Ok(()) } } diff --git a/src/filer/web.rs b/src/filer/web.rs index 34d46f6..6c44285 100644 --- a/src/filer/web.rs +++ b/src/filer/web.rs @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use miette::{IntoDiagnostic, Result}; +use eyre::Result; pub struct ZipSaveDialog; @@ -16,7 +16,7 @@ impl super::ZipWriteTarget for ZipSaveDialog { .await; if let Some(file) = saved { - file.write(data).await.into_diagnostic()?; + file.write(data).await?; } Ok(()) diff --git a/src/main.rs b/src/main.rs index 981c7ec..eb78346 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ #[cfg(not(target_family = "wasm"))] #[tokio::main] -async fn main() -> miette::Result<()> { +async fn main() -> eyre::Result<()> { templateer::cli::main().await } diff --git a/src/mod_ids.rs b/src/mod_ids.rs index fe9213f..94260e3 100644 --- a/src/mod_ids.rs +++ b/src/mod_ids.rs @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use miette::{miette, Result}; +use eyre::{eyre, Result}; const MIN_LENGTH: usize = 2; const MAX_LENGTH: usize = 64; @@ -18,7 +18,7 @@ fn is_valid_in_id(c: char) -> bool { pub fn validate_mod_id>(id: S) -> Result<()> { let id = id.as_ref(); if id.len() < MIN_LENGTH || id.len() > MAX_LENGTH { - return Err(miette!( + return Err(eyre!( "Length must be between {} and {}", MIN_LENGTH, MAX_LENGTH @@ -29,12 +29,12 @@ pub fn validate_mod_id>(id: S) -> Result<()> { let head = chars.next().unwrap(); if !is_valid_id_start(head) { - return Err(miette!("'{}' is not valid at the start of an ID", head)); + return Err(eyre!("'{}' is not valid at the start of an ID", head)); } for c in chars { if !is_valid_in_id(c) { - return Err(miette!("'{}' is not valid in IDs", c)); + return Err(eyre!("'{}' is not valid in IDs", c)); } } diff --git a/src/templates/mod.rs b/src/templates/mod.rs index 9358dad..a2c0624 100644 --- a/src/templates/mod.rs +++ b/src/templates/mod.rs @@ -36,67 +36,68 @@ pub fn compose_file_path(dir: &str, file_name: &str, include_dir: bool) -> Strin } #[cfg(target_family = "wasm")] -pub fn compose_relative_url(url: &str) -> miette::Result { +pub fn compose_relative_url(url: &str) -> eyre::Result { use crate::web::ResultExt; - use miette::{miette, IntoDiagnostic}; + use eyre::eyre; let document = web_sys::window() - .ok_or_else(|| miette!("Could not find window"))? + .ok_or_else(|| eyre!("Could not find window"))? .document() - .ok_or_else(|| miette!("Could not find document"))?; - let document_url = document.url().to_miette()?; + .ok_or_else(|| eyre!("Could not find document"))?; + let document_url = document.url().to_eyre()?; let base_url = if let Some(slash_index) = document_url.rfind("/") { &document_url[0..=slash_index] } else { &document_url }; - let base = reqwest::Url::parse(base_url).into_diagnostic()?; - reqwest::Url::options() - .base_url(Some(&base)) - .parse(url) - .into_diagnostic() + let base = reqwest::Url::parse(base_url)?; + Ok( + reqwest::Url::options() + .base_url(Some(&base)) + .parse(url)? + ) } #[cfg(target_family = "wasm")] pub async fn download_relative_text( client: std::sync::Arc, url: &str, -) -> miette::Result { - use miette::{miette, IntoDiagnostic}; +) -> eyre::Result { + use eyre::eyre; let parsed_url = compose_relative_url(url)?; - let response = client.get(parsed_url).send().await.into_diagnostic()?; + let response = client.get(parsed_url).send().await?; if !response.status().is_success() { - return Err(miette!( + return Err(eyre!( "Could not download {}: got status code {}", url, response.status() )); } - response.text().await.into_diagnostic() + Ok(response.text().await?) } #[cfg(target_family = "wasm")] pub async fn download_relative_binary( client: std::sync::Arc, url: &str, -) -> miette::Result { - use miette::{miette, IntoDiagnostic}; +) -> eyre::Result { + use eyre::eyre; let parsed_url = compose_relative_url(url)?; - let response = client.get(parsed_url).send().await.into_diagnostic()?; + let response = client.get(parsed_url).send().await?; if !response.status().is_success() { - return Err(miette!( + return Err(eyre!( "Could not download {}: got status code {}", url, response.status() )); } - response.bytes().await.into_diagnostic() + Ok(response.bytes().await?) } macro_rules! file_data { @@ -124,7 +125,7 @@ macro_rules! file_data_raw { #[cfg(not(target_family = "wasm"))] async fn $fn_name( _client: std::sync::Arc, - ) -> miette::Result { + ) -> eyre::Result { let path = crate::templates::compose_file_path($dir, $file_name, $include_dir_in_target); Ok(crate::templates::FileData { @@ -137,7 +138,7 @@ macro_rules! file_data_raw { #[cfg(target_family = "wasm")] async fn $fn_name( client: std::sync::Arc, - ) -> miette::Result { + ) -> eyre::Result { let path = crate::templates::compose_file_path($dir, $file_name, $include_dir_in_target); let url = format!("templates/{}/{}", $dir.replace("-", "_"), $file_name); @@ -151,8 +152,8 @@ macro_rules! file_data_raw { macro_rules! file_list { ($vis:vis $fn_name:ident, $($file_fn:ident)+) => { - $vis async fn $fn_name(client: std::sync::Arc) -> miette::Result> { - let mut output: Vec>>>> = Vec::new(); + $vis async fn $fn_name(client: std::sync::Arc) -> eyre::Result> { + let mut output: Vec>>>> = Vec::new(); $( output.push(Box::pin($file_fn(client.clone()))); )+ diff --git a/src/web.rs b/src/web.rs index daa1157..df22939 100644 --- a/src/web.rs +++ b/src/web.rs @@ -3,19 +3,19 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. use js_sys::{Array, JsString}; -use miette::{miette, Result}; +use eyre::{eyre, Result}; use strum::IntoEnumIterator; use wasm_bindgen::prelude::*; use crate::filer; pub trait ResultExt { - fn to_miette(self) -> Result; + fn to_eyre(self) -> Result; } -impl ResultExt for std::result::Result { - fn to_miette(self) -> Result { - self.map_err(|err| miette!("{:?}", err)) +impl ResultExt for Result { + fn to_eyre(self) -> Result { + self.map_err(|err| eyre!("{:?}", err)) } } diff --git a/version_resolver/Cargo.toml b/version_resolver/Cargo.toml index 7c856b6..c7cc77c 100644 --- a/version_resolver/Cargo.toml +++ b/version_resolver/Cargo.toml @@ -4,7 +4,7 @@ version = "1.0.0" edition = "2021" [dependencies] -miette = { workspace = true } +eyre = { workspace = true } reqwest = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } @@ -14,7 +14,6 @@ flexver-rs = "0.1.2" [target.'cfg(not(target_family = "wasm"))'.dependencies] clap = { workspace = true } -miette = { workspace = true, features = ["fancy"] } tokio = { workspace = true } xml_dom = { workspace = true } diff --git a/version_resolver/src/index.rs b/version_resolver/src/index.rs index 10dd14a..ff5f91a 100644 --- a/version_resolver/src/index.rs +++ b/version_resolver/src/index.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; -use miette::Result; +use eyre::Result; use serde::{Deserialize, Serialize}; use strum::IntoEnumIterator; diff --git a/version_resolver/src/main.rs b/version_resolver/src/main.rs index 3adcd3e..8677071 100644 --- a/version_resolver/src/main.rs +++ b/version_resolver/src/main.rs @@ -14,25 +14,24 @@ struct Cli { #[tokio::main] #[cfg(not(target_family = "wasm"))] -async fn main() -> miette::Result<()> { - use miette::IntoDiagnostic; +async fn main() -> eyre::Result<()> { use version_resolver::index::VersionIndex; let cli = Cli::parse(); let client = reqwest::Client::new(); let index = VersionIndex::resolve(&client).await?; - let json = serde_json::to_string_pretty(&index).into_diagnostic()?; + let json = serde_json::to_string_pretty(&index)?; let path = cli .output .unwrap_or(std::path::PathBuf::from("version_index.json")); if let Some(parent) = path.parent() { if !parent.exists() { - std::fs::create_dir_all(parent).into_diagnostic()?; + std::fs::create_dir_all(parent)?; } } - std::fs::write(path, json).into_diagnostic()?; + std::fs::write(path, json)?; Ok(()) } diff --git a/version_resolver/src/maven.rs b/version_resolver/src/maven.rs index dc21b3b..5ce94e2 100644 --- a/version_resolver/src/maven.rs +++ b/version_resolver/src/maven.rs @@ -6,7 +6,7 @@ use crate::{ minecraft::MinecraftVersion, xml::{read_node, XmlNode}, }; -use miette::{miette, IntoDiagnostic, Result}; +use eyre::{eyre, Result}; use reqwest::Client; const FABRIC_MAVEN: &'static str = "https://maven.fabricmc.net"; @@ -134,17 +134,17 @@ async fn download_maven_metadata(client: &Client, library: &MavenLibrary) -> Res library.group().replace(".", "/"), library.name() ); - let response = client.get(&url).send().await.into_diagnostic()?; + let response = client.get(&url).send().await?; if !response.status().is_success() { - return Err(miette!( + return Err(eyre!( "Could not download {}: got status code {}", url, response.status() )); } - let text = response.text().await.into_diagnostic()?; + let text = response.text().await?; read_node(text.as_str()) } @@ -182,7 +182,7 @@ where { let metadata = download_maven_metadata(client, &library).await?; get_latest_version_matching(&metadata, filter) - .ok_or_else(|| miette!("Could not find latest version for {}", library)) + .ok_or_else(|| eyre!("Could not find latest version for {}", library)) } pub async fn resolve_latest_version( @@ -191,5 +191,5 @@ pub async fn resolve_latest_version( ) -> Result { let metadata = download_maven_metadata(client, &library).await?; get_latest_version(&metadata) - .ok_or_else(|| miette!("Could not find latest version for {}", library)) + .ok_or_else(|| eyre!("Could not find latest version for {}", library)) } diff --git a/version_resolver/src/web.rs b/version_resolver/src/web.rs index 42a8725..c147cc4 100644 --- a/version_resolver/src/web.rs +++ b/version_resolver/src/web.rs @@ -2,14 +2,14 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use miette::{miette, Result}; +use eyre::{eyre, Result}; pub trait ResultExt { - fn to_miette(self) -> Result; + fn to_eyre(self) -> Result; } impl ResultExt for Result { - fn to_miette(self) -> Result { - self.map_err(|err| miette!("{:?}", err)) + fn to_eyre(self) -> Result { + self.map_err(|err| eyre!("{:?}", err)) } } diff --git a/version_resolver/src/xml.rs b/version_resolver/src/xml.rs index b4b2233..6559a0f 100644 --- a/version_resolver/src/xml.rs +++ b/version_resolver/src/xml.rs @@ -23,7 +23,7 @@ pub use native::read_node; #[cfg(not(target_family = "wasm"))] mod native { use super::XmlNode; - use miette::{IntoDiagnostic, Result}; + use eyre::Result; use std::iter::Map; use std::vec::IntoIter; use xml_dom::level2::{Element, Node, NodeType, RefNode}; @@ -62,7 +62,7 @@ mod native { } pub fn read_node(input: &str) -> Result { - let ref_node = read_xml(input).into_diagnostic()?; + let ref_node = read_xml(input)?; Ok(XmlNodeImpl(ref_node)) } @@ -86,7 +86,7 @@ pub use web::read_node; mod web { use super::XmlNode; use crate::web::ResultExt; - use miette::Result; + use eyre::Result; use std::iter::Map; use std::vec::IntoIter; use wasm_bindgen::JsCast; @@ -120,10 +120,10 @@ mod web { } pub fn read_node(input: &str) -> Result { - let parser = DomParser::new().to_miette()?; + let parser = DomParser::new().to_eyre()?; let document = parser .parse_from_string(input, SupportedType::ApplicationXml) - .to_miette()?; + .to_eyre()?; Ok(XmlNodeImpl(document.into())) }