1use std::path::Path;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum RunContext {
10 Server,
11 Client,
12 Shared,
13 Unknown,
14}
15
16impl RunContext {
17 #[must_use]
19 pub const fn as_str(self) -> &'static str {
20 match self {
21 Self::Server => "server",
22 Self::Client => "client",
23 Self::Shared => "shared",
24 Self::Unknown => "unknown",
25 }
26 }
27
28 #[must_use]
30 pub fn from_service(name: &str) -> Option<Self> {
31 match name {
32 "ServerScriptService" | "ServerStorage" | "Workspace" => Some(Self::Server),
33 "StarterPlayer"
34 | "StarterCharacterScripts"
35 | "StarterPlayerScripts"
36 | "StarterGui"
37 | "StarterPack" => Some(Self::Client),
38 "ReplicatedStorage" | "ReplicatedFirst" => Some(Self::Shared),
39 _ => None,
40 }
41 }
42
43 #[must_use]
45 pub fn from_file_name(name: &str) -> Option<Self> {
46 let stem = name
47 .strip_suffix(".luau")
48 .or_else(|| name.strip_suffix(".lua"))?;
49 if stem.ends_with(".server") {
50 Some(Self::Server)
51 } else if stem.ends_with(".client") {
52 Some(Self::Client)
53 } else {
54 None
55 }
56 }
57
58 #[must_use]
66 pub fn resolve(relative: &Path, instance_path: &str) -> Self {
67 Self::resolve_in(relative, instance_path, true)
68 }
69
70 #[must_use]
72 pub fn resolve_in(relative: &Path, instance_path: &str, allow_path_guessing: bool) -> Self {
73 let file_name = relative
74 .file_name()
75 .and_then(|name| name.to_str())
76 .unwrap_or_default();
77
78 if let Some(context) = Self::from_file_name(file_name) {
79 return context;
80 }
81
82 if let Some(service) = instance_path.split('/').next()
83 && let Some(context) = Self::from_service(service)
84 {
85 return context;
86 }
87
88 if allow_path_guessing && let Some(context) = Self::from_path(relative) {
89 return context;
90 }
91
92 Self::Unknown
93 }
94
95 fn from_path(path: &Path) -> Option<Self> {
96 let mut components: Vec<&str> = path
97 .components()
98 .filter_map(|component| component.as_os_str().to_str())
99 .collect();
100
101 for component in components.iter().rev() {
102 if let Some(context) = Self::from_service(component) {
103 return Some(context);
104 }
105 }
106
107 for component in components.drain(..).rev() {
108 match component.to_ascii_lowercase().as_str() {
109 "server" | "serverscript" | "serverscripts" | "serverside" => {
110 return Some(Self::Server);
111 }
112 "client" | "clientscript" | "clientscripts" | "clientside" => {
113 return Some(Self::Client);
114 }
115 "shared" | "common" => return Some(Self::Shared),
116 _ => {}
117 }
118 }
119
120 None
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use std::path::Path;
127
128 use super::RunContext;
129
130 #[test]
131 fn suffix_wins_over_service() {
132 let context = RunContext::resolve(
133 Path::new("src/ReplicatedStorage/Foo.client.luau"),
134 "ReplicatedStorage/Foo",
135 );
136 assert_eq!(context, RunContext::Client);
137 }
138
139 #[test]
140 fn service_decides_when_there_is_no_suffix() {
141 let context = RunContext::resolve(
142 Path::new("src/server/Admin.luau"),
143 "ServerScriptService/Admin",
144 );
145 assert_eq!(context, RunContext::Server);
146 }
147
148 #[test]
149 fn shared_container_is_not_a_guess() {
150 let context = RunContext::resolve(
151 Path::new("src/shared/Config.luau"),
152 "ReplicatedStorage/Config",
153 );
154 assert_eq!(context, RunContext::Shared);
155 }
156
157 #[test]
158 fn unknown_stays_unknown() {
159 let context = RunContext::resolve(Path::new("scratch.luau"), "");
160 assert_eq!(context, RunContext::Unknown);
161 }
162
163 #[test]
164 fn a_service_directory_resolves_without_a_sourcemap() {
165 assert_eq!(
166 RunContext::resolve(Path::new("ServerScriptService/Foo.lua"), ""),
167 RunContext::Server
168 );
169 assert_eq!(
170 RunContext::resolve(Path::new("ReplicatedStorage/Foo.lua"), ""),
171 RunContext::Shared
172 );
173 }
174
175 #[test]
176 fn plain_mode_does_not_guess_from_directories() {
177 assert_eq!(
178 RunContext::resolve_in(Path::new("src/server/Foo.luau"), "", false),
179 RunContext::Unknown
180 );
181 assert_eq!(
182 RunContext::resolve_in(Path::new("src/Foo.server.luau"), "", false),
183 RunContext::Server
184 );
185 }
186
187 #[test]
188 fn a_server_or_client_directory_resolves() {
189 assert_eq!(
190 RunContext::resolve(Path::new("src/server/Foo.luau"), ""),
191 RunContext::Server
192 );
193 assert_eq!(
194 RunContext::resolve(Path::new("src/client/Foo.luau"), ""),
195 RunContext::Client
196 );
197 assert_eq!(
198 RunContext::resolve(Path::new("src/shared/Foo.luau"), ""),
199 RunContext::Shared
200 );
201 }
202}