use regex::Regex; use std::path::PathBuf; use once_cell::sync::Lazy; static PATH_REGEX: Lazy = Lazy::new(|| { Regex::new(r"^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$").unwrap() }); pub fn path_exists(s: &str) -> Result { 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 { 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)) }