Skip to content

Commit

Permalink
Distinguish between library and plugin IDs in component dependencies
Browse files Browse the repository at this point in the history
commit-id:8452ee94
  • Loading branch information
integraledelebesgue committed Jan 22, 2025
1 parent e27151d commit db30b3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 17 additions & 10 deletions scarb/src/compiler/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,34 @@ pub struct CompilationUnitCairoPlugin {
/// Currently, a compilation unit can be uniquely identified by [`PackageId`] only.
/// It may be not sufficient in the future depending on changes to the compilation model.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct CompilationUnitComponentId {
pub package_id: PackageId,
pub enum CompilationUnitComponentId {
Library(PackageId),
Plugin(PackageId),
}

impl CompilationUnitComponentId {
/// Extracts the underlying `PackageId` regardless of the variant.
pub fn package_id(&self) -> PackageId {
match self {
CompilationUnitComponentId::Library(package_id) => *package_id,
CompilationUnitComponentId::Plugin(package_id) => *package_id,
}
}

pub fn to_metadata_component_id(&self) -> scarb_metadata::CompilationUnitComponentId {
self.package_id.to_serialized_string().into()
self.package_id().to_serialized_string().into()
}

pub fn to_discriminator(&self) -> Option<SmolStr> {
if self.package_id.name == PackageName::CORE {
if self.package_id().name == PackageName::CORE {
None
} else {
Some(self.to_crate_identifier().into())
}
}

pub fn to_crate_identifier(&self) -> CrateIdentifier {
self.package_id.to_serialized_string().into()
self.package_id().to_serialized_string().into()
}
}

Expand Down Expand Up @@ -289,8 +298,8 @@ impl CairoCompilationUnit {
}

impl CompilationUnitComponent {
/// Validate input and create new [CompilationUnitComponent] instance.
pub fn try_new(
/// Validate input and create new [CompilationUnitComponent] instance representing a library.
pub fn try_new_library(
package: Package,
targets: Vec<Target>,
cfg_set: Option<CfgSet>,
Expand Down Expand Up @@ -329,9 +338,7 @@ impl CompilationUnitComponent {
);
}
Ok(Self {
id: CompilationUnitComponentId {
package_id: package.id,
},
id: CompilationUnitComponentId::Library(package.id),
package,
targets,
cfg_set,
Expand Down
8 changes: 4 additions & 4 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ fn cairo_compilation_unit_for_target(
}
};

CompilationUnitComponent::try_new(package, targets, cfg_set)
CompilationUnitComponent::try_new_library(package, targets, cfg_set)
})
.collect::<Result<_>>()?;

Expand All @@ -495,7 +495,7 @@ fn cairo_compilation_unit_for_target(
});

// Add `lib` target for tested package, to be available as dependency.
components.push(CompilationUnitComponent::try_new(
components.push(CompilationUnitComponent::try_new_library(
member.clone(),
vec![target],
get_cfg_with_features(
Expand Down Expand Up @@ -722,7 +722,7 @@ impl<'a> PackageSolutionCollector<'a> {
component: &CompilationUnitComponent,
components: &[CompilationUnitComponent],
) -> Vec<CompilationUnitComponentId> {
let package_id = component.id.package_id;
let package_id = component.id.package_id();

// Those are direct dependencies of the component.
let dependencies_packages = self
Expand Down Expand Up @@ -830,7 +830,7 @@ pub fn generate_cairo_plugin_compilation_units(
.map(Arc::new),
)
.flatten();
let components = vec![CompilationUnitComponent::try_new(
let components = vec![CompilationUnitComponent::try_new_library(
member.clone(),
vec![member
.fetch_target(&TargetKind::CAIRO_PLUGIN)
Expand Down

0 comments on commit db30b3f

Please sign in to comment.