refactor(core): Expose all of the variants of VirtualManifests

This commit is contained in:
Ed Page 2024-03-15 21:09:40 -05:00
parent a694ea9777
commit 8152bfbb5e
6 changed files with 49 additions and 4 deletions

2
Cargo.lock generated
View File

@ -470,7 +470,7 @@ dependencies = [
[[package]] [[package]]
name = "cargo-util-schemas" name = "cargo-util-schemas"
version = "0.3.0" version = "0.3.1"
dependencies = [ dependencies = [
"semver", "semver",
"serde", "serde",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cargo-util-schemas" name = "cargo-util-schemas"
version = "0.3.0" version = "0.3.1"
rust-version = "1.76.0" # MSRV:1 rust-version = "1.76.0" # MSRV:1
edition.workspace = true edition.workspace = true
license.workspace = true license.workspace = true

View File

@ -26,7 +26,7 @@ pub use rust_version::RustVersion;
pub use rust_version::RustVersionError; pub use rust_version::RustVersionError;
/// This type is used to deserialize `Cargo.toml` files. /// This type is used to deserialize `Cargo.toml` files.
#[derive(Debug, Deserialize, Serialize)] #[derive(Default, Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct TomlManifest { pub struct TomlManifest {
// when adding new fields, be sure to check whether `requires_package` should disallow them // when adding new fields, be sure to check whether `requires_package` should disallow them

View File

@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
use crate::GlobalContext; use crate::GlobalContext;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc;
use super::BuildConfig; use super::BuildConfig;
@ -103,6 +104,10 @@ pub fn resolve_std<'gctx>(
/*custom_metadata*/ &None, /*custom_metadata*/ &None,
)); ));
let virtual_manifest = crate::core::VirtualManifest::new( let virtual_manifest = crate::core::VirtualManifest::new(
Rc::default(),
Rc::new(toml_edit::ImDocument::parse("".to_owned()).expect("empty is valid TOML")),
Rc::default(),
Rc::default(),
/*replace*/ Vec::new(), /*replace*/ Vec::new(),
patch, patch,
ws_config, ws_config,

View File

@ -88,6 +88,13 @@ pub struct Warnings(Vec<DelayedWarning>);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct VirtualManifest { pub struct VirtualManifest {
// alternate forms of manifests:
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
// this form of manifest:
replace: Vec<(PackageIdSpec, Dependency)>, replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>, patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig, workspace: WorkspaceConfig,
@ -629,6 +636,10 @@ impl Manifest {
impl VirtualManifest { impl VirtualManifest {
pub fn new( pub fn new(
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
replace: Vec<(PackageIdSpec, Dependency)>, replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>, patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig, workspace: WorkspaceConfig,
@ -637,6 +648,10 @@ impl VirtualManifest {
resolve_behavior: Option<ResolveBehavior>, resolve_behavior: Option<ResolveBehavior>,
) -> VirtualManifest { ) -> VirtualManifest {
VirtualManifest { VirtualManifest {
contents,
document,
original_toml,
resolved_toml,
replace, replace,
patch, patch,
workspace, workspace,
@ -647,6 +662,23 @@ impl VirtualManifest {
} }
} }
/// The raw contents of the original TOML
pub fn contents(&self) -> &str {
self.contents.as_str()
}
/// Collection of spans for the original TOML
pub fn document(&self) -> &toml_edit::ImDocument<String> {
&self.document
}
/// The [`TomlManifest`] with all fields expanded
pub fn original_toml(&self) -> &TomlManifest {
&self.original_toml
}
/// The [`TomlManifest`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
}
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] { pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace &self.replace
} }

View File

@ -60,7 +60,8 @@ pub fn read_manifest(
to_real_manifest(contents, document, original_toml, source_id, path, gctx) to_real_manifest(contents, document, original_toml, source_id, path, gctx)
.map(EitherManifest::Real) .map(EitherManifest::Real)
} else { } else {
to_virtual_manifest(original_toml, source_id, path, gctx).map(EitherManifest::Virtual) to_virtual_manifest(contents, document, original_toml, source_id, path, gctx)
.map(EitherManifest::Virtual)
} }
})() })()
.map_err(|err| { .map_err(|err| {
@ -1276,6 +1277,8 @@ fn load_inheritable_fields(
} }
fn to_virtual_manifest( fn to_virtual_manifest(
contents: String,
document: toml_edit::ImDocument<String>,
original_toml: manifest::TomlManifest, original_toml: manifest::TomlManifest,
source_id: SourceId, source_id: SourceId,
manifest_file: &Path, manifest_file: &Path,
@ -1362,7 +1365,12 @@ fn to_virtual_manifest(
bail!("virtual manifests must be configured with [workspace]"); bail!("virtual manifests must be configured with [workspace]");
} }
}; };
let resolved_toml = original_toml.clone();
let mut manifest = VirtualManifest::new( let mut manifest = VirtualManifest::new(
Rc::new(contents),
Rc::new(document),
Rc::new(original_toml),
Rc::new(resolved_toml),
replace, replace,
patch, patch,
workspace_config, workspace_config,