luaur_cli_lib/functions/
get_real_path.rs1use crate::enums::navigation_status::NavigationStatus;
2use crate::functions::is_directory::is_directory;
3use crate::functions::is_file::is_file;
4use crate::records::resolved_real_path::ResolvedRealPath;
5use luaur_common::macros::luau_assert::LUAU_ASSERT;
6
7const K_INIT_SUFFIXES: &[&str] = &["/init.luau", "/init.lua"];
8const K_SUFFIXES: &[&str] = &[".luau", ".lua"];
9
10pub fn get_real_path(module_path: alloc::string::String) -> ResolvedRealPath {
11 let mut found = false;
12 let mut suffix = "";
13
14 let last_slash = module_path.rfind('/');
15 LUAU_ASSERT!(last_slash.is_some());
16
17 let last_component = if let Some(idx) = last_slash {
18 &module_path[idx + 1..]
19 } else {
20 ""
21 };
22
23 if last_component != "init" {
24 for &potential_suffix in K_SUFFIXES {
25 let mut path_with_suffix = module_path.clone();
26 path_with_suffix.push_str(potential_suffix);
27 if is_file(&path_with_suffix) {
28 if found {
29 return ResolvedRealPath::new(
30 NavigationStatus::Ambiguous,
31 alloc::string::String::new(),
32 );
33 }
34
35 suffix = potential_suffix;
36 found = true;
37 }
38 }
39 }
40
41 if is_directory(&module_path) {
42 if found {
43 return ResolvedRealPath::new(
44 NavigationStatus::Ambiguous,
45 alloc::string::String::new(),
46 );
47 }
48
49 for &potential_suffix in K_INIT_SUFFIXES {
50 let mut path_with_suffix = module_path.clone();
51 path_with_suffix.push_str(potential_suffix);
52 if is_file(&path_with_suffix) {
53 if found {
54 return ResolvedRealPath::new(
55 NavigationStatus::Ambiguous,
56 alloc::string::String::new(),
57 );
58 }
59
60 suffix = potential_suffix;
61 found = true;
62 }
63 }
64
65 found = true;
66 }
67
68 if !found {
69 return ResolvedRealPath::new(NavigationStatus::NotFound, alloc::string::String::new());
70 }
71
72 let mut result_path = module_path;
73 result_path.push_str(suffix);
74 ResolvedRealPath::new(NavigationStatus::Success, result_path)
75}