firecracker_rs_sdk/models/
kernel_args.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
6pub struct KernelArgs(pub HashMap<String, Option<String>>);
7
8// serialize the kernelArgs back to a string that can be provided
9// to the kernel
10impl ToString for KernelArgs {
11    fn to_string(&self) -> String {
12        let mut fields: Vec<String> = Vec::new();
13        self.0.iter().for_each(|(key, value)| {
14            let mut field = key.to_owned();
15            if let Some(s) = value.as_ref() {
16                field.push_str("=");
17                field += s;
18            }
19            fields.push(field);
20        });
21        fields.join(" ")
22    }
23}
24
25// deserialize the provided string to a kernelArgs map
26impl From<String> for KernelArgs {
27    fn from(raw_string: String) -> Self {
28        let mut arg_map: HashMap<String, Option<String>> = HashMap::new();
29        raw_string.split_ascii_whitespace().for_each(|kv_pair| {
30            if let Some((key, value)) = kv_pair.split_once("=") {
31                arg_map.insert(key.into(), Some(value.into()));
32            } else {
33                arg_map.insert(kv_pair.into(), None);
34            }
35        }); 
36        
37        Self(arg_map)
38    }
39}