x64asm/convert/
mod.rs

1// This file is part of "x64asm"
2// Under the MIT License
3// Copyright (c) 2023 Antonin Hérault
4
5mod separator;
6
7pub use separator::Separator;
8
9use crate::{Instruction, instruction::Mnemonic};
10
11/// Trait to convert anything into assembly code or part of code. 
12pub trait ToAssembly {
13    /// Converts anything into a `String`, which is necessarily a valid 
14    /// assembly or a part of code. 
15    fn to_assembly(&self, separator: Separator) -> String;
16}
17
18/// Converts all the instructions into assembly syntax-valid code calling the 
19/// same function.
20impl ToAssembly for Vec<Instruction> {
21    fn to_assembly(&self, separator: Separator) -> String {
22        self.into_iter().map(|instruction| {
23            let start = match &instruction.mnemonic {
24                Mnemonic::Label(_) => "",
25                Mnemonic::Section(_) => "",
26                _ => "\t",
27            };
28
29            format!("{}{}\n", start, instruction.to_assembly(separator))
30        })
31        .collect::<String>()
32    }
33}