Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat: add dependencies to MultiBindingsInner #2606

Merged
merged 3 commits into from
Oct 10, 2023
Merged
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
35 changes: 31 additions & 4 deletions ethers-contract/ethers-contract-abigen/src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ impl MultiAbigen {
/// Build the contract bindings and prepare for writing
pub fn build(self) -> Result<MultiBindings> {
let format = self.abigens.iter().any(|gen| gen.format);
Ok(MultiBindings { expansion: MultiExpansion::from_abigen(self.abigens)?.expand(), format })
Ok(MultiBindings {
expansion: MultiExpansion::from_abigen(self.abigens)?.expand(),
format,
dependencies: vec![],
})
}
}

Expand Down Expand Up @@ -299,7 +303,12 @@ impl MultiExpansionResult {
}

/// Converts this result into [`MultiBindingsInner`]
fn into_bindings(mut self, single_file: bool, format: bool) -> MultiBindingsInner {
fn into_bindings(
mut self,
single_file: bool,
format: bool,
dependencies: Vec<String>,
) -> MultiBindingsInner {
self.set_shared_import_path(single_file);
let Self { contracts, shared_types, root, .. } = self;
let bindings = contracts
Expand Down Expand Up @@ -333,7 +342,7 @@ impl MultiExpansionResult {
None
};

MultiBindingsInner { root, bindings, shared_types }
MultiBindingsInner { root, bindings, shared_types, dependencies }
}
}

Expand Down Expand Up @@ -366,6 +375,7 @@ impl MultiExpansionResult {
pub struct MultiBindings {
expansion: MultiExpansionResult,
format: bool,
dependencies: Vec<String>,
}

impl MultiBindings {
Expand Down Expand Up @@ -396,8 +406,19 @@ impl MultiBindings {
self
}

/// Specify a set of dependencies to use for the generated crate.
///
/// By default, this is empty and only the `ethers` dependency is added.
pub fn dependencies(
mut self,
dependencies: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.dependencies = dependencies.into_iter().map(|dep| dep.into()).collect();
self
}

fn into_inner(self, single_file: bool) -> MultiBindingsInner {
self.expansion.into_bindings(single_file, self.format)
self.expansion.into_bindings(single_file, self.format, self.dependencies)
}

/// Generates all the bindings and writes them to the given module
Expand Down Expand Up @@ -581,6 +602,9 @@ struct MultiBindingsInner {
bindings: BTreeMap<String, ContractBindings>,
/// contains the content of the shared types if any
shared_types: Option<ContractBindings>,
/// Dependencies other than `ethers-rs` to add to the `Cargo.toml` for bindings generated as a
/// crate.
dependencies: Vec<String>,
}

// deref allows for inspection without modification
Expand Down Expand Up @@ -611,6 +635,9 @@ impl MultiBindingsInner {
writeln!(toml)?;
writeln!(toml, "[dependencies]")?;
writeln!(toml, r#"{crate_version}"#)?;
for dependency in self.dependencies.clone() {
writeln!(toml, "{}", dependency)?;
}
Ok(toml)
}

Expand Down