Refactor cargo-toml-workspace: Rm dep on binstalk-types (#1386)

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-09-25 15:34:40 +10:00 committed by GitHub
parent 4ec44749f1
commit 1fe4702bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 12 deletions

2
Cargo.lock generated
View file

@ -543,11 +543,11 @@ dependencies = [
name = "cargo-toml-workspace" name = "cargo-toml-workspace"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"binstalk-types",
"cargo_toml", "cargo_toml",
"compact_str", "compact_str",
"glob", "glob",
"normalize-path", "normalize-path",
"serde",
"tempfile", "tempfile",
"thiserror", "thiserror",
"tracing", "tracing",

View file

@ -10,11 +10,11 @@ authors = ["Jiahao XU <Jiahao_XU@outlook.com>"]
license = "Apache-2.0 OR MIT" license = "Apache-2.0 OR MIT"
[dependencies] [dependencies]
binstalk-types = { version = "0.6.0", path = "../binstalk-types" }
cargo_toml = "0.16.0" cargo_toml = "0.16.0"
compact_str = { version = "0.7.0", features = ["serde"] } compact_str = { version = "0.7.0", features = ["serde"] }
glob = "0.3.1" glob = "0.3.1"
normalize-path = { version = "0.2.1", path = "../normalize-path" } normalize-path = { version = "0.2.1", path = "../normalize-path" }
serde = "1.0.163"
thiserror = "1.0.40" thiserror = "1.0.40"
tracing = "0.1.37" tracing = "0.1.37"

View file

@ -3,11 +3,11 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use binstalk_types::cargo_toml_binstall::Meta;
use cargo_toml::{Error as CargoTomlError, Manifest}; use cargo_toml::{Error as CargoTomlError, Manifest};
use compact_str::CompactString; use compact_str::CompactString;
use glob::PatternError; use glob::PatternError;
use normalize_path::NormalizePath; use normalize_path::NormalizePath;
use serde::de::DeserializeOwned;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use tracing::{debug, instrument, warn}; use tracing::{debug, instrument, warn};
@ -19,11 +19,14 @@ pub use cargo_toml;
/// ///
/// * `workspace_path` - can be a directory (path to workspace) or /// * `workspace_path` - can be a directory (path to workspace) or
/// a file (path to `Cargo.toml`). /// a file (path to `Cargo.toml`).
pub fn load_manifest_from_workspace( pub fn load_manifest_from_workspace<Metadata: DeserializeOwned>(
workspace_path: impl AsRef<Path>, workspace_path: impl AsRef<Path>,
crate_name: impl AsRef<str>, crate_name: impl AsRef<str>,
) -> Result<Manifest<Meta>, Error> { ) -> Result<Manifest<Metadata>, Error> {
fn inner(workspace_path: &Path, crate_name: &str) -> Result<Manifest<Meta>, Error> { fn inner<Metadata: DeserializeOwned>(
workspace_path: &Path,
crate_name: &str,
) -> Result<Manifest<Metadata>, Error> {
load_manifest_from_workspace_inner(workspace_path, crate_name).map_err(|inner| Error { load_manifest_from_workspace_inner(workspace_path, crate_name).map_err(|inner| Error {
workspace_path: workspace_path.into(), workspace_path: workspace_path.into(),
crate_name: crate_name.into(), crate_name: crate_name.into(),
@ -62,10 +65,10 @@ enum ErrorInner {
} }
#[instrument] #[instrument]
fn load_manifest_from_workspace_inner( fn load_manifest_from_workspace_inner<Metadata: DeserializeOwned>(
workspace_path: &Path, workspace_path: &Path,
crate_name: &str, crate_name: &str,
) -> Result<Manifest<Meta>, ErrorInner> { ) -> Result<Manifest<Metadata>, ErrorInner> {
debug!( debug!(
"Loading manifest of crate {crate_name} from workspace: {}", "Loading manifest of crate {crate_name} from workspace: {}",
workspace_path.display() workspace_path.display()
@ -80,7 +83,7 @@ fn load_manifest_from_workspace_inner(
let mut manifest_paths = vec![manifest_path]; let mut manifest_paths = vec![manifest_path];
while let Some(manifest_path) = manifest_paths.pop() { while let Some(manifest_path) = manifest_paths.pop() {
let manifest = Manifest::<Meta>::from_path_with_metadata(&manifest_path)?; let manifest = Manifest::<Metadata>::from_path_with_metadata(&manifest_path)?;
let name = manifest.package.as_ref().map(|p| &*p.name); let name = manifest.package.as_ref().map(|p| &*p.name);
debug!( debug!(
@ -244,7 +247,8 @@ mod test {
.unwrap() .unwrap()
.join("e2e-tests/manifests/workspace"); .join("e2e-tests/manifests/workspace");
let manifest = load_manifest_from_workspace(&p, "cargo-binstall").unwrap(); let manifest =
load_manifest_from_workspace::<cargo_toml::Value>(&p, "cargo-binstall").unwrap();
let package = manifest.package.unwrap(); let package = manifest.package.unwrap();
assert_eq!(package.name, "cargo-binstall"); assert_eq!(package.name, "cargo-binstall");
assert_eq!(package.version.as_ref().unwrap(), "0.12.0"); assert_eq!(package.version.as_ref().unwrap(), "0.12.0");
@ -252,10 +256,12 @@ mod test {
assert_eq!(manifest.bin[0].name.as_deref().unwrap(), "cargo-binstall"); assert_eq!(manifest.bin[0].name.as_deref().unwrap(), "cargo-binstall");
assert_eq!(manifest.bin[0].path.as_deref().unwrap(), "src/main.rs"); assert_eq!(manifest.bin[0].path.as_deref().unwrap(), "src/main.rs");
let err = load_manifest_from_workspace_inner(&p, "cargo-binstall2").unwrap_err(); let err = load_manifest_from_workspace_inner::<cargo_toml::Value>(&p, "cargo-binstall2")
.unwrap_err();
assert!(matches!(err, ErrorInner::NotFound), "{:#?}", err); assert!(matches!(err, ErrorInner::NotFound), "{:#?}", err);
let manifest = load_manifest_from_workspace(&p, "cargo-watch").unwrap(); let manifest =
load_manifest_from_workspace::<cargo_toml::Value>(&p, "cargo-watch").unwrap();
let package = manifest.package.unwrap(); let package = manifest.package.unwrap();
assert_eq!(package.name, "cargo-watch"); assert_eq!(package.name, "cargo-watch");
assert_eq!(package.version.as_ref().unwrap(), "8.4.0"); assert_eq!(package.version.as_ref().unwrap(), "8.4.0");