Skip to main content

shell_sanitize/
marker.rs

1use std::fmt;
2
3/// Marker trait for sanitized value categories.
4///
5/// Implementing this trait on a zero-sized type lets you create
6/// domain-specific [`Sanitized<T>`](crate::Sanitized) values.
7///
8/// # Example
9///
10/// ```
11/// use shell_sanitize::MarkerType;
12///
13/// struct DatabaseInput;
14/// impl MarkerType for DatabaseInput {
15///     fn label() -> &'static str { "database_input" }
16/// }
17/// ```
18pub trait MarkerType: Send + Sync + 'static {
19    /// Human-readable label for this marker category.
20    fn label() -> &'static str;
21}
22
23/// Marker: value is safe to use as a shell argument.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct ShellArg;
26
27impl MarkerType for ShellArg {
28    fn label() -> &'static str {
29        "shell_arg"
30    }
31}
32
33impl fmt::Display for ShellArg {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "ShellArg")
36    }
37}
38
39/// Marker: value is safe to use as a file path component.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41pub struct FilePath;
42
43impl MarkerType for FilePath {
44    fn label() -> &'static str {
45        "file_path"
46    }
47}
48
49impl fmt::Display for FilePath {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "FilePath")
52    }
53}