Skip to content

Commit 26e9c3b

Browse files
committed
Make Project constructor more expressive
Signed-off-by: Patrick Luca Fazzi <patrick91@live.it>
1 parent cffbdc0 commit 26e9c3b

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rust_arkitect::dsl::{ArchitecturalRules, Arkitect, Project};
2525

2626
#[test]
2727
fn test_architectural_rules() {
28-
let project = Project::new();
28+
let project = Project::from_current_crate();
2929

3030
#[rustfmt::skip]
3131
let rules = ArchitecturalRules::define()
@@ -73,7 +73,7 @@ You can define and test architectural rules:
7373
```rust
7474
#[test]
7575
fn test_architecture_baseline() {
76-
let project = Project::new();
76+
let project = Project::from_current_workspace();
7777

7878
#[rustfmt::skip]
7979
let rules = ArchitecturalRules::define()

examples/sample_project/src/architecture_rules_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn test_mvc_architecture_rules() {
5454
fn test_three_tier_architecture() {
5555
Arkitect::init_logger();
5656

57-
let project = Project::from_relative_path(file!(), "./../");
57+
let project = Project::from_current_crate();
5858

5959
#[rustfmt::skip]
6060
let rules = ArchitecturalRules::define()

examples/workspace_project/tests/src/architecture_rules_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn test_vertical_slices_architecture_rules() {
2222

2323
.build();
2424

25-
let project = Project::new();
25+
let project = Project::from_current_workspace();
2626

2727
let result = Arkitect::ensure_that(project).complies_with(rules);
2828

@@ -33,7 +33,7 @@ fn test_vertical_slices_architecture_rules() {
3333
fn test_mvc_architecture_rules() {
3434
Arkitect::init_logger();
3535

36-
let project = Project::new();
36+
let project = Project::from_current_workspace();
3737

3838
#[rustfmt::skip]
3939
let rules = ArchitecturalRules::define()
@@ -57,7 +57,7 @@ fn test_mvc_architecture_rules() {
5757
fn test_three_tier_architecture() {
5858
Arkitect::init_logger();
5959

60-
let project = Project::new();
60+
let project = Project::from_current_workspace();
6161

6262
#[rustfmt::skip]
6363
let rules = ArchitecturalRules::define()

src/dsl/project.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ impl Project {
1212
}
1313
}
1414

15-
pub fn new() -> Project {
15+
/// Creates a Project rooted at the crate's directory.
16+
pub fn from_current_crate() -> Project {
17+
let cargo_manifest_dir =
18+
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set");
19+
20+
Project {
21+
project_root: cargo_manifest_dir,
22+
}
23+
}
24+
25+
/// Creates a Project rooted at the workspace's root directory.
26+
/// Panics if the current crate is not part of a workspace.
27+
pub fn from_current_workspace() -> Project {
1628
let cargo_manifest_dir =
1729
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set");
1830

@@ -32,11 +44,30 @@ impl Project {
3244
}
3345
}
3446

35-
Project {
36-
project_root: cargo_manifest_dir,
47+
panic!("Current crate is not part of a workspace or workspace root not found.");
48+
}
49+
50+
/// Method that creates a Project determining whether the current context is a workspace or crate.
51+
pub fn new() -> Project {
52+
let cargo_manifest_dir =
53+
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set");
54+
55+
let crate_path = Path::new(&cargo_manifest_dir);
56+
57+
if Self::is_workspace_root(crate_path) {
58+
return Self::from_current_workspace();
3759
}
60+
61+
if let Some(parent_path) = crate_path.parent() {
62+
if Self::is_workspace_root(parent_path) {
63+
return Self::from_current_workspace();
64+
}
65+
}
66+
67+
Self::from_current_crate()
3868
}
3969

70+
/// Checks if the given path is a workspace root by inspecting its `Cargo.toml`.
4071
fn is_workspace_root(path: &Path) -> bool {
4172
let cargo_toml = path.join("Cargo.toml");
4273
if !cargo_toml.exists() {

0 commit comments

Comments
 (0)