1#![forbid(unsafe_code)]
10#![deny(missing_docs)]
11
12mod config;
13mod dep;
14mod error;
15mod graph;
16mod project;
17mod registries;
18mod util;
19
20pub use config::Config;
21pub use error::{Error, Result};
22
23use graph::DepGraph;
24use project::Project;
25use std::{io::BufWriter, path::Path};
26
27pub fn get_dep_graph(cfg: Config) -> Result<DepGraph> {
31 let manifest_path = &cfg.manifest_path;
33 is_cargo_toml(manifest_path)?;
34 let manifest_path = util::find_file_search_parent_dirs(manifest_path)?;
35
36 let manifest = manifest_path.to_str().unwrap();
38 let lock_file = format!("{}.lock", &manifest[0..manifest.len() - 5]);
39 let lock_path = util::find_file_search_parent_dirs(&lock_file)?;
40
41 let project = Project::with_config(cfg)?;
43 project.graph(manifest_path, lock_path)
44}
45
46pub fn render_dep_graph(graph: DepGraph) -> Result<String> {
48 let bytes: Vec<u8> = Vec::new();
49 let mut writer = BufWriter::new(bytes);
50 graph.render_to(&mut writer)?;
51
52 String::from_utf8(writer.into_inner()?).map_err(|err| Error::Generic(err.to_string()))
53}
54
55fn is_cargo_toml(file_name: &str) -> Result<()> {
57 let path = Path::new(file_name);
58
59 if let Some(file_name) = path.file_name() {
60 if file_name != "Cargo.toml" {
61 return Err(Error::Toml(
62 "The manifest-path must be a path to a Cargo.toml file".into(),
63 ));
64 }
65 } else {
66 return Err(Error::Toml("The manifest path is not a valid file".into()));
67 }
68
69 Ok(())
70}