1use crate::pas_content_root::ContentRoot;
2use crate::psa_facet::Facet;
3use crate::psa_library::Library;
4
5pub struct Module {
6 pub name: String,
7 pub path: String,
8 pub facets: Vec<Facet>,
9 pub libraries: Vec<Library>,
10 pub sub_modules: Vec<Module>,
11 pub content_root: ContentRoot,
12}
13
14impl Module {
15 pub fn add_facet(&mut self, facet: Facet) {
16 self.facets.push(facet);
17 }
18
19 pub fn add_library(&mut self, lib: Library) {
20 self.libraries.push(lib);
21 }
22
23 pub fn add_sub_module(&mut self, sub_module: Module) {
24 self.sub_modules.push(sub_module);
25 }
26
27 pub fn add_sub_modules(&mut self, sub_modules: &mut Vec<Module>) {
28 self.sub_modules.append(sub_modules);
29 }
30
31 pub fn add_source_root(&mut self, source_root: String) {
32 self.content_root.add_source_root(source_root.as_str());
33 }
34
35 pub fn add_resource_root(&mut self, resource_root: String) {
36 self.content_root.add_resource_root(resource_root.as_str());
37 }
38
39 pub fn add_test_source_root(&mut self, test_source_root: String) {
40 self.content_root
41 .add_test_source_root(test_source_root.as_str());
42 }
43
44 pub(crate) fn add_test_resource_root(&mut self, test_resource_root: String) {
45 self.content_root
46 .add_test_resource_root(test_resource_root.as_str());
47 }
48
49 pub fn new(name: &str, path: &str) -> Self {
50 Module {
51 name: name.to_string(),
52 path: path.to_string(),
53 facets: vec![],
54 libraries: vec![],
55 sub_modules: vec![],
56 content_root: ContentRoot::default(),
57 }
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use crate::{Facet, Library, LibraryScope, Module};
64
65 #[test]
66 fn should_create_module() {
67 let module = Module::new("foo", "test/path");
68
69 assert_eq!(module.name, "foo".to_string());
70 assert_eq!(module.path, "test/path".to_string());
71 }
72
73 #[test]
74 fn should_add_facet() {
75 let mut module = Module::new("foo", "test/path");
76
77 module.add_facet(Facet {
78 name: "Java".to_string(),
79 });
80
81 assert_eq!(module.facets.len(), 1);
82 }
83
84 #[test]
85 fn should_add_library() {
86 let mut module = Module::new("foo", "test/path");
87
88 module.add_library(Library {
89 group: "org.springframework.boot".to_string(),
90 name: "spring-boot-starter-web".to_string(),
91 version: "1.0.0-RELEASE".to_string(),
92 scope: LibraryScope::Compile,
93 });
94
95 let lib = module.libraries.get(0).unwrap();
96 assert_eq!(module.libraries.len(), 1);
97 assert_eq!(lib.name, "spring-boot-starter-web");
98 }
99
100 #[test]
101 fn should_add_sub_module() {
102 let mut module = Module::new("parent", "test/path");
103
104 module.add_sub_module(Module::new("child", "test/child/path"));
105
106 let sub_module = module.sub_modules.get(0).unwrap();
107 assert_eq!(module.sub_modules.len(), 1);
108 assert_eq!(sub_module.name, "child")
109 }
110}