use super::super::Reader;
use super::{BindingLoad, LockedBinding, Purpose};
use super::{LockedManifest, Sources, failure};
use crate::{
CommittedGitTree, DiskTree, LoadError, LoadOptions, LockedCatalog, LockedPackageId,
LockedSource, PackageId, RegistryArchiveTree, RelPath, SourceAuthorityId, Tree, Workspace,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReferenceBinding<'a> {
binding: &'a LockedBinding,
}
impl ReferenceBinding<'_> {
#[must_use]
pub const fn row(&self) -> LockedPackageId {
self.binding.row()
}
#[must_use]
pub fn name(&self) -> &str {
self.binding.name()
}
#[must_use]
pub fn version(&self) -> &str {
self.binding.version()
}
#[must_use]
pub fn source(&self) -> Option<&LockedSource> {
self.binding.source()
}
}
impl BindingLoad<'_> {
pub(super) fn check_observed_path(
&self,
reader: &Reader<'_>,
declaration: &crate::DependencyDeclaration,
manifest: &RelPath,
destination: &RelPath,
) -> Result<(), LoadError> {
let Purpose::Structural(contexts) = self.purpose else {
return Err(failure(
manifest,
"declared path does not name the selected row's actual admitted manifest",
));
};
let (base, specification) = declaration
.inherited
.as_ref()
.map_or((manifest, &declaration.specification), |inherited| {
(&inherited.manifest, &inherited.specification)
});
let relative = specification.path.as_deref().ok_or_else(|| {
failure(
manifest,
"path relationship has no written path declaration",
)
})?;
let owner = contexts
.get(base.authority().index())
.ok_or_else(|| failure(manifest, "unknown owning source authority"))?;
let target = contexts
.get(destination.authority().index())
.ok_or_else(|| failure(destination, "unknown destination source authority"))?;
let bytes = base
.as_str()
.len()
.saturating_add(relative.len())
.saturating_add(destination.as_str().len());
reader.meter.work(bytes.saturating_add(1), manifest, None)?;
let matches = match (owner, target) {
(ReferenceTree::Disk(owner), ReferenceTree::Disk(target)) => {
reader.meter.items(
bytes
.saturating_add(owner.root_path_bytes())
.saturating_add(target.root_path_bytes())
.saturating_mul(4),
manifest,
None,
)?;
owner
.dependency_directory_matches(
&base.in_source(SourceAuthorityId::FIRST_PARTY),
relative,
target,
&destination.in_source(SourceAuthorityId::FIRST_PARTY),
)
.map_err(|error| {
LoadError::tree(manifest, None, &crate::error::Origin::Discovery, error)
})?
}
(ReferenceTree::Git(owner), ReferenceTree::Git(target))
if owner.source() == target.source() =>
{
let directory = base
.parent()
.ok_or_else(|| failure(base, "manifest has no owning directory"))?;
let expected = directory
.join(relative)
.and_then(|path| path.join("Cargo.toml"))
.map_err(|error| failure(manifest, &error.to_string()))?;
let expected = crate::modtree::tidy(reader.tree, &expected).map_err(|error| {
LoadError::tree(manifest, None, &crate::error::Origin::Discovery, error)
})?;
expected.in_source(destination.authority()) == *destination
}
_ => {
return Err(failure(
manifest,
"cross-authority path relationships require actual disk roots or one exact committed Git source",
));
}
};
if !matches {
return Err(failure(
manifest,
"declared path names a different package directory from the supplied destination",
));
}
Ok(())
}
}
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum ReferenceTree<'a> {
Opaque(&'a dyn Tree),
Disk(&'a DiskTree),
Git(&'a CommittedGitTree<'a>),
Registry(&'a RegistryArchiveTree<'a>),
}
impl<'a> ReferenceTree<'a> {
pub(super) fn tree(self) -> &'a dyn Tree {
match self {
Self::Opaque(tree) => tree,
Self::Disk(tree) => tree,
Self::Git(tree) => tree,
Self::Registry(tree) => tree,
}
}
pub(super) fn source(self) -> Option<&'a LockedSource> {
match self {
Self::Git(tree) => Some(tree.source()),
Self::Registry(tree) => Some(tree.source()),
Self::Opaque(_) | Self::Disk(_) => None,
}
}
}
#[derive(Clone, Copy)]
pub struct ReferencePackageRoots<'a> {
pub(super) tree: ReferenceTree<'a>,
pub(super) packages: &'a [LockedManifest<'a>],
}
impl<'a> ReferencePackageRoots<'a> {
#[must_use]
pub const fn new(tree: ReferenceTree<'a>, packages: &'a [LockedManifest<'a>]) -> Self {
Self { tree, packages }
}
}
impl Workspace {
#[must_use]
pub fn reference_binding(&self, package: PackageId) -> Option<ReferenceBinding<'_>> {
self.references
.get(package.index())
.map(|binding| ReferenceBinding { binding })
}
pub fn load_reference_sources_under(
catalog: &LockedCatalog,
first_party: ReferenceTree<'_>,
first_party_roots: &[RelPath],
local: &[LockedManifest<'_>],
sources: &[ReferencePackageRoots<'_>],
options: &LoadOptions,
) -> Result<Self, LoadError> {
super::check_first_party_roots(first_party_roots)?;
Self::load_locked_with(
catalog,
first_party,
Some(first_party_roots),
local,
Sources::Observed(sources),
options,
super::SyntaxLoad::Ordinary(None),
)
}
}