Skip to main content

app_json_settings/core/
validation.rs

1use crate::core::error::{ConfigError, Result};
2
3/// Returns `true` when `value` is safe to use as a single file name.
4///
5/// This intentionally rejects path separators and drive separators regardless
6/// of the host OS so tests behave consistently on Windows, macOS, and Unix.
7pub fn is_plain_file_name(value: &str) -> bool {
8    is_safe_path_component(value)
9}
10
11/// Returns `true` when `value` is safe to append as one path component.
12pub fn is_safe_path_component(value: &str) -> bool {
13    !value.is_empty()
14        && value != "."
15        && value != ".."
16        && !value.contains('/')
17        && !value.contains('\\')
18        && !value.contains(':')
19        && !value.chars().any(char::is_control)
20}
21
22pub fn validate_plain_file_name(value: &str) -> Result<&str> {
23    if is_plain_file_name(value) {
24        Ok(value)
25    } else {
26        Err(ConfigError::InvalidPathComponent(value.to_string()))
27    }
28}
29
30pub fn validate_path_component(value: &str) -> Result<&str> {
31    if is_safe_path_component(value) {
32        Ok(value)
33    } else {
34        Err(ConfigError::InvalidPathComponent(value.to_string()))
35    }
36}