use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use super::no_console;
use crate::Result;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case", tag = "kind", content = "at")]
#[non_exhaustive]
pub enum NativeNeed {
Program(String),
MacApp(PathBuf),
Browser,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct NativeApp {
pub name: String,
pub needs: Vec<NativeNeed>,
}
impl NativeApp {
pub(crate) fn new(name: &str, needs: Vec<NativeNeed>) -> NativeApp {
NativeApp { name: name.to_owned(), needs }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "way")]
#[non_exhaustive]
pub enum NativeOpen {
Program {
program: String,
args: Vec<String>,
},
MacApp {
app: PathBuf,
args: Vec<String>,
},
Url {
url: String,
},
}
impl NativeOpen {
#[must_use]
pub fn need(&self) -> NativeNeed {
match self {
NativeOpen::Program { program, .. } => NativeNeed::Program(program.clone()),
NativeOpen::MacApp { app, .. } => NativeNeed::MacApp(app.clone()),
NativeOpen::Url { .. } => NativeNeed::Browser,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NativeConsole {
pub machine: String,
pub app: String,
pub ways: Vec<NativeOpen>,
}
impl NativeConsole {
pub(crate) fn new(machine: &str, app: &NativeApp, ways: Vec<NativeOpen>) -> NativeConsole {
NativeConsole {
machine: machine.to_owned(),
app: app.name.clone(),
ways,
}
}
}
pub(crate) fn program<'a>(program: &str, args: impl IntoIterator<Item = &'a str>) -> NativeOpen {
NativeOpen::Program {
program: program.to_owned(),
args: args.into_iter().map(str::to_owned).collect(),
}
}
pub(crate) fn needs_program(program: &str) -> NativeNeed {
NativeNeed::Program(program.to_owned())
}
pub(crate) fn argument<'a>(machine: &str, value: &'a str) -> Result<&'a str> {
if value.trim().is_empty() || value.starts_with('-') || value.chars().any(char::is_control) {
return Err(no_console(
machine,
format!("its VM's name or file `{}` can't be handed to its app", value.escape_debug()),
));
}
Ok(value)
}
pub(crate) fn host_name(h: &str) -> bool {
!h.is_empty() && h.len() <= 253 && h.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.') && !h.starts_with('-')
}
pub(crate) fn no_app(machine: &str) -> crate::error::Error {
no_console(machine, "its hypervisor has no app of its own to show it in")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_argument_that_could_be_read_as_more_is_refused() {
assert_eq!(argument("web", "web-vm - web").unwrap(), "web-vm - web");
for bad in ["", " ", "--type=gui", "a\nb", "a\u{0}b"] {
assert_eq!(argument("web", bad).unwrap_err().code(), crate::ErrorCode::NoConsole, "{bad:?}");
}
}
#[test]
fn a_host_name_is_only_a_name() {
assert!(host_name("172.16.1.20") && host_name("pve.stack.example"));
for bad in ["", "-x", "a b", "user@host", "host/path", "host?q", "host#x", "h:1"] {
assert!(!host_name(bad), "{bad}");
}
}
}