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

Chop manifest module into multiple submodules #354

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 34 additions & 0 deletions scarb/src/core/manifest/compiler_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use serde::{Deserialize, Serialize};

use crate::compiler::{DefaultForProfile, Profile};
use crate::core::TomlCairo;

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct ManifestCompilerConfig {
/// Replace all names in generated Sierra code with dummy counterparts, representing the
/// expanded information about the named items.
///
/// For libfuncs and types that would be recursively opening their generic arguments.
/// For functions, that would be their original name in Cairo.
/// For example, while the Sierra name be `[6]`, with this flag turned on it might be:
/// - For libfuncs: `felt252_const<2>` or `unbox<Box<Box<felt252>>>`.
/// - For types: `felt252` or `Box<Box<felt252>>`.
/// - For user functions: `test::foo`.
pub sierra_replace_ids: bool,
}

impl DefaultForProfile for ManifestCompilerConfig {
fn default_for_profile(profile: &Profile) -> Self {
Self {
sierra_replace_ids: profile.is_dev(),
}
}
}

impl From<ManifestCompilerConfig> for TomlCairo {
fn from(config: ManifestCompilerConfig) -> Self {
Self {
sierra_replace_ids: Some(config.sierra_replace_ids),
}
}
}
40 changes: 40 additions & 0 deletions scarb/src/core/manifest/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fmt;

use semver::VersionReq;

use crate::core::{PackageId, PackageName, SourceId, Summary};

#[derive(Clone, Eq, PartialEq, Hash)]
pub struct ManifestDependency {
pub name: PackageName,
pub version_req: VersionReq,
pub source_id: SourceId,
}

impl ManifestDependency {
pub fn matches_summary(&self, summary: &Summary) -> bool {
self.matches_package_id(summary.package_id)
}

pub fn matches_package_id(&self, package_id: PackageId) -> bool {
package_id.name == self.name && self.version_req.matches(&package_id.version)
}
}

impl fmt::Display for ManifestDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.name, self.version_req)?;

if !self.source_id.is_default_registry() {
write!(f, " ({})", self.source_id)?;
}

Ok(())
}
}

impl fmt::Debug for ManifestDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ManifestDependency({self})")
}
}
176 changes: 7 additions & 169 deletions scarb/src/core/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::collections::BTreeMap;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;

use once_cell::sync::Lazy;
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
use toml::Value;

pub use compiler_config::*;
pub use dependency::*;
pub use scripts::*;
pub use summary::*;
pub use target::*;
pub use toml_manifest::*;

use crate::compiler::{DefaultForProfile, Profile};
use crate::core::package::{PackageId, PackageName};
use crate::core::source::SourceId;
use crate::compiler::Profile;

mod compiler_config;
mod dependency;
mod scripts;
mod summary;
mod target;
mod toml_manifest;

Expand All @@ -35,103 +35,6 @@ pub struct Manifest {
pub profiles: Vec<Profile>,
}

/// Subset of a [`Manifest`] that contains only the most important information about a package.
/// See [`SummaryInner`] for public fields reference.
#[derive(Clone, Debug)]
pub struct Summary(Arc<SummaryInner>);

#[derive(Debug)]
#[non_exhaustive]
pub struct SummaryInner {
pub package_id: PackageId,
pub dependencies: Vec<ManifestDependency>,
pub no_core: bool,
}

impl Deref for Summary {
type Target = SummaryInner;

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

impl Summary {
pub fn build(package_id: PackageId) -> SummaryBuilder {
SummaryBuilder::new(package_id)
}

pub fn minimal(package_id: PackageId, dependencies: Vec<ManifestDependency>) -> Self {
Self::build(package_id)
.with_dependencies(dependencies)
.finish()
}

fn new(data: SummaryInner) -> Self {
Self(Arc::new(data))
}

pub fn full_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
self.dependencies.iter().chain(self.implicit_dependencies())
}

pub fn implicit_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
static CORE_DEPENDENCY: Lazy<ManifestDependency> = Lazy::new(|| {
// NOTE: Pin `core` to exact version, because we know that's the only one we have.
let cairo_version = crate::version::get().cairo.version;
let version_req = VersionReq::parse(&format!("={cairo_version}")).unwrap();
ManifestDependency {
name: PackageName::CORE,
version_req,
source_id: SourceId::default(),
}
});

let mut deps: Vec<&ManifestDependency> = Vec::new();

if !self.no_core {
deps.push(&CORE_DEPENDENCY);
}

deps.into_iter()
}
}

#[derive(Debug)]
pub struct SummaryBuilder {
package_id: PackageId,
dependencies: Vec<ManifestDependency>,
no_core: bool,
}

impl SummaryBuilder {
fn new(package_id: PackageId) -> Self {
Self {
package_id,
dependencies: Vec::new(),
no_core: false,
}
}

pub fn with_dependencies(mut self, dependencies: Vec<ManifestDependency>) -> Self {
self.dependencies = dependencies;
self
}

pub fn no_core(mut self, no_core: bool) -> Self {
self.no_core = no_core;
self
}

pub fn finish(self) -> Summary {
Summary::new(SummaryInner {
package_id: self.package_id,
dependencies: self.dependencies,
no_core: self.no_core,
})
}
}

/// Subset of a [`Manifest`] that contains package metadata.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct ManifestMetadata {
Expand All @@ -149,68 +52,3 @@ pub struct ManifestMetadata {
pub tool_metadata: Option<BTreeMap<SmolStr, Value>>,
pub cairo_version: Option<VersionReq>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct ManifestCompilerConfig {
/// Replace all names in generated Sierra code with dummy counterparts, representing the
/// expanded information about the named items.
///
/// For libfuncs and types that would be recursively opening their generic arguments.
/// For functions, that would be their original name in Cairo.
/// For example, while the Sierra name be `[6]`, with this flag turned on it might be:
/// - For libfuncs: `felt252_const<2>` or `unbox<Box<Box<felt252>>>`.
/// - For types: `felt252` or `Box<Box<felt252>>`.
/// - For user functions: `test::foo`.
pub sierra_replace_ids: bool,
}

impl DefaultForProfile for ManifestCompilerConfig {
fn default_for_profile(profile: &Profile) -> Self {
Self {
sierra_replace_ids: profile.is_dev(),
}
}
}

impl From<ManifestCompilerConfig> for TomlCairo {
fn from(config: ManifestCompilerConfig) -> Self {
Self {
sierra_replace_ids: Some(config.sierra_replace_ids),
}
}
}

#[derive(Clone, Eq, PartialEq, Hash)]
pub struct ManifestDependency {
pub name: PackageName,
pub version_req: VersionReq,
pub source_id: SourceId,
}

impl ManifestDependency {
pub fn matches_summary(&self, summary: &Summary) -> bool {
self.matches_package_id(summary.package_id)
}

pub fn matches_package_id(&self, package_id: PackageId) -> bool {
package_id.name == self.name && self.version_req.matches(&package_id.version)
}
}

impl fmt::Display for ManifestDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.name, self.version_req)?;

if !self.source_id.is_default_registry() {
write!(f, " ({})", self.source_id)?;
}

Ok(())
}
}

impl fmt::Debug for ManifestDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ManifestDependency({self})")
}
}
106 changes: 106 additions & 0 deletions scarb/src/core/manifest/summary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::ops::Deref;
use std::sync::Arc;

use once_cell::sync::Lazy;
use semver::VersionReq;

#[cfg(doc)]
use crate::core::Manifest;
use crate::core::{ManifestDependency, PackageId, PackageName, SourceId};

/// Subset of a [`Manifest`] that contains only the most important information about a package.
/// See [`SummaryInner`] for public fields reference.
#[derive(Clone, Debug)]
pub struct Summary(Arc<SummaryInner>);

#[derive(Debug)]
#[non_exhaustive]
pub struct SummaryInner {
pub package_id: PackageId,
pub dependencies: Vec<ManifestDependency>,
pub no_core: bool,
}

impl Deref for Summary {
type Target = SummaryInner;

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

impl Summary {
pub fn build(package_id: PackageId) -> SummaryBuilder {
SummaryBuilder::new(package_id)
}

pub fn minimal(package_id: PackageId, dependencies: Vec<ManifestDependency>) -> Self {
Self::build(package_id)
.with_dependencies(dependencies)
.finish()
}

fn new(data: SummaryInner) -> Self {
Self(Arc::new(data))
}

pub fn full_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
self.dependencies.iter().chain(self.implicit_dependencies())
}

pub fn implicit_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
static CORE_DEPENDENCY: Lazy<ManifestDependency> = Lazy::new(|| {
// NOTE: Pin `core` to exact version, because we know that's the only one we have.
let cairo_version = crate::version::get().cairo.version;
let version_req = VersionReq::parse(&format!("={cairo_version}")).unwrap();
ManifestDependency {
name: PackageName::CORE,
version_req,
source_id: SourceId::default(),
}
});

let mut deps: Vec<&ManifestDependency> = Vec::new();

if !self.no_core {
deps.push(&CORE_DEPENDENCY);
}

deps.into_iter()
}
}

#[derive(Debug)]
pub struct SummaryBuilder {
package_id: PackageId,
dependencies: Vec<ManifestDependency>,
no_core: bool,
}

impl SummaryBuilder {
fn new(package_id: PackageId) -> Self {
Self {
package_id,
dependencies: Vec::new(),
no_core: false,
}
}

pub fn with_dependencies(mut self, dependencies: Vec<ManifestDependency>) -> Self {
self.dependencies = dependencies;
self
}

pub fn no_core(mut self, no_core: bool) -> Self {
self.no_core = no_core;
self
}

pub fn finish(self) -> Summary {
Summary::new(SummaryInner {
package_id: self.package_id,
dependencies: self.dependencies,
no_core: self.no_core,
})
}
}