use std::{collections::HashSet, path::Path};
use cargo_toml::{DependencyDetail, Manifest};
use faccess::{AccessMode, PathExt};
use crate::error::Error;
pub fn get_dependency_paths(
source_path: &Path,
dependencies: &mut HashSet<String>,
) -> Result<(), Error> {
let source_manifest =
Manifest::from_path(source_path.join("Cargo.toml")).map_err(|_| Error::ManifestFailure)?;
for dependency in source_manifest.dependencies.values() {
if let Some(DependencyDetail {
path: Some(current_path),
..
}) = dependency.detail()
{
let derived_path = get_absolute_path(current_path).unwrap_or(get_absolute_path(
source_path.join(current_path).as_os_str().to_str().unwrap(),
)?);
if !dependencies.contains(&derived_path) {
dependencies.insert(derived_path.clone());
let _ = get_dependency_paths(Path::new(&derived_path), dependencies);
}
}
}
Ok(())
}
pub fn package_name(current_dir: &Path) -> Result<String, Error> {
Manifest::from_path(current_dir.join("Cargo.toml"))
.map(|f| (f.package.as_ref().unwrap()).name.to_string())
.map_err(|_| Error::ManifestFailure)
}
pub fn get_absolute_path(dir: &str) -> Result<String, Error> {
let canonicalized_path =
dunce::canonicalize(dir).map_err(|_| Error::InvalidDependencyPath)?;
canonicalized_path
.access(AccessMode::WRITE)
.map(|_| String::from(canonicalized_path.to_string_lossy()))
.map_err(|_| Error::InvalidDependencyPath)
}