roan_engine/module/loaders/ident.rs
1use std::path::PathBuf;
2
3#[derive(Debug)]
4pub struct ModuleIdentifier {
5 pub main_name: String,
6 pub rest: Option<Vec<String>>,
7}
8
9impl ModuleIdentifier {
10 /// File name is always the last part of the identifier. If only one part is present file name defaults to `lib.roan`
11 pub fn file_name(&self) -> String {
12 match &self.rest {
13 Some(rest) => rest.join(std::path::MAIN_SEPARATOR_STR) + ".roan",
14 None => "lib.roan".to_string(),
15 }
16 }
17
18 /// Checks if spec is a module identifier and parses it. Module identifier consists of a main name and a sub name separated by a `::`.
19 ///
20 /// "std::io" -> build/deps/std/io.roan
21 ///
22 /// "std::io::file" -> build/deps/std/io/file.roan
23 ///
24 /// "some_dep" -> build/deps/some_dep/lib.roan
25 ///
26 /// "std" -> build/deps/std/lib.roan
27 ///
28 /// # Arguments
29 ///
30 /// * `spec` - A string slice that represents the specification of the path to check.
31 ///
32 /// # Returns
33 ///
34 /// A boolean value indicating whether the spec is a module identifier.
35 pub fn parse_module_identifier(spec: &str) -> Option<ModuleIdentifier> {
36 if !spec.contains("::") {
37 return None;
38 }
39
40 let parts: Vec<&str> = spec.split("::").collect();
41
42 if parts.len() >= 2 {
43 Some(ModuleIdentifier {
44 main_name: parts[0].to_string(),
45 rest: Some(parts[1..].iter().map(|s| s.to_string()).collect()),
46 })
47 } else {
48 None
49 }
50 }
51}