qemu_command_builder/args/
global.rs1use std::str::FromStr;
2
3use bon::Builder;
4use proptest_derive::Arbitrary;
5
6use crate::parsers::{ARG_GLOBAL, DELIM_COMMA};
7use crate::to_command::ToCommand;
8
9#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Default, Builder, Arbitrary)]
11pub struct Global {
12 driver: String,
14 property: String,
16 value: String,
18}
19
20impl ToCommand for Global {
21 fn command(&self) -> String {
22 ARG_GLOBAL.to_string()
23 }
24 fn to_args(&self) -> Vec<String> {
25 vec![format!("driver={},property={},value={}", self.driver, self.property, self.value)]
26 }
27}
28
29impl FromStr for Global {
30 type Err = String;
31
32 fn from_str(s: &str) -> Result<Self, Self::Err> {
33 if let Some((left, value)) = s.rsplit_once('=')
34 && !left.contains(',')
35 && let Some((driver, property)) = left.rsplit_once('.')
36 {
37 return Ok(Self {
38 driver: driver.to_string(),
39 property: property.to_string(),
40 value: value.to_string(),
41 });
42 }
43
44 let mut driver = None;
45 let mut property = None;
46 let mut value_field = None;
47 for part in s.split(DELIM_COMMA) {
48 let (key, value) = part.split_once('=').ok_or_else(|| format!("invalid -global option: {part}"))?;
49 match key {
50 "driver" => driver = Some(value.to_string()),
51 "property" => property = Some(value.to_string()),
52 "value" => value_field = Some(value.to_string()),
53 other => return Err(format!("unsupported -global option: {other}")),
54 }
55 }
56 Ok(Self {
57 driver: driver.ok_or_else(|| "global requires driver=".to_string())?,
58 property: property.ok_or_else(|| "global requires property=".to_string())?,
59 value: value_field.ok_or_else(|| "global requires value=".to_string())?,
60 })
61 }
62}