Skip to main content

arcbox_helper/validate/
cli_name.rs

1use std::str::FromStr;
2
3/// A validated CLI tool name from the allow list (e.g. `docker`).
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct CliName(String);
6
7impl CliName {
8    pub fn as_str(&self) -> &str {
9        &self.0
10    }
11}
12
13impl FromStr for CliName {
14    type Err = String;
15
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        if arcbox_constants::paths::DOCKER_CLI_TOOLS.contains(&s) {
18            Ok(Self(s.to_owned()))
19        } else {
20            Err(format!(
21                "CLI name '{s}' is not in the allow list: {}",
22                arcbox_constants::paths::DOCKER_CLI_TOOLS.join(", ")
23            ))
24        }
25    }
26}
27
28impl std::fmt::Display for CliName {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        f.write_str(&self.0)
31    }
32}