From 96e571eca2991a19fc9bcdc26e451c4fad5bc791 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 13 Feb 2022 19:46:18 -0600 Subject: refactor: restructure inter-crate dependencies Fixes #19 --- zone_core/Cargo.toml | 3 +++ zone_core/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'zone_core') diff --git a/zone_core/Cargo.toml b/zone_core/Cargo.toml index 8ac3fa3..6051564 100644 --- a/zone_core/Cargo.toml +++ b/zone_core/Cargo.toml @@ -19,3 +19,6 @@ serde = "1.0.136" strum = "0.23.0" strum_macros = "0.23.1" tabled = "0.4.2" +zone_zfs = { version = "0.1.0", path = "../zone_zfs" } +zone_nspawn = { version = "0.1.0", path = "../zone_nspawn" } +anyhow = "1.0.53" diff --git a/zone_core/src/lib.rs b/zone_core/src/lib.rs index ca204fd..1c7fab2 100644 --- a/zone_core/src/lib.rs +++ b/zone_core/src/lib.rs @@ -1,9 +1,13 @@ +use std::path::PathBuf; + +use anyhow::{Context}; use clap::Args; use rocket::{FromForm, FromFormField}; use rocket_okapi::okapi::schemars::{self, JsonSchema}; use serde::{Deserialize, Serialize}; use strum_macros::{Display, EnumString}; use tabled::Tabled; +use zone_zfs::file_system::FileSystem; pub static DEFAULT_ENDPOINT: &str = "127.0.0.1:8000"; @@ -61,3 +65,42 @@ impl PartialEqOrDefault for Container { && (self.status == other.status || self.status == Self::default().status) } } + +impl TryFrom for Container { + type Error = anyhow::Error; + + fn try_from(file_system: FileSystem) -> Result { + let path_buf = PathBuf::from(&file_system) + .file_name() + .context(format!("Invalid FileSystem path: {:?}", file_system))? + .to_string_lossy() + .into_owned(); + + let (user, id) = path_buf + .rsplit_once("-") + .context(format!("Invalid FileSystem name: {:?}", file_system))?; + + let id = id.parse::().context("Failed to parse container ID")?; + + let template = PathBuf::from(&file_system) + .parent() + .context(format!("Invalid path for filesystem: {:?}", &file_system))? + .to_string_lossy() + .into_owned(); + + Ok(Container { + id, + template, + user: user.to_string(), + status: ContainerStatus::default(), + }) + } +} + +impl TryFrom for Container { + type Error = anyhow::Error; + + fn try_from(_value: zone_nspawn::Container) -> Result { + todo!() + } +} -- cgit v1.2.3-70-g09d2