use std::collections::HashMap;
use std::env::consts::ARCH;
use std::path::PathBuf;
use std::vec::Vec;
use serde_derive::Deserialize;
#[derive(Deserialize, Clone)]
pub struct Mount {
pub host_path: PathBuf,
#[serde(default)]
pub writable: bool,
}
#[derive(Deserialize, Clone)]
pub struct VMConfig {
#[serde(default = "VMConfig::default_cpus")]
pub num_cpus: u8,
#[serde(default = "VMConfig::default_memory")]
pub memory: String,
#[serde(default = "HashMap::new")]
pub mounts: HashMap<String, Mount>,
pub bios: Option<PathBuf>,
#[serde(default = "Vec::new")]
pub extra_args: Vec<String>,
}
impl VMConfig {
fn default_cpus() -> u8 {
2
}
fn default_memory() -> String {
"4G".into()
}
}
impl Default for VMConfig {
fn default() -> Self {
Self {
num_cpus: Self::default_cpus(),
memory: Self::default_memory(),
mounts: HashMap::new(),
bios: None,
extra_args: Vec::new(),
}
}
}
#[derive(Deserialize, Clone)]
pub struct Target {
pub name: String,
pub image: Option<PathBuf>,
#[serde(default)]
pub uefi: bool,
pub kernel: Option<PathBuf>,
pub kernel_args: Option<String>,
#[serde(default = "Target::default_rootfs")]
pub rootfs: PathBuf,
#[serde(default = "Target::default_arch")]
pub arch: String,
pub qemu_command: Option<String>,
pub command: String,
#[serde(default)]
pub vm: VMConfig,
}
impl Target {
pub fn default_rootfs() -> PathBuf {
"/".into()
}
pub fn default_arch() -> String {
ARCH.to_string()
}
}
impl Default for Target {
fn default() -> Self {
Self {
name: "".into(),
image: None,
uefi: false,
kernel: None,
kernel_args: None,
rootfs: Self::default_rootfs(),
arch: Self::default_arch(),
qemu_command: None,
command: "".into(),
vm: VMConfig::default(),
}
}
}
#[derive(Deserialize)]
pub struct Config {
pub target: Vec<Target>,
}
#[test]
fn test_triple_quoted_strings_are_literal() {
let config: Config = toml::from_str(
r#"
[[target]]
name = "test"
command = '''this string has 'single' and "double" quotes'''
"#,
)
.unwrap();
assert_eq!(
config.target[0].command,
r#"this string has 'single' and "double" quotes"#
);
}
#[test]
fn test_triple_quoted_strings_backslash() {
let config: Config = toml::from_str(
r#"
[[target]]
name = "test"
command = '''this string has \back \slash\es'''
"#,
)
.unwrap();
assert_eq!(
config.target[0].command,
r#"this string has \back \slash\es"#
);
}
#[test]
fn test_default_vmconfig() {
let config: Config = toml::from_str(
r#"
[[target]]
name = "test"
command = "real command"
"#,
)
.unwrap();
assert_eq!(config.target[0].vm.memory, "4G");
assert_eq!(config.target[0].vm.num_cpus, 2);
assert_eq!(config.target[0].vm.bios, None);
assert_eq!(config.target[0].vm.extra_args.len(), 0);
assert_eq!(config.target[0].vm.mounts.len(), 0);
}