cargo_deps/
lib.rs

1//! This library is the backend for the `cargo-deps` command-line program, containing the actual
2//! logic.
3//!
4//! This library provides the following functionality:
5//!
6//! + Getting the dependency graph of a crate in its full intermediate representation.
7//! + Getting the final graphviz representation of a crate's dependencies.
8
9#![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
27/// Gets the full representation of the dependency graph, without converting it to graphviz output.
28///
29/// Pass the result of this function to `render_dep_graph` for the graphviz string.
30pub fn get_dep_graph(cfg: Config) -> Result<DepGraph> {
31    // Search through parent dirs for Cargo.toml.
32    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    // Cargo.lock must be in the same directory as Cargo.toml or in a parent directory.
37    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    // Graph the project.
42    let project = Project::with_config(cfg)?;
43    project.graph(manifest_path, lock_path)
44}
45
46/// Converts the dependency graph representation into a graphviz string.
47pub 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
55// Check that the manifest file name is "Cargo.toml".
56fn 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}