aboutsummaryrefslogtreecommitdiffstats
path: root/zone_core
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-02-13 19:46:18 -0600
committerToby Vincent <tobyv13@gmail.com>2022-02-13 19:46:18 -0600
commit96e571eca2991a19fc9bcdc26e451c4fad5bc791 (patch)
tree3789eeb445efb95c529f0ce6279560f412dda4c5 /zone_core
parentbbe0eff61d636dd1df14f93d03d2ab1ce9f6c61e (diff)
refactor: restructure inter-crate dependencies
Fixes #19
Diffstat (limited to 'zone_core')
-rw-r--r--zone_core/Cargo.toml3
-rw-r--r--zone_core/src/lib.rs43
2 files changed, 46 insertions, 0 deletions
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<FileSystem> for Container {
+ type Error = anyhow::Error;
+
+ fn try_from(file_system: FileSystem) -> Result<Self, Self::Error> {
+ 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::<u64>().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<zone_nspawn::Container> for Container {
+ type Error = anyhow::Error;
+
+ fn try_from(_value: zone_nspawn::Container) -> Result<Self, Self::Error> {
+ todo!()
+ }
+}