28 lines
698 B
Rust
28 lines
698 B
Rust
use regex::Regex;
|
|
use std::path::PathBuf;
|
|
use once_cell::sync::Lazy;
|
|
|
|
static PATH_REGEX: Lazy<Regex> = Lazy::new(|| {
|
|
Regex::new(r"^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$").unwrap()
|
|
});
|
|
|
|
pub fn path_exists(s: &str) -> Result<PathBuf, String> {
|
|
let p = PathBuf::from(s);
|
|
p.exists().then_some(p).ok_or("path does not exist".to_string())
|
|
}
|
|
|
|
pub fn valid_possible_path(s: &str) -> Result<PathBuf, String>
|
|
{
|
|
let lower = s.to_ascii_lowercase();
|
|
|
|
if lower == "true" || lower == "false" {
|
|
return Err("boolean literal is not allowed".into());
|
|
}
|
|
|
|
if PATH_REGEX.is_match(s) {
|
|
return Err("numeric literal is not allowed".into());
|
|
}
|
|
|
|
Ok(PathBuf::from(s))
|
|
}
|