qemu_command_builder/args/
memory.rs1use crate::parsers::ARG_MEMORY;
2use crate::parsers::DELIM_COMMA;
3use crate::qao;
4use crate::shell_string::ShellStringError;
5use crate::to_command::ToCommand;
6use bon::Builder;
7use proptest_derive::Arbitrary;
8use std::fmt::Display;
9use std::str::FromStr;
10
11const KEY_SIZE: &str = "size=";
12const KEY_SLOTS: &str = "slots=";
13const KEY_MAXMEM: &str = "maxmem=";
14
15#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Arbitrary)]
17pub enum MemoryUnit {
18 Bytes(u64),
19 MegaBytes(u64),
20 GigaBytes(u64),
21}
22
23impl Display for MemoryUnit {
24 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25 match self {
26 MemoryUnit::Bytes(amount) => {
27 write!(f, "{}", amount)
28 }
29 MemoryUnit::MegaBytes(amount) => {
30 write!(f, "{}M", amount)
31 }
32 MemoryUnit::GigaBytes(amount) => {
33 write!(f, "{}G", amount)
34 }
35 }
36 }
37}
38
39impl FromStr for MemoryUnit {
40 type Err = ShellStringError;
41
42 fn from_str(s: &str) -> Result<Self, Self::Err> {
43 parse_memory_unit(s).map_err(ShellStringError::new)
44 }
45}
46#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder, Arbitrary)]
61pub struct Memory {
62 mem: MemoryUnit,
64 slots: Option<usize>,
66 maxmem: Option<MemoryUnit>,
68}
69
70impl ToCommand for Memory {
71 fn command(&self) -> String {
72 ARG_MEMORY.to_string()
73 }
74 fn to_args(&self) -> Vec<String> {
75 let mut args = vec![];
76
77 args.push(self.mem.to_string());
78 qao!(self.slots, args, KEY_SLOTS);
79 qao!(&self.maxmem, args, KEY_MAXMEM);
80
81 vec![args.join(DELIM_COMMA)]
82 }
83}
84
85impl FromStr for Memory {
86 type Err = ShellStringError;
87
88 fn from_str(s: &str) -> Result<Self, Self::Err> {
89 parse_memory(s).map_err(ShellStringError::new)
90 }
91}
92
93fn parse_memory_unit(s: &str) -> Result<MemoryUnit, String> {
94 if let Some(amount) = s.strip_suffix('M') {
95 return Ok(MemoryUnit::MegaBytes(amount.parse::<u64>().map_err(|e| format!("invalid memory amount: {e}"))?));
96 }
97 if let Some(amount) = s.strip_suffix('G') {
98 return Ok(MemoryUnit::GigaBytes(amount.parse::<u64>().map_err(|e| format!("invalid memory amount: {e}"))?));
99 }
100
101 Ok(MemoryUnit::Bytes(s.parse::<u64>().map_err(|e| format!("invalid memory amount: {e}"))?))
102}
103
104fn parse_memory(s: &str) -> Result<Memory, String> {
105 let mut parts = s.split(DELIM_COMMA);
106 let first = parts.next().ok_or_else(|| "empty memory argument".to_string())?;
107
108 let mem = if let Some(value) = first.strip_prefix(KEY_SIZE) {
109 parse_memory_unit(value)?
110 } else if first.contains('=') {
111 return Err(format!("unsupported memory option: {first}"));
112 } else {
113 parse_memory_unit(first)?
114 };
115
116 let mut slots = None;
117 let mut maxmem = None;
118
119 for part in parts {
120 let (key, value) = part.split_once('=').ok_or_else(|| format!("invalid memory option: {part}"))?;
121 match key {
122 "size" => return Err("size= is only valid as the first -m component".to_string()),
123 "slots" => {
124 slots = Some(value.parse::<usize>().map_err(|e| format!("invalid slots value: {e}"))?);
125 }
126 "maxmem" => maxmem = Some(parse_memory_unit(value)?),
127 other => return Err(format!("unsupported memory option: {other}")),
128 }
129 }
130
131 Ok(Memory { mem, slots, maxmem })
132}