From 1fef014508c915b388de371583b0a869684fc7ea Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Tue, 12 Apr 2022 13:30:15 -0400 Subject: Add `House` definition and impls Also rename modules to make more sense with the theme --- src/house.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/house.rs (limited to 'src/house.rs') diff --git a/src/house.rs b/src/house.rs new file mode 100644 index 0000000..6159c33 --- /dev/null +++ b/src/house.rs @@ -0,0 +1,61 @@ +/*! + * `House`s are where [crate::waifu::Waifu]s live (the physical hypervisors that + * libvirtd connects to + */ +use crate::waifu::*; +use crate::errors::Error; +use serde::{Serialize, Deserialize}; +use std::convert::TryFrom; +use virt::connect::Connect; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + +#[derive(Serialize, Deserialize, Debug)] +pub enum Address { + IP(String), + Domain(String) +} + +impl ToString for Address { + fn to_string(&self) -> String { + match self { + Address::IP(s) => s.clone(), + Address::Domain(s) => s.clone(), + } + } +} + +/// Defines a "house" where waifus live +#[derive(Debug, Serialize, Deserialize)] +pub struct House { + /// Hostname + pub name: String, + /// FQDN or IP address, a way to talk to the house + pub address: Address +} + +impl House { + pub fn new(hostname: String, addr: Address) -> Result { + let mut c = Connect::open(&format!("qemu+ssh://{}/system", addr.to_string()))?; + + // If the connection succeeds, we've got one! + Ok(Self { + name: hostname.clone(), + address: addr + }) + } +} + +#[cfg(test)] +mod test { + use super::*; + /// Kind of a stupid test, but just a sanity check to make sure that [ToString] impl up above + /// is still working + #[test] + fn addr_to_string() { + let a: Address = Address::IP("127.0.0.1".to_string()); + let d: Address = Address::Domain("example.com".to_string()); + + assert_eq!(String::from("127.0.0.1"), a.to_string()); + assert_eq!("example.com".to_string(), d.to_string()); + } +} -- cgit v1.2.3