1use crate::Error;
2use crate::Result;
3use std::{env, path::Path};
4
5pub fn add_system_path<P>(directory: P) -> Result<()>
8where
9 P: AsRef<Path>,
10{
11 let path_separator = if cfg!(windows) { ';' } else { ':' };
13 let mut path = env::var("PATH").unwrap_or_default();
15 let directory_str = directory
17 .as_ref()
18 .to_str()
19 .ok_or_else(|| Error::Option("add_system_path 解析路径出错".into()))?
20 .to_string();
21 if directory_str.is_empty() || path.is_empty() {
23 return Err(Error::Empty);
24 }
25 let new_path = format!("{}{}", &directory_str, path_separator);
26 if path.contains(&new_path) {
28 return Err(Error::Exists(new_path.into()));
29 }
30 path.push_str(&new_path);
31 env::set_var("PATH", path);
33
34 Ok(())
35}