qemu_command_builder/
cpu.rs

1use std::ops::Add;
2
3use bon::Builder;
4
5use crate::ToCommand;
6use crate::cpu_flags::CPUFlags;
7use crate::cpu_type::{CpuTypeAarch64, CpuTypeX86_64};
8
9#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder)]
10pub struct CpuX86 {
11    cpu_type: CpuTypeX86_64,
12    migratable: Option<bool>,
13    flags: Vec<CPUFlags>,
14}
15
16impl CpuX86 {
17    pub fn new(cpu_type: CpuTypeX86_64) -> Self {
18        CpuX86 {
19            cpu_type,
20            migratable: None,
21            flags: vec![],
22        }
23    }
24
25    pub fn migratable(&mut self, state: bool) -> &mut Self {
26        self.migratable = Some(state);
27        self
28    }
29    pub fn flags(&mut self, flags: Vec<CPUFlags>) -> &mut Self {
30        self.flags = flags;
31        self
32    }
33}
34
35impl ToCommand for CpuX86 {
36    fn to_command(&self) -> Vec<String> {
37        let mut cmd = vec!["-cpu".to_string()];
38        let mut ct = self.cpu_type.to_command().join("");
39
40        if let Some(migratable) = self.migratable
41            && migratable
42        {
43            ct.push_str(",migratable=yes");
44        }
45        let flags: Vec<String> = self
46            .flags
47            .iter()
48            .map(|v| v.to_command())
49            .collect::<Vec<Vec<String>>>()
50            .concat();
51        if !flags.is_empty() {
52            let flags: Vec<String> = flags.iter().map(|flag| "-".to_string().add(flag)).collect();
53            ct.push_str(format!(",{}", flags.join(",")).as_str());
54        }
55
56        cmd.push(ct);
57
58        cmd
59    }
60}
61
62#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder)]
63pub struct CpuAarch64 {
64    cpu_type: CpuTypeAarch64,
65}
66
67impl ToCommand for CpuAarch64 {
68    fn to_command(&self) -> Vec<String> {
69        let mut cmd = vec!["-cpu".to_string()];
70        let ct = self.cpu_type.to_command().join("");
71        cmd.push(ct);
72        cmd
73    }
74}