use super::{PackageInfo, Reader, SyntaxLoad, Workspace};
use crate::configuration::{self, ActiveConfiguration, DeclaredConfiguration};
use crate::meter::Meter;
use crate::{DependencyDeclaration, LoadError, LoadOptions, RelPath, Tree};
impl Workspace {
pub fn load_configured_target(
tree: &dyn Tree,
manifest: &RelPath,
configuration: DeclaredConfiguration<'_>,
options: &LoadOptions,
) -> Result<Self, LoadError> {
Self::load_selected(
tree,
manifest,
options,
SyntaxLoad::Configured(configuration),
)
}
#[cfg(target_os = "linux")]
pub fn par_load_configured_target(
tree: &dyn crate::ParallelTree,
manifest: &RelPath,
configuration: DeclaredConfiguration<'_>,
options: &LoadOptions,
parallel: &crate::ParallelOptions,
) -> Result<Self, LoadError> {
Self::load_selected(
tree,
manifest,
options,
SyntaxLoad::ConfiguredParallel(configuration, parallel),
)
}
fn load_selected(
tree: &dyn Tree,
manifest: &RelPath,
options: &LoadOptions,
syntax: SyntaxLoad<'_>,
) -> Result<Self, LoadError> {
let meter = Meter::new(options);
meter.items(crate::index::LOAD_BASE_BYTES, manifest, None)?;
let discovered = crate::discovery::single(tree, manifest, &meter)?;
Self::build_discovered_with_syntax(tree, options, &meter, None, &discovered, syntax)
}
}
pub(super) fn configure(
reader: &Reader<'_>,
declarations: &[Vec<DependencyDeclaration>],
infos: &mut [PackageInfo],
targets: &mut Vec<(&RelPath, crate::target::Derived)>,
input: DeclaredConfiguration<'_>,
) -> Result<ActiveConfiguration, LoadError> {
let [found] = reader.found else {
return Err(configuration::failure(
&RelPath::root(),
"select exactly one package manifest",
));
};
let manifest = &found.package.manifest;
let active = configuration::admit(
input,
&found.manifest.features,
&declarations[0],
reader.meter,
manifest,
)?;
let mut selected = None;
for (index, (_, derived)) in targets.iter().enumerate() {
reader.meter.work(
derived
.target
.name
.len()
.saturating_add(active.target_name().len())
.saturating_add(1),
manifest,
None,
)?;
if derived.target.kind == active.target_kind()
&& derived.target.name == active.target_name()
{
if selected.replace(index).is_some() {
return Err(configuration::failure(
manifest,
"declared kind/name selects multiple package targets",
));
}
for feature in &derived.target.required_features {
if !configuration::contains(active.features(), feature, reader.meter, manifest)? {
return Err(configuration::failure(
manifest,
"selected target requires an inactive feature",
));
}
}
}
}
let selected = selected.ok_or_else(|| {
configuration::failure(manifest, "declared kind/name selects no package target")
})?;
let target = targets.remove(selected);
targets.clear();
targets.push(target);
infos[0].configured = true;
for dependency in &mut infos[0].dependencies {
dependency.package = None;
}
Ok(active)
}