use crate::{asm::Instruction, diag::Result};
#[derive(PartialEq, Debug)]
pub enum Layout {
Assembly,
Source,
}
impl From<bool> for Layout {
fn from(src_available: bool) -> Self {
if src_available {
Self::Source
} else {
Self::Assembly
}
}
}
pub trait PrintFn = FnMut(&Instruction, &Layout) -> Result<Option<String>>;
pub fn default(instruction: &Instruction, _: &Layout) -> Result<Option<String>> {
let ret = format!("{instruction}");
println!("{ret}");
Ok(Some(ret))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_print_default_returns_string() {
let parser = crate::asm::Parser::new().expect("parser new");
let opcode: [u8; 5] = [0xe8, 0x05, 0x00, 0x00, 0x00];
let inst = parser
.get_instruction_from(&opcode, 0x1000)
.expect("decoding");
let res = default(&inst, &Layout::Assembly).expect("print default failed");
assert!(res.is_some());
let s = res.unwrap();
assert!(s.contains("call") || s.contains("callq"));
}
}