Skip to content

Commit

Permalink
refactor(lockfile): move anyhow to thiserror (#18178)
Browse files Browse the repository at this point in the history
  • Loading branch information
attila-lin authored and pull[bot] committed Sep 4, 2023
1 parent 746b4de commit 1795620
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion 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 @@ -122,6 +122,7 @@ sha2 = { version = "0.10.6", features = ["oid"] }
smallvec = "1.8"
socket2 = "0.4.7"
tar = "=0.4.38"
thiserror = "=1.0.38"
tokio = { version = "=1.25.0", features = ["full"] }
tokio-rustls = "0.23.3"
tokio-tungstenite = "0.16.1"
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ shell-escape = "=0.1.5"
tar.workspace = true
text-size = "=1.1.0"
text_lines = "=0.6.0"
thiserror = "=1.0.38"
thiserror.workspace = true
tokio.workspace = true
tokio-util.workspace = true
tower-lsp.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion lockfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ description = "An implementation of a lockfile used in Deno"
path = "lib.rs"

[dependencies]
anyhow.workspace = true
ring.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true

[dev-dependencies]
test_util.workspace = true
15 changes: 15 additions & 0 deletions lockfile/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use thiserror::Error;

#[derive(Debug, Error)]
pub enum LockfileError {
#[error(transparent)]
Io(#[from] std::io::Error),

#[error("Unable to read lockfile: \"{0}\"")]
ReadError(String),

#[error("Unable to parse contents of lockfile: \"{0}\"")]
ParseError(String),
}
40 changes: 13 additions & 27 deletions lockfile/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

mod error;
pub use error::LockfileError as Error;

use std::collections::BTreeMap;
use std::io::Write;

use anyhow::Context;
use anyhow::Error as AnyError;
use ring::digest;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -109,7 +110,7 @@ pub struct Lockfile {
}

impl Lockfile {
pub fn new(filename: PathBuf, overwrite: bool) -> Result<Lockfile, AnyError> {
pub fn new(filename: PathBuf, overwrite: bool) -> Result<Lockfile, Error> {
// Writing a lock file always uses the new format.
if overwrite {
return Ok(Lockfile {
Expand All @@ -136,35 +137,20 @@ impl Lockfile {
}
};

let s = result.with_context(|| {
format!("Unable to read lockfile: \"{}\"", filename.display())
})?;
let value: serde_json::Value =
serde_json::from_str(&s).with_context(|| {
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?;
let s =
result.map_err(|_| Error::ReadError(filename.display().to_string()))?;
let value: serde_json::Value = serde_json::from_str(&s)
.map_err(|_| Error::ParseError(filename.display().to_string()))?;
let version = value.get("version").and_then(|v| v.as_str());
let content = if version == Some("2") {
serde_json::from_value::<LockfileContent>(value).with_context(|| {
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?
serde_json::from_value::<LockfileContent>(value)
.map_err(|_| Error::ParseError(filename.display().to_string()))?
} else {
// If there's no version field, we assume that user is using the old
// version of the lockfile. We'll migrate it in-place into v2 and it
// will be writte in v2 if user uses `--lock-write` flag.
// will be written in v2 if user uses `--lock-write` flag.
let remote: BTreeMap<String, String> = serde_json::from_value(value)
.with_context(|| {
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?;
.map_err(|_| Error::ParseError(filename.display().to_string()))?;
LockfileContent {
version: "2".to_string(),
remote,
Expand All @@ -181,7 +167,7 @@ impl Lockfile {
}

// Synchronize lock file to disk - noop if --lock-write file is not specified.
pub fn write(&self) -> Result<(), AnyError> {
pub fn write(&self) -> Result<(), Error> {
if !self.has_content_changed && !self.overwrite {
return Ok(());
}
Expand Down

0 comments on commit 1795620

Please sign in to comment.