Skip to content

Commit

Permalink
refactor: adopt thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed May 28, 2024
1 parent ebdb244 commit d3beafa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions nix/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rustPlatform.buildRustPackage rec {
root = ../.;
fileset = lib.fileset.unions [
../src
../tests
../Cargo.lock
../Cargo.toml
];
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fs, path::Path};

use owo_colors::{OwoColorize as _, Stream};
use serde::Deserialize;
use thiserror::Error;

use indexmap::IndexMap;

Expand All @@ -16,9 +17,12 @@ pub struct PackageJson {
pub scripts: AIndexMap<String, String>,
}

#[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 {
Expand Down Expand Up @@ -51,12 +55,8 @@ impl PackageJsonFromPathError {

impl PackageJson {
pub fn from_path(path: &Path) -> Result<Self, PackageJsonFromPathError> {
fs::read(path)
.map_err(PackageJsonFromPathError::FileError)
.and_then(|mut raw| {
simd_json::from_slice::<PackageJson>(&mut raw)
.map_err(PackageJsonFromPathError::ParseError)
})
let mut raw = fs::read(path)?;
Ok(simd_json::from_slice::<PackageJson>(&mut raw)?)
}

pub fn from_path_safe(path: &Path) -> Option<Self> {
Expand Down

0 comments on commit d3beafa

Please sign in to comment.