1use std::{env, path::Path, process::ExitCode};
2
3pub mod dependency_graph;
4pub mod dependency_rule;
5pub mod metadata;
6
7#[derive(Debug, Clone)]
8pub enum ReturnStatus {
9 NoViolation,
10 Violation,
11}
12impl ReturnStatus {
13 pub fn to_return_code(&self) -> ExitCode {
14 match self {
15 ReturnStatus::NoViolation => ExitCode::SUCCESS,
16 ReturnStatus::Violation => ExitCode::FAILURE,
17 }
18 }
19}
20
21pub fn handler(
22 graph_build_configs: dependency_graph::DependencyGraphBuildConfigs,
23 metadata_configs: metadata::CollectMetadataConfig,
24) -> anyhow::Result<ReturnStatus> {
25 let metadata = metadata::collect_metadata(metadata_configs.clone())?;
26 let graph = dependency_graph::build_dependency_graph(metadata, graph_build_configs)?;
27
28 if let Some(manifest_path) = metadata_configs.manifest_path {
29 let manifest_path = Path::new(&manifest_path);
30 let rules = dependency_rule::DependencyRule::from_file(
31 manifest_path
32 .parent()
33 .unwrap()
34 .join(Path::new("dependency_rules.toml")),
35 )?;
36
37 dependency_graph::tree::print(&graph, manifest_path, rules)
38 } else {
39 let current_dir = env::current_dir()?;
40 let manifest_path = current_dir.join(Path::new("Cargo.toml"));
41 let rules = dependency_rule::DependencyRule::from_file(
42 manifest_path
43 .parent()
44 .unwrap()
45 .join(Path::new("dependency_rules.toml")),
46 )?;
47
48 dependency_graph::tree::print(&graph, manifest_path, rules)
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55 use crate::{dependency_graph::DependencyGraphBuildConfigs, metadata::CollectMetadataConfig};
56 use anyhow::Result;
57
58 #[test]
59 #[ignore]
60 fn test_main() -> Result<()> {
61 let build_config = DependencyGraphBuildConfigs::default();
62 let collect_metadata_config = CollectMetadataConfig {
63 manifest_path: Some("tests/demo_crates/clean-arch/Cargo.toml".to_string()),
64 ..CollectMetadataConfig::default()
65 };
66
67 let _ = handler(build_config, collect_metadata_config)?;
68
69 Ok(())
70 }
71
72 #[test]
73 fn test_handler_success() -> Result<()> {
74 let expected_return_code = ExitCode::SUCCESS;
75 let build_config = DependencyGraphBuildConfigs::default();
76 let collect_metadata_config = CollectMetadataConfig {
77 manifest_path: Some("tests/demo_crates/clean-arch/Cargo.toml".to_string()),
78 ..CollectMetadataConfig::default()
79 };
80
81 let result = handler(build_config, collect_metadata_config)?;
82 assert_eq!(result.to_return_code(), expected_return_code);
83
84 Ok(())
85 }
86
87 #[test]
88 fn test_handler_failure() -> Result<()> {
89 let expected_return_code = ExitCode::FAILURE;
90 let build_config = DependencyGraphBuildConfigs::default();
91 let collect_metadata_config = CollectMetadataConfig {
92 manifest_path: Some("tests/demo_crates/tangled-clean-arch/Cargo.toml".to_string()),
93 ..CollectMetadataConfig::default()
94 };
95
96 let result = handler(build_config, collect_metadata_config)?;
97 assert_eq!(result.to_return_code(), expected_return_code);
98
99 Ok(())
100 }
101}