use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GuestType {
Shell,
Http,
}
impl GuestType {
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Shell => "shell",
Self::Http => "http",
}
}
#[must_use]
pub fn binary(&self) -> &'static [u8] {
match self {
Self::Shell => GUEST_BINARIES.shell,
Self::Http => GUEST_BINARIES.http,
}
}
pub fn all() -> impl Iterator<Item = Self> {
[Self::Shell, Self::Http].into_iter()
}
}
impl fmt::Display for GuestType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
pub struct GuestBinaries {
pub shell: &'static [u8],
pub http: &'static [u8],
}
pub static GUEST_BINARIES: GuestBinaries = GuestBinaries {
shell: include_bytes!(concat!(env!("OUT_DIR"), "/guests/shell_guest")),
http: include_bytes!(concat!(env!("OUT_DIR"), "/guests/http_guest")),
};
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[test]
fn guest_type_name_shell() {
assert_eq!(GuestType::Shell.name(), "shell");
}
#[test]
fn guest_type_name_http() {
assert_eq!(GuestType::Http.name(), "http");
}
#[test]
fn guest_type_display_shell() {
assert_eq!(format!("{}", GuestType::Shell), "shell");
}
#[test]
fn guest_type_display_http() {
assert_eq!(format!("{}", GuestType::Http), "http");
}
#[test]
fn guest_type_all_returns_both() {
let types: Vec<GuestType> = GuestType::all().collect();
assert_eq!(types.len(), 2);
assert!(types.contains(&GuestType::Shell));
assert!(types.contains(&GuestType::Http));
}
#[test]
fn guest_type_clone() {
let original = GuestType::Shell;
let cloned = original.clone();
assert_eq!(original, cloned);
}
#[test]
fn guest_type_copy() {
let original = GuestType::Http;
let copied = original; assert_eq!(original, copied);
}
#[test]
fn guest_type_equality() {
assert_eq!(GuestType::Shell, GuestType::Shell);
assert_eq!(GuestType::Http, GuestType::Http);
assert_ne!(GuestType::Shell, GuestType::Http);
}
#[test]
fn guest_type_hash() {
let mut set = HashSet::new();
set.insert(GuestType::Shell);
set.insert(GuestType::Http);
assert_eq!(set.len(), 2);
assert!(set.contains(&GuestType::Shell));
assert!(set.contains(&GuestType::Http));
}
#[test]
fn guest_type_debug() {
assert!(format!("{:?}", GuestType::Shell).contains("Shell"));
assert!(format!("{:?}", GuestType::Http).contains("Http"));
}
#[test]
fn guest_binaries_not_empty() {
assert!(!GUEST_BINARIES.shell.is_empty());
assert!(!GUEST_BINARIES.http.is_empty());
}
#[test]
fn guest_type_binary() {
let shell_bytes = GuestType::Shell.binary();
let http_bytes = GuestType::Http.binary();
assert!(!shell_bytes.is_empty());
assert!(!http_bytes.is_empty());
}
}