#![forbid(unsafe_code)]
#![deny(missing_docs)]
mod config;
mod dep;
mod error;
mod graph;
mod project;
mod registries;
mod util;
pub use config::Config;
pub use error::{Error, Result};
use graph::DepGraph;
use project::Project;
use std::{io::BufWriter, path::Path};
pub fn get_dep_graph(cfg: Config) -> Result<DepGraph> {
let manifest_path = &cfg.manifest_path;
is_cargo_toml(manifest_path)?;
let manifest_path = util::find_file_search_parent_dirs(manifest_path)?;
let manifest = manifest_path.to_str().unwrap();
let lock_file = format!("{}.lock", &manifest[0..manifest.len() - 5]);
let lock_path = util::find_file_search_parent_dirs(&lock_file)?;
let project = Project::with_config(cfg)?;
project.graph(manifest_path, lock_path)
}
pub fn render_dep_graph(graph: DepGraph) -> Result<String> {
let bytes: Vec<u8> = Vec::new();
let mut writer = BufWriter::new(bytes);
graph.render_to(&mut writer)?;
String::from_utf8(writer.into_inner()?).map_err(|err| Error::Generic(err.to_string()))
}
fn is_cargo_toml(file_name: &str) -> Result<()> {
let path = Path::new(file_name);
if let Some(file_name) = path.file_name() {
if file_name != "Cargo.toml" {
return Err(Error::Toml(
"The manifest-path must be a path to a Cargo.toml file".into(),
));
}
} else {
return Err(Error::Toml("The manifest path is not a valid file".into()));
}
Ok(())
}