1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor(lockfile): move anyhow to thiserror (#18178)

This commit is contained in:
Yiyu Lin 2023-03-14 22:55:52 +08:00 committed by GitHub
parent 8d412f6412
commit 96b1ede254
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 30 deletions

2
Cargo.lock generated
View file

@ -1211,11 +1211,11 @@ dependencies = [
name = "deno_lockfile" name = "deno_lockfile"
version = "0.8.0" version = "0.8.0"
dependencies = [ dependencies = [
"anyhow",
"ring", "ring",
"serde", "serde",
"serde_json", "serde_json",
"test_util", "test_util",
"thiserror",
] ]
[[package]] [[package]]

View file

@ -122,6 +122,7 @@ sha2 = { version = "0.10.6", features = ["oid"] }
smallvec = "1.8" smallvec = "1.8"
socket2 = "0.4.7" socket2 = "0.4.7"
tar = "=0.4.38" tar = "=0.4.38"
thiserror = "=1.0.38"
tokio = { version = "=1.25.0", features = ["full"] } tokio = { version = "=1.25.0", features = ["full"] }
tokio-rustls = "0.23.3" tokio-rustls = "0.23.3"
tokio-tungstenite = "0.16.1" tokio-tungstenite = "0.16.1"

View file

@ -101,7 +101,7 @@ shell-escape = "=0.1.5"
tar.workspace = true tar.workspace = true
text-size = "=1.1.0" text-size = "=1.1.0"
text_lines = "=0.6.0" text_lines = "=0.6.0"
thiserror = "=1.0.38" thiserror.workspace = true
tokio.workspace = true tokio.workspace = true
tokio-util.workspace = true tokio-util.workspace = true
tower-lsp.workspace = true tower-lsp.workspace = true

View file

@ -11,10 +11,10 @@ description = "An implementation of a lockfile used in Deno"
path = "lib.rs" path = "lib.rs"
[dependencies] [dependencies]
anyhow.workspace = true
ring.workspace = true ring.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
thiserror.workspace = true
[dev-dependencies] [dev-dependencies]
test_util.workspace = true test_util.workspace = true

15
lockfile/error.rs Normal file
View file

@ -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),
}

View file

@ -1,10 +1,11 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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::collections::BTreeMap;
use std::io::Write; use std::io::Write;
use anyhow::Context;
use anyhow::Error as AnyError;
use ring::digest; use ring::digest;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
@ -109,7 +110,7 @@ pub struct Lockfile {
} }
impl 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. // Writing a lock file always uses the new format.
if overwrite { if overwrite {
return Ok(Lockfile { return Ok(Lockfile {
@ -136,35 +137,20 @@ impl Lockfile {
} }
}; };
let s = result.with_context(|| { let s =
format!("Unable to read lockfile: \"{}\"", filename.display()) result.map_err(|_| Error::ReadError(filename.display().to_string()))?;
})?; let value: serde_json::Value = serde_json::from_str(&s)
let value: serde_json::Value = .map_err(|_| Error::ParseError(filename.display().to_string()))?;
serde_json::from_str(&s).with_context(|| {
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?;
let version = value.get("version").and_then(|v| v.as_str()); let version = value.get("version").and_then(|v| v.as_str());
let content = if version == Some("2") { let content = if version == Some("2") {
serde_json::from_value::<LockfileContent>(value).with_context(|| { serde_json::from_value::<LockfileContent>(value)
format!( .map_err(|_| Error::ParseError(filename.display().to_string()))?
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?
} else { } else {
// If there's no version field, we assume that user is using the old // 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 // 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) let remote: BTreeMap<String, String> = serde_json::from_value(value)
.with_context(|| { .map_err(|_| Error::ParseError(filename.display().to_string()))?;
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?;
LockfileContent { LockfileContent {
version: "2".to_string(), version: "2".to_string(),
remote, remote,
@ -181,7 +167,7 @@ impl Lockfile {
} }
// Synchronize lock file to disk - noop if --lock-write file is not specified. // 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 { if !self.has_content_changed && !self.overwrite {
return Ok(()); return Ok(());
} }