From d3beafa6f3a31b810ece4fbee127bdd3b403c236 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Tue, 28 May 2024 15:38:42 +0800 Subject: [PATCH] refactor: adopt thiserror --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + nix/package.nix | 11 ++++++----- src/package_json.rs | 16 ++++++++-------- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bffe7bf..b28aebf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -579,6 +579,7 @@ dependencies = [ "shlex", "simd-json", "strsim", + "thiserror", "trycmd", ] @@ -882,6 +883,26 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml_datetime" version = "0.6.6" diff --git a/Cargo.toml b/Cargo.toml index 0c7c09f..64a96fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ serde = { version = "1.0.203", features = ["derive"] } shlex = "1.3.0" simd-json = "0.13.10" strsim = "0.11.1" +thiserror = "1.0.61" [dev-dependencies] trycmd = "0.15.4" diff --git a/nix/package.nix b/nix/package.nix index 99681fd..4fdbf4a 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -18,6 +18,7 @@ rustPlatform.buildRustPackage rec { root = ../.; fileset = lib.fileset.unions [ ../src + ../tests ../Cargo.lock ../Cargo.toml ]; @@ -27,12 +28,12 @@ rustPlatform.buildRustPackage rec { lockFile = ../Cargo.lock; }; - buildInputs = lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [ - CoreFoundation - Security - IOKit + buildInputs = lib.optionals stdenv.isDarwin [ + darwin.apple_sdk.frameworks.CoreFoundation + darwin.apple_sdk.frameworks.Security + darwin.apple_sdk.frameworks.IOKit darwin.libiconv - ]); + ]; nativeBuildInputs = lib.optionals stdenv.isDarwin [ pkg-config diff --git a/src/package_json.rs b/src/package_json.rs index 4a6c470..4f97e0b 100644 --- a/src/package_json.rs +++ b/src/package_json.rs @@ -2,6 +2,7 @@ use std::{fs, path::Path}; use owo_colors::{OwoColorize as _, Stream}; use serde::Deserialize; +use thiserror::Error; use indexmap::IndexMap; @@ -16,9 +17,12 @@ pub struct PackageJson { pub scripts: AIndexMap, } +#[derive(Error, Debug)] pub enum PackageJsonFromPathError { - FileError(std::io::Error), - ParseError(simd_json::Error), + #[error("error reading file")] + FileError(#[from] std::io::Error), + #[error("error parsing file")] + ParseError(#[from] simd_json::Error), } impl PackageJsonFromPathError { @@ -51,12 +55,8 @@ impl PackageJsonFromPathError { impl PackageJson { pub fn from_path(path: &Path) -> Result { - fs::read(path) - .map_err(PackageJsonFromPathError::FileError) - .and_then(|mut raw| { - simd_json::from_slice::(&mut raw) - .map_err(PackageJsonFromPathError::ParseError) - }) + let mut raw = fs::read(path)?; + Ok(simd_json::from_slice::(&mut raw)?) } pub fn from_path_safe(path: &Path) -> Option {