py_executer_lib/path.rs
1use anyhow::{anyhow, Result};
2use colored::*;
3use std::path::PathBuf;
4
5/// Parse and validate a script path.
6///
7/// The function takes a `PathBuf` as argument and checks if the path exists.
8/// If the path exists, it returns a tuple of `(PathBuf, PathBuf)`, where the first element
9/// is the absolute path of the script and the second element is the parent directory of
10/// the script.
11///
12/// # Errors
13///
14/// The function returns an `Err` if the path does not exist or if the parent directory
15/// cannot be obtained.
16pub fn parse_and_validate_script_path(script_path: &PathBuf) -> Result<PathBuf> {
17 match script_path.canonicalize() {
18 Ok(path) => {
19 if !path.exists() {
20 return Err(anyhow!("{} not exists", path.display().to_string().bold()));
21 }
22 Ok(path)
23 }
24 Err(err) => Err(anyhow!("Failed to get absolute path of script: {}", err)),
25 }
26}