Skip to content

Commit bb3d78b

Browse files
authored
feat: add more getter methods to bytecode structs (#30)
1 parent b1ced35 commit bb3d78b

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

src/artifacts/bytecode.rs

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl CompactBytecode {
5252
pub fn empty() -> Self {
5353
Self { object: Default::default(), source_map: None, link_references: Default::default() }
5454
}
55+
5556
/// Returns the parsed source map
5657
///
5758
/// See also <https://docs.soliditylang.org/en/v0.8.10/internals/source_mappings.html>
@@ -89,6 +90,16 @@ impl CompactBytecode {
8990
}
9091
false
9192
}
93+
94+
/// Returns the bytes of the bytecode object.
95+
pub fn bytes(&self) -> Option<&Bytes> {
96+
self.object.as_bytes()
97+
}
98+
99+
/// Returns the underlying `Bytes` if the object is a valid bytecode.
100+
pub fn into_bytes(self) -> Option<Bytes> {
101+
self.object.into_bytes()
102+
}
92103
}
93104

94105
impl From<Bytecode> for CompactBytecode {
@@ -203,78 +214,88 @@ impl Bytecode {
203214
}
204215
false
205216
}
217+
218+
/// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
219+
pub fn bytes(&self) -> Option<&Bytes> {
220+
self.object.as_bytes()
221+
}
222+
223+
/// Returns the underlying `Bytes` if the object is a valid bytecode.
224+
pub fn into_bytes(self) -> Option<Bytes> {
225+
self.object.into_bytes()
226+
}
206227
}
207228

208229
/// Represents the bytecode of a contracts that might be not fully linked yet.
209230
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
210231
#[serde(untagged)]
211232
pub enum BytecodeObject {
212-
/// Fully linked bytecode object
233+
/// Fully linked bytecode object.
213234
#[serde(deserialize_with = "serde_helpers::deserialize_bytes")]
214235
Bytecode(Bytes),
215-
/// Bytecode as hex string that's not fully linked yet and contains library placeholders
236+
/// Bytecode as hex string that's not fully linked yet and contains library placeholders.
216237
#[serde(with = "serde_helpers::string_bytes")]
217238
Unlinked(String),
218239
}
219240

220241
impl BytecodeObject {
221-
/// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty
222-
pub fn into_bytes(self) -> Option<Bytes> {
242+
/// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
243+
pub fn as_bytes(&self) -> Option<&Bytes> {
223244
match self {
224245
BytecodeObject::Bytecode(bytes) => Some(bytes),
225246
BytecodeObject::Unlinked(_) => None,
226247
}
227248
}
228249

229-
/// Returns a reference to the underlying `Bytes` if the object is a valid bytecode, and not
230-
/// empty
231-
pub fn as_bytes(&self) -> Option<&Bytes> {
250+
/// Returns the underlying `Bytes` if the object is a valid bytecode.
251+
pub fn into_bytes(self) -> Option<Bytes> {
232252
match self {
233253
BytecodeObject::Bytecode(bytes) => Some(bytes),
234254
BytecodeObject::Unlinked(_) => None,
235255
}
236256
}
237257

238-
/// Returns the number of bytes of the fully linked bytecode
258+
/// Returns the number of bytes of the fully linked bytecode.
239259
///
240260
/// Returns `0` if this object is unlinked.
241261
pub fn bytes_len(&self) -> usize {
242262
self.as_bytes().map(|b| b.as_ref().len()).unwrap_or_default()
243263
}
244264

245-
/// Returns a reference to the underlying `String` if the object is unlinked
265+
/// Returns a reference to the underlying `String` if the object is unlinked.
246266
pub fn as_str(&self) -> Option<&str> {
247267
match self {
248268
BytecodeObject::Bytecode(_) => None,
249269
BytecodeObject::Unlinked(s) => Some(s.as_str()),
250270
}
251271
}
252272

253-
/// Returns the unlinked `String` if the object is unlinked or empty
273+
/// Returns the unlinked `String` if the object is unlinked.
254274
pub fn into_unlinked(self) -> Option<String> {
255275
match self {
256276
BytecodeObject::Bytecode(_) => None,
257277
BytecodeObject::Unlinked(code) => Some(code),
258278
}
259279
}
260280

261-
/// Whether this object is still unlinked
281+
/// Whether this object is still unlinked.
262282
pub fn is_unlinked(&self) -> bool {
263283
matches!(self, BytecodeObject::Unlinked(_))
264284
}
265285

266-
/// Whether this object a valid bytecode
286+
/// Whether this object a valid bytecode.
267287
pub fn is_bytecode(&self) -> bool {
268288
matches!(self, BytecodeObject::Bytecode(_))
269289
}
270290

271291
/// Returns `true` if the object is a valid bytecode and not empty.
272-
/// Returns false the object is a valid but empty bytecode or unlinked.
292+
///
293+
/// Returns `false` if the object is a valid but empty or unlinked bytecode.
273294
pub fn is_non_empty_bytecode(&self) -> bool {
274295
self.as_bytes().map(|c| !c.0.is_empty()).unwrap_or_default()
275296
}
276297

277-
/// Tries to resolve the unlinked string object a valid bytecode object in place
298+
/// Tries to resolve the unlinked string object a valid bytecode object in place.
278299
///
279300
/// Returns the string if it is a valid
280301
pub fn resolve(&mut self) -> Option<&Bytes> {
@@ -286,7 +307,8 @@ impl BytecodeObject {
286307
self.as_bytes()
287308
}
288309

289-
/// Link using the fully qualified name of a library
310+
/// Links using the fully qualified name of a library.
311+
///
290312
/// The fully qualified library name is the path of its source file and the library name
291313
/// separated by `:` like `file.sol:Math`
292314
///
@@ -311,18 +333,19 @@ impl BytecodeObject {
311333
self
312334
}
313335

314-
/// Link using the `file` and `library` names as fully qualified name `<file>:<library>`
315-
/// See `BytecodeObject::link_fully_qualified`
336+
/// Links using the `file` and `library` names as fully qualified name `<file>:<library>`.
337+
///
338+
/// See [`link_fully_qualified`](Self::link_fully_qualified).
316339
pub fn link(
317340
&mut self,
318341
file: impl AsRef<str>,
319342
library: impl AsRef<str>,
320343
addr: Address,
321344
) -> &mut Self {
322-
self.link_fully_qualified(format!("{}:{}", file.as_ref(), library.as_ref(),), addr)
345+
self.link_fully_qualified(format!("{}:{}", file.as_ref(), library.as_ref()), addr)
323346
}
324347

325-
/// Links the bytecode object with all provided `(file, lib, addr)`
348+
/// Links the bytecode object with all provided `(file, lib, addr)`.
326349
pub fn link_all<I, S, T>(&mut self, libs: I) -> &mut Self
327350
where
328351
I: IntoIterator<Item = (S, T, Address)>,
@@ -335,7 +358,7 @@ impl BytecodeObject {
335358
self
336359
}
337360

338-
/// Whether the bytecode contains a matching placeholder using the qualified name
361+
/// Returns whether the bytecode contains a matching placeholder using the qualified name.
339362
pub fn contains_fully_qualified_placeholder(&self, name: impl AsRef<str>) -> bool {
340363
if let BytecodeObject::Unlinked(unlinked) = self {
341364
let name = name.as_ref();
@@ -346,7 +369,7 @@ impl BytecodeObject {
346369
}
347370
}
348371

349-
/// Whether the bytecode contains a matching placeholder
372+
/// Returns whether the bytecode contains a matching placeholder.
350373
pub fn contains_placeholder(&self, file: impl AsRef<str>, library: impl AsRef<str>) -> bool {
351374
self.contains_fully_qualified_placeholder(format!("{}:{}", file.as_ref(), library.as_ref()))
352375
}
@@ -400,9 +423,14 @@ pub struct DeployedBytecode {
400423
}
401424

402425
impl DeployedBytecode {
403-
/// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty
426+
/// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
427+
pub fn bytes(&self) -> Option<&Bytes> {
428+
self.bytecode.as_ref().and_then(|bytecode| bytecode.object.as_bytes())
429+
}
430+
431+
/// Returns the underlying `Bytes` if the object is a valid bytecode.
404432
pub fn into_bytes(self) -> Option<Bytes> {
405-
self.bytecode?.object.into_bytes()
433+
self.bytecode.and_then(|bytecode| bytecode.object.into_bytes())
406434
}
407435
}
408436

@@ -432,6 +460,16 @@ impl CompactDeployedBytecode {
432460
Self { bytecode: Some(CompactBytecode::empty()), immutable_references: Default::default() }
433461
}
434462

463+
/// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
464+
pub fn bytes(&self) -> Option<&Bytes> {
465+
self.bytecode.as_ref().and_then(|bytecode| bytecode.object.as_bytes())
466+
}
467+
468+
/// Returns the underlying `Bytes` if the object is a valid bytecode.
469+
pub fn into_bytes(self) -> Option<Bytes> {
470+
self.bytecode.and_then(|bytecode| bytecode.object.into_bytes())
471+
}
472+
435473
/// Returns the parsed source map
436474
///
437475
/// See also <https://docs.soliditylang.org/en/v0.8.10/internals/source_mappings.html>

src/artifacts/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ pub(crate) type VersionedFilteredSources = BTreeMap<Solc, (Version, FilteredSour
5353
const SOLIDITY: &str = "Solidity";
5454
const YUL: &str = "Yul";
5555

56+
/// Input type `solc` expects.
57+
#[derive(Clone, Debug, Serialize, Deserialize)]
58+
pub struct CompilerInput {
59+
pub language: String,
60+
pub sources: Sources,
61+
pub settings: Settings,
62+
}
63+
5664
/// Default `language` field is set to `"Solidity"`.
5765
impl Default for CompilerInput {
5866
fn default() -> Self {
@@ -63,13 +71,6 @@ impl Default for CompilerInput {
6371
}
6472
}
6573
}
66-
/// Input type `solc` expects
67-
#[derive(Clone, Debug, Serialize, Deserialize)]
68-
pub struct CompilerInput {
69-
pub language: String,
70-
pub sources: Sources,
71-
pub settings: Settings,
72-
}
7374

7475
impl CompilerInput {
7576
/// Reads all contracts found under the path
@@ -494,9 +495,8 @@ impl Settings {
494495
/// Adds `ast` to output
495496
#[must_use]
496497
pub fn with_ast(mut self) -> Self {
497-
let output =
498-
self.output_selection.as_mut().entry("*".to_string()).or_insert_with(BTreeMap::default);
499-
output.insert("".to_string(), vec!["ast".to_string()]);
498+
let output = self.output_selection.as_mut().entry("*".to_string()).or_default();
499+
output.insert(String::new(), vec!["ast".to_string()]);
500500
self
501501
}
502502

0 commit comments

Comments
 (0)