Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support workspace inheritance #104

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) fn try_get_workspace_manifest(manifest_dir: &Path) -> Result<Workspac
let manifest_str = fs::read_to_string(cargo_toml_path)?;
let mut manifest: WorkspaceManifest = basic_toml::from_str(&manifest_str)?;

fix_dependencies(&mut manifest.workspace.dependencies, manifest_dir);
fix_patches(&mut manifest.patch, manifest_dir);
fix_replacements(&mut manifest.replace, manifest_dir);

Expand Down Expand Up @@ -66,12 +67,27 @@ fn fix_replacements(replacements: &mut Map<String, Patch>, dir: &Path) {

#[derive(Deserialize, Default, Debug)]
pub struct WorkspaceManifest {
#[serde(default)]
pub workspace: Workspace,
#[serde(default)]
pub patch: Map<String, RegistryPatch>,
#[serde(default)]
pub replace: Map<String, Patch>,
}

#[derive(Deserialize, Default, Debug)]
pub struct Workspace {
#[serde(default)]
pub package: WorkspacePackage,
#[serde(default)]
pub dependencies: Map<String, Dependency>,
}

#[derive(Deserialize, Default, Debug)]
pub struct WorkspacePackage {
pub edition: Option<String>,
}

#[derive(Deserialize, Default, Debug)]
pub struct Manifest {
#[serde(default, rename = "cargo-features")]
Expand Down Expand Up @@ -107,6 +123,8 @@ pub struct Dependency {
pub default_features: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub features: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub workspace: bool,
#[serde(flatten)]
pub rest: Map<String, Value>,
}
Expand Down Expand Up @@ -136,6 +154,11 @@ fn is_true(boolean: &bool) -> bool {
*boolean
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(boolean: &bool) -> bool {
!*boolean
}

impl Serialize for Dependency {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -171,6 +194,7 @@ impl<'de> Deserialize<'de> for Dependency {
path: None,
default_features: true,
features: Vec::new(),
workspace: false,
rest: Map::new(),
})
}
Expand Down
8 changes: 7 additions & 1 deletion src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ fn make_manifest(
features,
dependencies: std::collections::BTreeMap::new(),
bins: Vec::new(),
workspace: Some(Workspace {}),
workspace: Some(Workspace {
package: crate::manifest::WorkspacePackage {
edition: workspace_manifest.workspace.package.edition,
},
dependencies: workspace_manifest.workspace.dependencies,
}),
// Within a workspace, only the [patch] and [replace] sections in
// the workspace root's Cargo.toml are applied by Cargo.
patch: workspace_manifest.patch,
Expand All @@ -286,6 +291,7 @@ fn make_manifest(
path: Some(project.source_dir.clone()),
default_features: false,
features: Vec::new(),
workspace: false,
rest: std::collections::BTreeMap::new(),
},
);
Expand Down
24 changes: 21 additions & 3 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::dependencies::{Dependency, Patch, RegistryPatch};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap as Map;
use std::ffi::OsStr;
use std::path::PathBuf;
Expand Down Expand Up @@ -27,13 +28,13 @@ pub struct Manifest {
pub struct Package {
pub name: String,
pub version: String,
pub edition: Edition,
pub publish: bool,
pub edition: Edition,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reordering is needed to avoid ValueAfterTable error from basic-toml crate.

}

// Do not use enum for edition for future-compatibility.
#[derive(Serialize, Deserialize, Debug)]
pub struct Edition(pub String);
pub struct Edition(pub Value);

#[derive(Serialize, Debug)]
pub struct Bin {
Expand All @@ -55,7 +56,24 @@ pub struct Build {
}

#[derive(Serialize, Debug)]
pub struct Workspace {}
pub struct Workspace {
#[serde(skip_serializing_if = "WorkspacePackage::is_none")]
pub package: WorkspacePackage,
#[serde(skip_serializing_if = "Map::is_empty")]
pub dependencies: Map<String, Dependency>,
}

#[derive(Serialize, Debug)]
pub struct WorkspacePackage {
#[serde(skip_serializing_if = "Option::is_none")]
pub edition: Option<String>,
}

impl WorkspacePackage {
fn is_none(&self) -> bool {
self.edition.is_none()
}
}

impl Default for Edition {
fn default() -> Self {
Expand Down
Loading