use std::path::Path;
use install_framework_core::interface::InstallMethod;
use super::registry;
use crate::error::Error;
#[cfg(windows)]
pub fn remove_path<TError: Error>(bin_dir: &Path, method: InstallMethod) -> Result<(), TError::ErrorType>
{
let value = match method
{
InstallMethod::UserInstall => registry::get_path(true),
InstallMethod::SystemInstall => registry::get_path(false)
};
match value
{
Err(e) => return Err(TError::generic(format!("Error accessing registry: {:?}", e))),
Ok(mut v) =>
{
let old = v.len();
v.retain(|d|
{
return d != bin_dir.as_os_str();
});
if v.len() == old
{
return Ok(());
}
let res = match method
{
InstallMethod::UserInstall => registry::set_path(true, v),
InstallMethod::SystemInstall => registry::set_path(false, v)
};
if let Err(e) = res
{
return Err(TError::generic(format!("Error accessing registry: {:?}", e)));
}
}
};
return Ok(());
}
#[cfg(windows)]
pub fn append_path<TError: Error>(bin_dir: &Path, method: InstallMethod) -> Result<(), TError::ErrorType>
{
use std::ffi::OsString;
let value = match method
{
InstallMethod::UserInstall => registry::get_path(true),
InstallMethod::SystemInstall => registry::get_path(false)
};
match value
{
Err(e) => return Err(TError::generic(format!("Error accessing registry: {:?}", e))),
Ok(mut v) =>
{
for p in &v
{
if p == bin_dir.as_os_str()
{
return Ok(());
}
}
v.push(OsString::from(bin_dir.as_os_str()));
let res = match method
{
InstallMethod::UserInstall => registry::set_path(true, v),
InstallMethod::SystemInstall => registry::set_path(false, v)
};
if let Err(e) = res
{
return Err(TError::generic(format!("Error accessing registry: {:?}", e)));
}
}
};
return Ok(());
}