use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Runtime {
Bash,
Python,
Node,
Custom {
command: String,
flag: String,
},
}
impl Runtime {
#[inline]
pub fn resolve(&self) -> (&str, &str) {
match self {
Runtime::Bash => ("bash", "-c"),
Runtime::Node => ("node", "-e"),
Runtime::Python => ("python3", "-c"),
Runtime::Custom { command, flag } => (command.as_str(), flag.as_str()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_bash() {
let (cmd, flag) = Runtime::Bash.resolve();
assert_eq!(cmd, "bash");
assert_eq!(flag, "-c");
}
#[test]
fn resolve_python() {
let (cmd, flag) = Runtime::Python.resolve();
assert_eq!(cmd, "python3");
assert_eq!(flag, "-c");
}
#[test]
fn resolve_node() {
let (cmd, flag) = Runtime::Node.resolve();
assert_eq!(cmd, "node");
assert_eq!(flag, "-e");
}
#[test]
fn resolve_custom() {
let rt = Runtime::Custom {
command: "ruby".into(),
flag: "-e".into(),
};
let (cmd, flag) = rt.resolve();
assert_eq!(cmd, "ruby");
assert_eq!(flag, "-e");
}
#[test]
fn serde_roundtrip_bash() {
let rt = Runtime::Bash;
let json = serde_json::to_string(&rt).unwrap();
assert_eq!(json, r#""bash""#);
let back: Runtime = serde_json::from_str(&json).unwrap();
assert_eq!(back, rt);
}
#[test]
fn serde_roundtrip_custom() {
let rt = Runtime::Custom {
command: "perl".into(),
flag: "-e".into(),
};
let json = serde_json::to_string(&rt).unwrap();
let back: Runtime = serde_json::from_str(&json).unwrap();
assert_eq!(back, rt);
}
}