Skip to main content

luaur_cli_lib/methods/
vfs_navigator_reset_to_path.rs

1use crate::enums::navigation_status::NavigationStatus;
2use crate::functions::get_current_working_directory::get_current_working_directory;
3use crate::functions::get_module_path::get_module_path;
4use crate::functions::is_absolute_path::is_absolute_path;
5use crate::functions::normalize_path::normalize_path;
6use crate::records::vfs_navigator::VfsNavigator;
7use luaur_common::macros::luau_assert::LUAU_ASSERT;
8
9impl VfsNavigator {
10    pub fn reset_to_path(&mut self, path: &str) -> NavigationStatus {
11        let normalized_path = normalize_path(path);
12
13        if is_absolute_path(&normalized_path) {
14            self.module_path = get_module_path(&normalized_path);
15            self.absolute_module_path = self.module_path.clone();
16
17            let first_slash = normalized_path.find('/');
18            LUAU_ASSERT!(first_slash.is_some());
19            self.absolute_path_prefix = normalized_path
20                .get(..first_slash.unwrap())
21                .unwrap_or("")
22                .to_string();
23        } else {
24            let cwd = get_current_working_directory();
25            if cwd.is_none() {
26                return NavigationStatus::NotFound;
27            }
28
29            self.module_path = get_module_path(&normalized_path);
30            let joined_path = normalize_path(&format!("{}/{}", cwd.unwrap(), normalized_path));
31            self.absolute_module_path = get_module_path(&joined_path);
32
33            let first_slash = joined_path.find('/');
34            LUAU_ASSERT!(first_slash.is_some());
35            self.absolute_path_prefix = joined_path
36                .get(..first_slash.unwrap())
37                .unwrap_or("")
38                .to_string();
39        }
40
41        self.update_real_paths()
42    }
43}