1use std::collections::BTreeMap;
2use std::path::{Path, PathBuf};
3
4use serde::{Deserialize, Deserializer, Serialize};
5
6use crate::Error;
7
8#[derive(Debug, Clone, Default, Deserialize)]
10#[serde(default)]
11pub struct Config {
12 pub project: ProjectConfig,
13 pub run: RunConfig,
14 pub custom: CustomConfig,
16 pub rules: BTreeMap<String, String>,
18 pub rule: RuleOptions,
20}
21
22#[derive(Debug, Clone, Default, Deserialize)]
24#[serde(default)]
25pub struct RuleOptions {
26 #[serde(rename = "GB117", alias = "gb117")]
27 pub gb117: Gb117Options,
28}
29
30#[derive(Debug, Clone, Deserialize)]
32#[serde(default)]
33pub struct Gb117Options {
34 pub remotes: Vec<String>,
36 pub sanitizers: Vec<String>,
38}
39
40impl Default for Gb117Options {
41 fn default() -> Self {
42 Self {
43 remotes: ["SmeersLightsHandler", "Horn", "Smoke_FE"]
44 .into_iter()
45 .map(str::to_owned)
46 .collect(),
47 sanitizers: ["owner", "vehicletokens", "isdescendantof"]
48 .into_iter()
49 .map(str::to_owned)
50 .collect(),
51 }
52 }
53}
54
55#[derive(Debug, Clone, Deserialize)]
57#[serde(default)]
58pub struct CustomConfig {
59 pub dir: String,
61 pub runtime: String,
63 pub timeout: u64,
65}
66
67impl Default for CustomConfig {
68 fn default() -> Self {
69 Self {
70 dir: ".plut/rules".to_owned(),
71 runtime: "luneblox".to_owned(),
72 timeout: 60,
73 }
74 }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
79#[serde(rename_all = "lowercase")]
80pub enum Model {
81 #[default]
82 Auto,
83 Rojo,
84 Plain,
85}
86
87impl Model {
88 #[must_use]
90 pub const fn as_str(self) -> &'static str {
91 match self {
92 Self::Auto => "auto",
93 Self::Rojo => "rojo",
94 Self::Plain => "plain",
95 }
96 }
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
101#[serde(rename_all = "lowercase")]
102pub enum Target {
103 Roblox,
104 Lute,
105 Lune,
106 Bare,
107}
108
109impl Target {
110 #[must_use]
112 pub const fn as_str(self) -> &'static str {
113 match self {
114 Self::Roblox => "roblox",
115 Self::Lute => "lute",
116 Self::Lune => "lune",
117 Self::Bare => "bare",
118 }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Default)]
124pub enum TargetsConfig {
125 #[default]
126 Auto,
127 List(Vec<Target>),
128}
129
130impl<'de> Deserialize<'de> for TargetsConfig {
131 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
132 where
133 D: Deserializer<'de>,
134 {
135 #[derive(Deserialize)]
136 #[serde(untagged)]
137 enum Raw {
138 Auto(String),
139 List(Vec<Target>),
140 }
141
142 match Raw::deserialize(deserializer)? {
143 Raw::Auto(value) if value.eq_ignore_ascii_case("auto") => Ok(Self::Auto),
144 Raw::Auto(value) => Err(serde::de::Error::custom(format!(
145 "targets must be \"auto\" or a list, got \"{value}\""
146 ))),
147 Raw::List(targets) => Ok(Self::List(targets)),
148 }
149 }
150}
151
152impl TargetsConfig {
153 #[must_use]
156 pub fn resolve(&self, model_plain: bool) -> Vec<Target> {
157 match self {
158 Self::Auto if model_plain => vec![Target::Bare],
159 Self::Auto => vec![Target::Roblox],
160 Self::List(targets) => targets.clone(),
161 }
162 }
163}
164
165#[derive(Debug, Clone, Deserialize)]
167#[serde(default)]
168pub struct ProjectConfig {
169 pub sourcemap: String,
172 pub model: Model,
174 pub targets: TargetsConfig,
176}
177
178impl Default for ProjectConfig {
179 fn default() -> Self {
180 Self {
181 sourcemap: "auto".to_owned(),
182 model: Model::Auto,
183 targets: TargetsConfig::Auto,
184 }
185 }
186}
187
188#[derive(Debug, Clone, Deserialize)]
190#[serde(default)]
191pub struct RunConfig {
192 pub exclude: Vec<String>,
194}
195
196impl Default for RunConfig {
197 fn default() -> Self {
198 Self {
199 exclude: vec![
200 "**/roblox_packages/**".to_owned(),
203 "**/roblox_server_packages/**".to_owned(),
204 "**/Packages/**".to_owned(),
205 "**/node_modules/**".to_owned(),
206 "**/vendor/**".to_owned(),
207 "**/test/**".to_owned(),
211 "**/tests/**".to_owned(),
212 "**/spec/**".to_owned(),
213 "**/*.spec.luau".to_owned(),
214 "**/*.test.luau".to_owned(),
215 "**/benchmark/**".to_owned(),
217 "**/benchmarks/**".to_owned(),
218 "**/example/**".to_owned(),
219 "**/examples/**".to_owned(),
220 "**/*.d.luau".to_owned(),
222 ],
223 }
224 }
225}
226
227impl Config {
228 pub fn load(explicit: Option<&Path>, root: &Path) -> Result<Self, Error> {
232 let path: PathBuf = explicit.map_or_else(|| root.join("plut.toml"), Path::to_path_buf);
233
234 if !path.is_file() {
235 return Ok(Self::default());
236 }
237
238 let text = std::fs::read_to_string(&path).map_err(|source| Error::Read {
239 path: path.clone(),
240 source,
241 })?;
242
243 toml::from_str(&text).map_err(|source| Error::ParseToml { path, source })
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use super::Config;
250
251 #[test]
252 fn model_and_targets_default_to_auto() {
253 let config: Config = toml::from_str("").expect("empty config");
254 assert_eq!(config.project.model, super::Model::Auto);
255 assert_eq!(config.project.targets, super::TargetsConfig::Auto);
256 assert_eq!(
257 config.project.targets.resolve(false),
258 vec![super::Target::Roblox]
259 );
260 assert_eq!(
261 config.project.targets.resolve(true),
262 vec![super::Target::Bare]
263 );
264 }
265
266 #[test]
267 fn followers_read_model_and_targets() {
268 let config: Config =
269 toml::from_str("[project]\nmodel = \"plain\"\ntargets = [\"lune\", \"bare\"]\n")
270 .expect("config");
271 assert_eq!(config.project.model, super::Model::Plain);
272 assert_eq!(
273 config.project.targets,
274 super::TargetsConfig::List(vec![super::Target::Lune, super::Target::Bare])
275 );
276 }
277
278 #[test]
279 fn custom_rules_have_defaults() {
280 let config: Config = toml::from_str("").expect("empty config");
281 assert_eq!(config.custom.dir, ".plut/rules");
282 assert_eq!(config.custom.runtime, "luneblox");
283 assert_eq!(config.custom.timeout, 60);
284 }
285
286 #[test]
287 fn gb117_has_default_remotes_and_sanitizers() {
288 let config: Config = toml::from_str("").expect("empty config");
289 assert_eq!(
290 config.rule.gb117.remotes,
291 ["SmeersLightsHandler", "Horn", "Smoke_FE"]
292 );
293 assert!(config.rule.gb117.sanitizers.contains(&"owner".to_owned()));
294 }
295
296 #[test]
297 fn gb117_reads_its_section() {
298 let config: Config = toml::from_str(
299 "[rule.GB117]\n\
300 remotes = [\"MyChassisRemote\"]\n\
301 sanitizers = [\"driver\"]\n",
302 )
303 .expect("config");
304 assert_eq!(config.rule.gb117.remotes, ["MyChassisRemote"]);
305 assert_eq!(config.rule.gb117.sanitizers, ["driver"]);
306 }
307
308 #[test]
309 fn custom_rules_read_their_section() {
310 let config: Config =
311 toml::from_str("[custom]\ndir = \"rules\"\nruntime = \"lune\"\ntimeout = 5\n")
312 .expect("config");
313 assert_eq!(config.custom.dir, "rules");
314 assert_eq!(config.custom.runtime, "lune");
315 assert_eq!(config.custom.timeout, 5);
316 }
317}