-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.rs
More file actions
331 lines (276 loc) · 10.1 KB
/
run.rs
File metadata and controls
331 lines (276 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use std::fs;
use std::path::Path;
use crate::commands::generate_constraint::parse_domain;
use crate::commands::generate_constraint::validate_name;
use crate::error::{CliError, CliResult};
use crate::output;
use super::generators::{generate_entity, generate_fact, generate_solution};
use super::utils::{ensure_domain_dir, find_file_for_type, snake_to_pascal, validate_score_type};
use super::wiring::{
inject_planning_variable, replace_score_type, update_domain_mod, wire_collection_into_solution,
};
pub fn run_entity(
name: &str,
planning_variable: Option<&str>,
fields: &[String],
force: bool,
pretend: bool,
) -> CliResult {
validate_name(name)?;
if let Some(var) = planning_variable {
validate_name(var)?;
}
let parsed_fields = parse_fields(fields)?;
let domain_dir = Path::new("src/domain");
ensure_domain_dir(domain_dir)?;
let pascal = snake_to_pascal(name);
let file_path = domain_dir.join(format!("{}.rs", name));
if file_path.exists() && !force {
return Err(CliError::ResourceExists {
kind: "entity",
name: name.to_string(),
});
}
let src = generate_entity(&pascal, planning_variable, &parsed_fields);
if pretend {
println!("Would create {}", file_path.display());
println!("Would update src/domain/mod.rs");
println!("Would wire into planning solution");
return Ok(());
}
fs::write(&file_path, &src).map_err(|e| CliError::IoError {
context: format!("failed to write {}", file_path.display()),
source: e,
})?;
update_domain_mod(name, &pascal)?;
wire_collection_into_solution(name, &pascal, "planning_entity_collection")?;
let plural = super::utils::pluralize(name);
crate::commands::sf_config::add_entity(name, &pascal, &plural)?;
output::print_create(file_path.to_str().unwrap());
print_diff_verbose("", &src);
output::print_update("src/domain/mod.rs");
Ok(())
}
pub fn run_fact(name: &str, fields: &[String], force: bool, pretend: bool) -> CliResult {
validate_name(name)?;
let parsed_fields = parse_fields(fields)?;
let domain_dir = Path::new("src/domain");
ensure_domain_dir(domain_dir)?;
let pascal = snake_to_pascal(name);
let file_path = domain_dir.join(format!("{}.rs", name));
if file_path.exists() && !force {
return Err(CliError::ResourceExists {
kind: "fact",
name: name.to_string(),
});
}
let src = generate_fact(&pascal, &parsed_fields);
if pretend {
println!("Would create {}", file_path.display());
println!("Would update src/domain/mod.rs");
println!("Would wire into planning solution");
return Ok(());
}
fs::write(&file_path, &src).map_err(|e| CliError::IoError {
context: format!("failed to write {}", file_path.display()),
source: e,
})?;
update_domain_mod(name, &pascal)?;
wire_collection_into_solution(name, &pascal, "problem_fact_collection")?;
let plural = super::utils::pluralize(name);
crate::commands::sf_config::add_fact(name, &pascal, &plural)?;
output::print_create(file_path.to_str().unwrap());
print_diff_verbose("", &src);
output::print_update("src/domain/mod.rs");
Ok(())
}
// Parse "name:Type" field specifications.
fn parse_fields(fields: &[String]) -> CliResult<Vec<(String, String)>> {
fields
.iter()
.map(|f| {
let mut parts = f.splitn(2, ':');
let field_name = parts.next().unwrap_or("").trim().to_string();
let field_type = parts.next().unwrap_or("").trim().to_string();
if field_name.is_empty() || field_type.is_empty() {
Err(CliError::general(format!(
"invalid --field '{}': expected 'name:Type'",
f
)))
} else {
Ok((field_name, field_type))
}
})
.collect()
}
// Print lines of new_src prefixed with '+' when verbose.
fn print_diff_verbose(before: &str, after: &str) {
if !output::is_verbose() {
return;
}
let before_lines: Vec<&str> = if before.is_empty() {
vec![]
} else {
before.lines().collect()
};
let after_lines: Vec<&str> = after.lines().collect();
for line in &after_lines {
if !before_lines.contains(line) {
println!("+ {}", line);
}
}
}
pub fn run_solution(name: &str, score: &str) -> CliResult {
validate_name(name)?;
validate_score_type(score)?;
let domain_dir = Path::new("src/domain");
ensure_domain_dir(domain_dir)?;
let pascal = snake_to_pascal(name);
// Check if any solution already exists
if let Some(domain) = parse_domain() {
if is_default_scaffold()? {
remove_default_scaffold()?;
} else {
return Err(CliError::with_hint(
format!(
"a planning solution '{}' already exists",
domain.solution_type
),
"use `solverforge destroy solution` then `solverforge generate solution` to replace it",
));
}
}
let file_path = domain_dir.join(format!("{}.rs", name));
if file_path.exists() {
return Err(CliError::ResourceExists {
kind: "solution file",
name: file_path.display().to_string(),
});
}
let src = generate_solution(&pascal, score);
fs::write(&file_path, src).map_err(|e| CliError::IoError {
context: format!("failed to write {}", file_path.display()),
source: e,
})?;
update_domain_mod(name, &pascal)?;
output::print_create(file_path.to_str().unwrap());
output::print_update("src/domain/mod.rs");
Ok(())
}
pub fn run_variable(field: &str, entity: &str) -> CliResult {
validate_name(field)?;
let domain_dir = Path::new("src/domain");
if !domain_dir.exists() {
return Err(CliError::NotInProject {
missing: "src/domain/",
});
}
let entity_file = find_file_for_type(domain_dir, entity)?;
let src = fs::read_to_string(&entity_file).map_err(|e| CliError::IoError {
context: format!("failed to read {}", entity_file.display()),
source: e,
})?;
let new_src = inject_planning_variable(&src, entity, field)?;
fs::write(&entity_file, new_src).map_err(|e| CliError::IoError {
context: format!("failed to write {}", entity_file.display()),
source: e,
})?;
output::print_update(entity_file.to_str().unwrap());
Ok(())
}
pub fn run_score(score_type: &str) -> CliResult {
validate_score_type(score_type)?;
let domain_dir = Path::new("src/domain");
if !domain_dir.exists() {
return Err(CliError::NotInProject {
missing: "src/domain/",
});
}
let domain = parse_domain().ok_or(CliError::NotInProject {
missing: "src/domain/ (no planning solution found)",
})?;
let solution_file = find_file_for_type(domain_dir, &domain.solution_type)?;
let src = fs::read_to_string(&solution_file).map_err(|e| CliError::IoError {
context: format!("failed to read {}", solution_file.display()),
source: e,
})?;
let new_src = replace_score_type(&src, &domain.score_type, score_type)?;
fs::write(&solution_file, new_src).map_err(|e| CliError::IoError {
context: format!("failed to write {}", solution_file.display()),
source: e,
})?;
output::print_update(solution_file.to_str().unwrap());
Ok(())
}
fn is_default_scaffold() -> CliResult<bool> {
let plan_path = Path::new("src/domain/plan.rs");
if !plan_path.exists() {
return Ok(false);
}
let content = fs::read_to_string(plan_path).map_err(|e| CliError::IoError {
context: "failed to read plan.rs".to_string(),
source: e,
})?;
Ok(content.contains("Rename this to something domain-specific"))
}
pub(crate) fn remove_default_scaffold() -> CliResult {
let domain_files = ["plan.rs", "task.rs", "resource.rs"];
for file in &domain_files {
let path = Path::new("src/domain").join(file);
if path.exists() {
fs::remove_file(&path).map_err(|e| CliError::IoError {
context: format!("failed to remove {}", file),
source: e,
})?;
}
}
let domain_mod = Path::new("src/domain/mod.rs");
if domain_mod.exists() {
fs::write(domain_mod, "// Domain module\n").map_err(|e| CliError::IoError {
context: "failed to clear domain/mod.rs".to_string(),
source: e,
})?;
}
let all_assigned = Path::new("src/constraints/all_assigned.rs");
if all_assigned.exists() {
fs::remove_file(all_assigned).map_err(|e| CliError::IoError {
context: "failed to remove all_assigned.rs".to_string(),
source: e,
})?;
}
let constraints_mod = Path::new("src/constraints/mod.rs");
if constraints_mod.exists() {
let content = fs::read_to_string(constraints_mod).map_err(|e| CliError::IoError {
context: "failed to read constraints/mod.rs".to_string(),
source: e,
})?;
let lines: Vec<&str> = content
.lines()
.filter(|line| !line.contains("all_assigned"))
.collect();
let new_content = lines.join("\n");
fs::write(constraints_mod, new_content).map_err(|e| CliError::IoError {
context: "failed to update constraints/mod.rs".to_string(),
source: e,
})?;
}
let data_mod = Path::new("src/data/mod.rs");
if data_mod.exists() {
let stub_content = generate_data_loader_stub();
fs::write(data_mod, stub_content).map_err(|e| CliError::IoError {
context: "failed to stub data/mod.rs".to_string(),
source: e,
})?;
}
output::print_remove("default scaffold");
Ok(())
}
pub(crate) fn generate_data_loader_stub() -> &'static str {
r#"/* Data loading module.
Replace `load()` with code that reads your real inputs and constructs the
domain objects your API or solver layer needs. */
pub fn load() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
"#
}