Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix clippy warnings in avm-transpiler #4416

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
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
41 changes: 24 additions & 17 deletions avm-transpiler/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::fmt::{self, Display};
use std::fmt::{Debug, Formatter};

use crate::opcodes::AvmOpcode;
Expand Down Expand Up @@ -28,26 +28,29 @@ pub struct AvmInstruction {
/// Different instructions have different numbers of operands
pub operands: Vec<AvmOperand>,
}
impl AvmInstruction {
/// String representation for printing AVM programs
pub fn to_string(&self) -> String {
let mut out_str = format!("opcode {}", self.opcode.name());

impl Display for AvmInstruction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "opcode {}", self.opcode.name())?;
if let Some(indirect) = self.indirect {
out_str += format!(", indirect: {}", indirect).as_str();
write!(f, ", indirect: {}", indirect)?;
}
// TODO(4271): add in_tag alongside its support in TS
if let Some(dst_tag) = self.dst_tag {
out_str += format!(", dst_tag: {}", dst_tag as u8).as_str();
write!(f, ", dst_tag: {}", dst_tag as u8)?;
}
if !self.operands.is_empty() {
out_str += ", operands: [";
write!(f, ", operands: [")?;
for operand in &self.operands {
out_str += format!("{}, ", operand.to_string()).as_str();
write!(f, "{operand}, ")?;
}
out_str += "]";
}
out_str
write!(f, "]")?;
};
Ok(())
}
}

impl AvmInstruction {
/// Bytes representation for generating AVM bytecode
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
Expand All @@ -69,7 +72,7 @@ impl AvmInstruction {

impl Debug for AvmInstruction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string())
write!(f, "{self}")
}
}

Expand Down Expand Up @@ -106,14 +109,18 @@ pub enum AvmOperand {
// TODO(4267): Support operands of size other than 32 bits (for SET)
U128 { value: u128 },
}
impl AvmOperand {
pub fn to_string(&self) -> String {

impl Display for AvmOperand {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
AvmOperand::U32 { value } => format!(" U32:{}", value),
AvmOperand::U32 { value } => write!(f, " U32:{}", value),
// TODO(4267): Support operands of size other than 32 bits (for SET)
AvmOperand::U128 { value } => format!(" U128:{}", value),
AvmOperand::U128 { value } => write!(f, " U128:{}", value),
}
}
}

impl AvmOperand {
pub fn to_be_bytes(&self) -> Vec<u8> {
match self {
AvmOperand::U32 { value } => value.to_be_bytes().to_vec(),
Expand Down
3 changes: 2 additions & 1 deletion avm-transpiler/src/transpile_contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::Engine;
use log::info;
use regex::Regex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -101,7 +102,7 @@ impl From<CompiledAcirContract> for TranspiledContract {
function_type: function.function_type,
is_internal: function.is_internal,
abi: function.abi,
bytecode: base64::encode(avm_bytecode),
bytecode: base64::prelude::BASE64_STANDARD.encode(avm_bytecode),
debug_symbols: function.debug_symbols,
}));
} else {
Expand Down
Loading