Skip to content

Commit

Permalink
Use typed-builder for Summary build process
Browse files Browse the repository at this point in the history
commit-id:0dbb3623
  • Loading branch information
mkaput committed May 18, 2023
1 parent 320e252 commit 80bdd53
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 59 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ clap = { version = "4.2.7", features = ["derive", "env", "string"] }
clap-verbosity-flag = "2.0.1"
console = "0.15.5"
data-encoding = "2.3.3"
derive_builder = "0.12.0"
deno_task_shell = "0.12.0"
derive_builder = "0.12.0"
diffy = "0.3.0"
directories = "5.0.1"
dunce = "1.0.4"
Expand Down Expand Up @@ -71,6 +71,7 @@ tracing = "0.1.37"
tracing-futures = "0.2.5"
tracing-log = "0.1.3"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
typed-builder = "0.14.0"
url = { version = "2.3.1", features = ["serde"] }
walkdir = "2.3.2"
which = "4.4.0"
Expand Down
3 changes: 2 additions & 1 deletion scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ scarb-metadata = { version = "1.3.0", path = "../scarb-metadata", default-featur
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio.workspace = true
smol_str.workspace = true
thiserror.workspace = true
tokio.workspace = true
toml.workspace = true
toml_edit.workspace = true
tracing-futures.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
typed-builder.workspace = true
url.workspace = true
walkdir.workspace = true
which.workspace = true
Expand Down
63 changes: 16 additions & 47 deletions scarb/src/core/manifest/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ use std::sync::Arc;

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

#[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.
/// Construct using [`Summary::builder`].
#[derive(Clone, Debug)]
pub struct Summary(Arc<SummaryInner>);

#[derive(Debug)]
#[derive(TypedBuilder, Debug)]
#[builder(builder_type(name = SummaryBuilder))]
#[builder(builder_method(vis = ""))]
#[builder(build_method(into = Summary))]
#[non_exhaustive]
pub struct SummaryInner {
pub package_id: PackageId,
#[builder(default)]
pub dependencies: Vec<ManifestDependency>,
#[builder(default = false)]
pub no_core: bool,
}

Expand All @@ -29,19 +36,16 @@ impl Deref for Summary {
}
}

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()
#[doc(hidden)]
impl From<SummaryInner> for Summary {
fn from(data: SummaryInner) -> Self {
Self(Arc::new(data))
}
}

fn new(data: SummaryInner) -> Self {
Self(Arc::new(data))
impl Summary {
pub fn builder() -> SummaryBuilder {
SummaryInner::builder()
}

pub fn full_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
Expand Down Expand Up @@ -69,38 +73,3 @@ impl Summary {
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,
})
}
}
7 changes: 4 additions & 3 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ impl TomlManifest {
let profiles = self.collect_profiles()?;

Ok(Manifest {
summary: Summary::build(package_id)
.with_dependencies(dependencies)
summary: Summary::builder()
.package_id(package_id)
.dependencies(dependencies)
.no_core(no_core)
.finish(),
.build(),
targets,
metadata: ManifestMetadata {
authors: package.authors.clone(),
Expand Down
12 changes: 5 additions & 7 deletions scarb/src/core/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ pub(crate) mod mock {
}

fn build_package(package_id: PackageId, dependencies: Vec<ManifestDependency>) -> Package {
let mut sb = Summary::build(package_id).with_dependencies(dependencies);

if package_id.is_core() {
sb = sb.no_core(true);
}

let summary = sb.finish();
let summary = Summary::builder()
.package_id(package_id)
.dependencies(dependencies)
.no_core(package_id.is_core())
.build();

let manifest = Box::new(Manifest {
summary,
Expand Down

0 comments on commit 80bdd53

Please sign in to comment.