isothrum_runner/graphical/
native.rs1use std::path::PathBuf;
11
12use serde::{Deserialize, Serialize};
13
14use super::no_console;
15use crate::Result;
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
19#[serde(rename_all = "snake_case", tag = "kind", content = "at")]
20#[non_exhaustive]
21pub enum NativeNeed {
22 Program(String),
24 MacApp(PathBuf),
26 Browser,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
33#[non_exhaustive]
34pub struct NativeApp {
35 pub name: String,
37 pub needs: Vec<NativeNeed>,
40}
41
42impl NativeApp {
43 pub(crate) fn new(name: &str, needs: Vec<NativeNeed>) -> NativeApp {
45 NativeApp { name: name.to_owned(), needs }
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51#[serde(rename_all = "snake_case", tag = "way")]
52#[non_exhaustive]
53pub enum NativeOpen {
54 Program {
56 program: String,
58 args: Vec<String>,
60 },
61 MacApp {
63 app: PathBuf,
65 args: Vec<String>,
67 },
68 Url {
70 url: String,
72 },
73}
74
75impl NativeOpen {
76 #[must_use]
78 pub fn need(&self) -> NativeNeed {
79 match self {
80 NativeOpen::Program { program, .. } => NativeNeed::Program(program.clone()),
81 NativeOpen::MacApp { app, .. } => NativeNeed::MacApp(app.clone()),
82 NativeOpen::Url { .. } => NativeNeed::Browser,
83 }
84 }
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
89#[non_exhaustive]
90pub struct NativeConsole {
91 pub machine: String,
93 pub app: String,
95 pub ways: Vec<NativeOpen>,
98}
99
100impl NativeConsole {
101 pub(crate) fn new(machine: &str, app: &NativeApp, ways: Vec<NativeOpen>) -> NativeConsole {
103 NativeConsole {
104 machine: machine.to_owned(),
105 app: app.name.clone(),
106 ways,
107 }
108 }
109}
110
111pub(crate) fn program<'a>(program: &str, args: impl IntoIterator<Item = &'a str>) -> NativeOpen {
113 NativeOpen::Program {
114 program: program.to_owned(),
115 args: args.into_iter().map(str::to_owned).collect(),
116 }
117}
118
119pub(crate) fn needs_program(program: &str) -> NativeNeed {
121 NativeNeed::Program(program.to_owned())
122}
123
124pub(crate) fn argument<'a>(machine: &str, value: &'a str) -> Result<&'a str> {
127 if value.trim().is_empty() || value.starts_with('-') || value.chars().any(char::is_control) {
128 return Err(no_console(
129 machine,
130 format!("its VM's name or file `{}` can't be handed to its app", value.escape_debug()),
131 ));
132 }
133 Ok(value)
134}
135
136pub(crate) fn host_name(h: &str) -> bool {
138 !h.is_empty() && h.len() <= 253 && h.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.') && !h.starts_with('-')
139}
140
141pub(crate) fn no_app(machine: &str) -> crate::error::Error {
143 no_console(machine, "its hypervisor has no app of its own to show it in")
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn an_argument_that_could_be_read_as_more_is_refused() {
152 assert_eq!(argument("web", "web-vm - web").unwrap(), "web-vm - web");
153 for bad in ["", " ", "--type=gui", "a\nb", "a\u{0}b"] {
154 assert_eq!(argument("web", bad).unwrap_err().code(), crate::ErrorCode::NoConsole, "{bad:?}");
155 }
156 }
157
158 #[test]
159 fn a_host_name_is_only_a_name() {
160 assert!(host_name("172.16.1.20") && host_name("pve.stack.example"));
161 for bad in ["", "-x", "a b", "user@host", "host/path", "host?q", "host#x", "h:1"] {
162 assert!(!host_name(bad), "{bad}");
163 }
164 }
165}