From 1584628ce4c7e8744b8486514c2d8a8a91156cd4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 5 Dec 2023 00:33:10 +0100 Subject: [PATCH] feat: add more getter methods to bytecode structs --- src/artifacts/bytecode.rs | 84 ++++++++++++++++++++++++++++----------- src/artifacts/mod.rs | 20 +++++----- 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/src/artifacts/bytecode.rs b/src/artifacts/bytecode.rs index cd19b143..7931ea6c 100644 --- a/src/artifacts/bytecode.rs +++ b/src/artifacts/bytecode.rs @@ -52,6 +52,7 @@ impl CompactBytecode { pub fn empty() -> Self { Self { object: Default::default(), source_map: None, link_references: Default::default() } } + /// Returns the parsed source map /// /// See also @@ -89,6 +90,16 @@ impl CompactBytecode { } false } + + /// Returns the bytes of the bytecode object. + pub fn bytes(&self) -> Option<&Bytes> { + self.object.as_bytes() + } + + /// Returns the underlying `Bytes` if the object is a valid bytecode. + pub fn into_bytes(self) -> Option { + self.object.into_bytes() + } } impl From for CompactBytecode { @@ -203,46 +214,55 @@ impl Bytecode { } false } + + /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode. + pub fn bytes(&self) -> Option<&Bytes> { + self.object.as_bytes() + } + + /// Returns the underlying `Bytes` if the object is a valid bytecode. + pub fn into_bytes(self) -> Option { + self.object.into_bytes() + } } /// Represents the bytecode of a contracts that might be not fully linked yet. #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] #[serde(untagged)] pub enum BytecodeObject { - /// Fully linked bytecode object + /// Fully linked bytecode object. #[serde(deserialize_with = "serde_helpers::deserialize_bytes")] Bytecode(Bytes), - /// Bytecode as hex string that's not fully linked yet and contains library placeholders + /// Bytecode as hex string that's not fully linked yet and contains library placeholders. #[serde(with = "serde_helpers::string_bytes")] Unlinked(String), } impl BytecodeObject { - /// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty - pub fn into_bytes(self) -> Option { + /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode. + pub fn as_bytes(&self) -> Option<&Bytes> { match self { BytecodeObject::Bytecode(bytes) => Some(bytes), BytecodeObject::Unlinked(_) => None, } } - /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode, and not - /// empty - pub fn as_bytes(&self) -> Option<&Bytes> { + /// Returns the underlying `Bytes` if the object is a valid bytecode. + pub fn into_bytes(self) -> Option { match self { BytecodeObject::Bytecode(bytes) => Some(bytes), BytecodeObject::Unlinked(_) => None, } } - /// Returns the number of bytes of the fully linked bytecode + /// Returns the number of bytes of the fully linked bytecode. /// /// Returns `0` if this object is unlinked. pub fn bytes_len(&self) -> usize { self.as_bytes().map(|b| b.as_ref().len()).unwrap_or_default() } - /// Returns a reference to the underlying `String` if the object is unlinked + /// Returns a reference to the underlying `String` if the object is unlinked. pub fn as_str(&self) -> Option<&str> { match self { BytecodeObject::Bytecode(_) => None, @@ -250,7 +270,7 @@ impl BytecodeObject { } } - /// Returns the unlinked `String` if the object is unlinked or empty + /// Returns the unlinked `String` if the object is unlinked. pub fn into_unlinked(self) -> Option { match self { BytecodeObject::Bytecode(_) => None, @@ -258,23 +278,24 @@ impl BytecodeObject { } } - /// Whether this object is still unlinked + /// Whether this object is still unlinked. pub fn is_unlinked(&self) -> bool { matches!(self, BytecodeObject::Unlinked(_)) } - /// Whether this object a valid bytecode + /// Whether this object a valid bytecode. pub fn is_bytecode(&self) -> bool { matches!(self, BytecodeObject::Bytecode(_)) } /// Returns `true` if the object is a valid bytecode and not empty. - /// Returns false the object is a valid but empty bytecode or unlinked. + /// + /// Returns `false` if the object is a valid but empty or unlinked bytecode. pub fn is_non_empty_bytecode(&self) -> bool { self.as_bytes().map(|c| !c.0.is_empty()).unwrap_or_default() } - /// Tries to resolve the unlinked string object a valid bytecode object in place + /// Tries to resolve the unlinked string object a valid bytecode object in place. /// /// Returns the string if it is a valid pub fn resolve(&mut self) -> Option<&Bytes> { @@ -286,7 +307,8 @@ impl BytecodeObject { self.as_bytes() } - /// Link using the fully qualified name of a library + /// Links using the fully qualified name of a library. + /// /// The fully qualified library name is the path of its source file and the library name /// separated by `:` like `file.sol:Math` /// @@ -311,18 +333,19 @@ impl BytecodeObject { self } - /// Link using the `file` and `library` names as fully qualified name `:` - /// See `BytecodeObject::link_fully_qualified` + /// Links using the `file` and `library` names as fully qualified name `:`. + /// + /// See [`link_fully_qualified`](Self::link_fully_qualified). pub fn link( &mut self, file: impl AsRef, library: impl AsRef, addr: Address, ) -> &mut Self { - self.link_fully_qualified(format!("{}:{}", file.as_ref(), library.as_ref(),), addr) + self.link_fully_qualified(format!("{}:{}", file.as_ref(), library.as_ref()), addr) } - /// Links the bytecode object with all provided `(file, lib, addr)` + /// Links the bytecode object with all provided `(file, lib, addr)`. pub fn link_all(&mut self, libs: I) -> &mut Self where I: IntoIterator, @@ -335,7 +358,7 @@ impl BytecodeObject { self } - /// Whether the bytecode contains a matching placeholder using the qualified name + /// Returns whether the bytecode contains a matching placeholder using the qualified name. pub fn contains_fully_qualified_placeholder(&self, name: impl AsRef) -> bool { if let BytecodeObject::Unlinked(unlinked) = self { let name = name.as_ref(); @@ -346,7 +369,7 @@ impl BytecodeObject { } } - /// Whether the bytecode contains a matching placeholder + /// Returns whether the bytecode contains a matching placeholder. pub fn contains_placeholder(&self, file: impl AsRef, library: impl AsRef) -> bool { self.contains_fully_qualified_placeholder(format!("{}:{}", file.as_ref(), library.as_ref())) } @@ -400,9 +423,14 @@ pub struct DeployedBytecode { } impl DeployedBytecode { - /// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty + /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode. + pub fn bytes(&self) -> Option<&Bytes> { + self.bytecode.as_ref().and_then(|bytecode| bytecode.object.as_bytes()) + } + + /// Returns the underlying `Bytes` if the object is a valid bytecode. pub fn into_bytes(self) -> Option { - self.bytecode?.object.into_bytes() + self.bytecode.and_then(|bytecode| bytecode.object.into_bytes()) } } @@ -432,6 +460,16 @@ impl CompactDeployedBytecode { Self { bytecode: Some(CompactBytecode::empty()), immutable_references: Default::default() } } + /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode. + pub fn bytes(&self) -> Option<&Bytes> { + self.bytecode.as_ref().and_then(|bytecode| bytecode.object.as_bytes()) + } + + /// Returns the underlying `Bytes` if the object is a valid bytecode. + pub fn into_bytes(self) -> Option { + self.bytecode.and_then(|bytecode| bytecode.object.into_bytes()) + } + /// Returns the parsed source map /// /// See also diff --git a/src/artifacts/mod.rs b/src/artifacts/mod.rs index 1b886d6a..77b8008a 100644 --- a/src/artifacts/mod.rs +++ b/src/artifacts/mod.rs @@ -53,6 +53,14 @@ pub(crate) type VersionedFilteredSources = BTreeMap Self { @@ -63,13 +71,6 @@ impl Default for CompilerInput { } } } -/// Input type `solc` expects -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct CompilerInput { - pub language: String, - pub sources: Sources, - pub settings: Settings, -} impl CompilerInput { /// Reads all contracts found under the path @@ -494,9 +495,8 @@ impl Settings { /// Adds `ast` to output #[must_use] pub fn with_ast(mut self) -> Self { - let output = - self.output_selection.as_mut().entry("*".to_string()).or_insert_with(BTreeMap::default); - output.insert("".to_string(), vec!["ast".to_string()]); + let output = self.output_selection.as_mut().entry("*".to_string()).or_default(); + output.insert(String::new(), vec!["ast".to_string()]); self }