1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![forbid(unsafe_code)]
#![deny(missing_docs)]
mod config;
mod dep;
mod error;
mod graph;
mod project;
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 mut bytes: Vec<u8> = Vec::new();
let mut writer = BufWriter::new(&mut bytes);
graph.render_to(&mut writer)?;
drop(writer);
String::from_utf8(bytes).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(())
}