use std::{borrow::Cow, collections::BTreeMap, path::PathBuf, str::FromStr}; use serde::Deserialize; use crate::disk_mapping::DiskMapping; #[derive(Clone, Copy, Debug, Deserialize, PartialEq)] pub enum VdevMode { #[serde(rename = "mirror")] Mirror, #[serde(rename = "raidz")] Raidz, #[serde(rename = "raidz1")] Raidz1, #[serde(rename = "raidz2")] Raidz2, #[serde(rename = "raidz3")] Raidz3, } impl VdevMode { fn str(&self) -> &'static str { match self { Self::Mirror => "mirror", Self::Raidz => "raidz", Self::Raidz1 => "raidz1", Self::Raidz2 => "raidz2", Self::Raidz3 => "raidz3", } } } #[derive(Clone, Debug, Deserialize)] pub struct Vdev { pub mode: VdevMode, pub members: Vec, } impl Vdev { pub fn cli_args(&self, disk_mapper: &DiskMapping) -> anyhow::Result>> { let mut args = Vec::with_capacity(self.members.len() + 1); if self.members.len() > 1 || self.mode != VdevMode::Mirror { args.push(Cow::Borrowed(self.mode.str())); } for member in self.members.iter() { let resolved = disk_mapper.resolve(member)?; args.push(resolved.into()); } Ok(args) } } #[derive(Clone, Debug, Deserialize)] #[serde(transparent)] pub struct Vdevs(pub Vec); impl FromStr for Vdevs { type Err = anyhow::Error; fn from_str(s: &str) -> Result { common::json::from_str(s) } } #[derive(Clone, Debug, Deserialize)] #[serde(transparent)] pub struct Options(pub BTreeMap); impl FromStr for Options { type Err = anyhow::Error; fn from_str(s: &str) -> Result { common::json::from_str(s) } } #[derive(Clone, Debug, Deserialize)] pub struct Dataset { pub options: Options, pub mountpoint: Option, } #[derive(Clone, Debug, Deserialize)] #[serde(transparent)] pub struct Datasets(pub BTreeMap); impl FromStr for Datasets { type Err = anyhow::Error; fn from_str(s: &str) -> Result { common::json::from_str(s) } }