From edaa1202abd1f684d6aad1a75b60dc9fae61b02f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 12 May 2022 11:36:24 +0200 Subject: [PATCH] feat(solc): add source map access functions --- ethers-solc/src/artifact_output/mod.rs | 17 +++++++++++++++++ ethers-solc/src/artifacts/bytecode.rs | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/ethers-solc/src/artifact_output/mod.rs b/ethers-solc/src/artifact_output/mod.rs index f08756432..fa736bc34 100644 --- a/ethers-solc/src/artifact_output/mod.rs +++ b/ethers-solc/src/artifact_output/mod.rs @@ -22,6 +22,7 @@ use crate::{ SourceFile, }, compile::output::{contracts::VersionedContracts, sources::VersionedSourceFiles}, + sourcemap::{SourceMap, SyntaxError}, }; pub use configurable::*; @@ -398,6 +399,22 @@ pub trait Artifact { fn get_abi(&self) -> Option> { self.get_contract_bytecode().abi } + + /// Returns the `sourceMap` of the contract + /// + /// Returns `None` if no `sourceMap` string was included in the compiler output + /// Returns `Some(Err)` if parsing the sourcemap failed + fn get_source_map(&self) -> Option> { + self.get_bytecode()?.source_map() + } + + /// Returns the `sourceMap` as str if it was included in the compiler output + fn get_source_map_str(&self) -> Option> { + match self.get_bytecode()? { + Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed), + Cow::Owned(code) => code.source_map.map(Cow::Owned), + } + } } impl Artifact for T diff --git a/ethers-solc/src/artifacts/bytecode.rs b/ethers-solc/src/artifacts/bytecode.rs index 6025669c1..8f6837ce5 100644 --- a/ethers-solc/src/artifacts/bytecode.rs +++ b/ethers-solc/src/artifacts/bytecode.rs @@ -46,6 +46,13 @@ pub struct CompactBytecode { } impl CompactBytecode { + /// Returns the parsed source map + /// + /// See also + pub fn source_map(&self) -> Option> { + self.source_map.as_ref().map(|map| sourcemap::parse(map)) + } + /// Tries to link the bytecode object with the `file` and `library` name. /// Replaces all library placeholders with the given address. ///