solverforge-cli 1.1.3

CLI for scaffolding and managing SolverForge constraint solver projects
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::fs;
use std::path::Path;

use super::utils::{find_file_for_type, pluralize};
use crate::commands::generate_constraint::parse_domain;
use crate::output;

/// Adds `mod <name>; pub use <name>::<Pascal>;` to `src/domain/mod.rs`.
pub(crate) fn update_domain_mod(mod_name: &str, pascal: &str) -> Result<(), String> {
    let mod_path = Path::new("src/domain/mod.rs");
    if !mod_path.exists() {
        return Ok(()); // nothing to wire
    }

    let src = fs::read_to_string(mod_path)
        .map_err(|e| format!("failed to read src/domain/mod.rs: {}", e))?;

    let mod_line = format!("mod {};", mod_name);
    let use_line = format!("pub use {}::{};", mod_name, pascal);

    if src.contains(&mod_line) {
        return Ok(()); // already present
    }

    let new_src = format!("{}\n{}\n{}\n", src.trim_end(), mod_line, use_line);
    fs::write(mod_path, new_src).map_err(|e| format!("failed to write src/domain/mod.rs: {}", e))
}

/// Inserts a `#[annotation] pub <plural>: Vec<Type>` field into the solution struct,
/// adds a `use super::Type;` import, and updates the `new()` constructor.
pub(crate) fn wire_collection_into_solution(
    name: &str,
    pascal: &str,
    annotation: &str,
) -> Result<(), String> {
    let domain = parse_domain();
    let solution_type = match &domain {
        Some(d) => d.solution_type.clone(),
        None => return Ok(()), // no solution yet, nothing to wire
    };

    let domain_dir = Path::new("src/domain");
    let solution_file = match find_file_for_type(domain_dir, &solution_type) {
        Ok(f) => f,
        Err(_) => return Ok(()),
    };

    let src = fs::read_to_string(&solution_file)
        .map_err(|e| format!("failed to read {}: {}", solution_file.display(), e))?;

    let plural = pluralize(name);
    let field_block = format!(
        "    #[{}]\n    pub {}: Vec<{}>,",
        annotation, plural, pascal
    );

    // Skip if already wired
    if src.contains(&format!("pub {}: Vec<{}>", plural, pascal)) {
        return Ok(());
    }

    let new_src = insert_field_and_import(&src, &solution_type, pascal, &plural, &field_block)?;
    fs::write(&solution_file, new_src)
        .map_err(|e| format!("failed to write {}: {}", solution_file.display(), e))?;

    output::print_update(solution_file.to_str().unwrap());
    Ok(())
}

/// Inserts the field block before the `#[planning_score]` field (or before the closing `}`),
/// adds the import, and patches the `new()` constructor.
pub(crate) fn insert_field_and_import(
    src: &str,
    solution_type: &str,
    pascal: &str,
    _plural: &str,
    field_block: &str,
) -> Result<String, String> {
    // 1. Add import after last `use` line (or at top of file)
    let src = add_import(src, &format!("use super::{};", pascal));

    // 2. Insert field into struct before `#[planning_score]` or before last field's `}`
    let src = insert_struct_field(&src, solution_type, field_block)?;

    rebuild_solution_constructor(&src)
}

fn rebuild_solution_constructor(src: &str) -> Result<String, String> {
    let collection_fields: Vec<(String, String)> =
        src.lines().filter_map(parse_solution_vec_field).collect();
    let new_signature = if collection_fields.is_empty() {
        "    pub fn new() -> Self {".to_string()
    } else {
        format!(
            "    pub fn new({}) -> Self {{",
            collection_fields
                .iter()
                .map(|(field, ty)| format!("{field}: Vec<{ty}>"))
                .collect::<Vec<_>>()
                .join(", ")
        )
    };
    let new_body = if collection_fields.is_empty() {
        "        Self { score: None }".to_string()
    } else {
        format!(
            "        Self {{ {}, score: None }}",
            collection_fields
                .iter()
                .map(|(field, _)| format!("{field}: {field}"))
                .collect::<Vec<_>>()
                .join(", ")
        )
    };

    let lines: Vec<String> = src.lines().map(|line| line.to_string()).collect();
    let fn_start = lines
        .iter()
        .position(|line| line.contains("pub fn new("))
        .ok_or_else(|| "could not find solution constructor".to_string())?;
    let mut fn_end = None;
    let mut depth = 0i32;
    for (idx, line) in lines.iter().enumerate().skip(fn_start) {
        depth += line.chars().filter(|&c| c == '{').count() as i32;
        depth -= line.chars().filter(|&c| c == '}').count() as i32;
        if idx > fn_start && depth == 0 {
            fn_end = Some(idx);
            break;
        }
    }
    let fn_end = fn_end.ok_or_else(|| "could not find constructor end".to_string())?;

    let mut out = Vec::new();
    out.extend(lines[..fn_start].iter().cloned());
    out.push(new_signature);
    out.push(new_body);
    out.push("    }".to_string());
    out.extend(lines[fn_end + 1..].iter().cloned());
    Ok(out.join("\n") + "\n")
}

fn parse_solution_vec_field(line: &str) -> Option<(String, String)> {
    let trimmed = line.trim();
    if !trimmed.starts_with("pub ") || trimmed.contains("score:") {
        return None;
    }
    let trimmed = trimmed.trim_start_matches("pub ").trim_end_matches(',');
    let colon = trimmed.find(':')?;
    let field = trimmed[..colon].trim().to_string();
    let type_part = trimmed[colon + 1..].trim();
    if type_part.starts_with("Vec<") && type_part.ends_with('>') {
        Some((field, type_part[4..type_part.len() - 1].to_string()))
    } else {
        None
    }
}

pub(crate) fn add_import(src: &str, import: &str) -> String {
    if src.contains(import) {
        return src.to_string();
    }
    // Insert after the last `use ` line
    let mut lines: Vec<&str> = src.lines().collect();
    let last_use = lines
        .iter()
        .rposition(|l| l.trim_start().starts_with("use "));
    let insert_at = last_use.map(|i| i + 1).unwrap_or(0);
    lines.insert(insert_at, import);
    lines.join("\n") + "\n"
}

pub(crate) fn insert_struct_field(
    src: &str,
    _solution_type: &str,
    field_block: &str,
) -> Result<String, String> {
    // Insert before `#[planning_score]` if it exists, else before the last `}` of a struct block
    if let Some(pos) = src.find("    #[planning_score]") {
        let mut result = src.to_string();
        result.insert_str(pos, &format!("{}\n", field_block));
        return Ok(result);
    }

    // Find the closing `}` of the struct definition by locating `pub struct` and tracking brace depth
    let lines: Vec<&str> = src.lines().collect();
    let struct_line = lines.iter().position(|l| l.contains("pub struct "));
    if let Some(start) = struct_line {
        let mut depth = 0;
        let mut struct_close = None;
        for (i, line) in lines.iter().enumerate().skip(start) {
            for ch in line.chars() {
                match ch {
                    '{' => depth += 1,
                    '}' => {
                        depth -= 1;
                        if depth == 0 {
                            struct_close = Some(i);
                            break;
                        }
                    }
                    _ => {}
                }
            }
            if struct_close.is_some() {
                break;
            }
        }
        if let Some(i) = struct_close {
            let mut result_lines = lines.to_vec();
            let field_lines: Vec<&str> = field_block.lines().collect();
            for (j, fl) in field_lines.iter().enumerate() {
                result_lines.insert(i + j, fl);
            }
            return Ok(result_lines.join("\n") + "\n");
        }
    }

    Err("could not find insertion point in solution struct".to_string())
}

#[cfg(test)]
/// Adds a `#[planning_variable]` field to an existing entity struct and patches `new()`.
pub(crate) fn inject_planning_variable(
    src: &str,
    entity: &str,
    field: &str,
) -> Result<String, String> {
    inject_standard_variable(src, entity, field, "", true)
}

pub(crate) fn inject_standard_variable(
    src: &str,
    entity: &str,
    field: &str,
    range: &str,
    allows_unassigned: bool,
) -> Result<String, String> {
    let annotation = if range.is_empty() {
        format!(
            "    #[planning_variable(allows_unassigned = {})]",
            allows_unassigned
        )
    } else {
        format!(
            "    #[planning_variable(value_range = \"{}\", allows_unassigned = {})]",
            range, allows_unassigned
        )
    };
    let field_block = format!("{}\n    pub {}: Option<usize>,", annotation, field);
    if src.contains(&format!("pub {}: Option<usize>", field)) {
        return Err(format!("field '{}' already exists in {}", field, entity));
    }

    // Insert before closing `}` of the struct
    let src = insert_struct_field(src, entity, &field_block)?;

    // Patch new(): add `, <field>: None` to Self { ... }
    // Actually patch Self init with `field: None`
    let field_none = format!("{}: None,", field);
    let src = if src.contains(&field_none) {
        src
    } else {
        add_self_none_init(&src, field)
    };

    Ok(src)
}

pub(crate) fn add_self_none_init(src: &str, field: &str) -> String {
    let field_init = format!("{}: None,", field);
    if src.contains(&field_init) {
        return src.to_string();
    }

    // Find `Self {` that is a struct literal (has content after `{` on the same line),
    // not a block opener like `-> Self {` (where `{` is at end of line).
    let self_pos = src
        .match_indices("Self {")
        .find(|(pos, _)| {
            let after_brace = &src[pos + "Self {".len()..];
            // It's a struct literal if the char immediately after `{` is not `\n` or `\r`
            after_brace
                .chars()
                .next()
                .map(|c| c != '\n' && c != '\r')
                .unwrap_or(false)
        })
        .map(|(pos, _)| pos);

    if let Some(self_pos) = self_pos {
        let after_self = &src[self_pos..];
        let mut depth = 0;
        let mut close_pos = None;
        for (i, ch) in after_self.char_indices() {
            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        close_pos = Some(self_pos + i);
                        break;
                    }
                }
                _ => {}
            }
        }
        if let Some(close) = close_pos {
            let indent = "            ";
            // Check if we need to add a comma to the previous field
            let before_close = &src[..close];
            let trimmed = before_close.trim_end();
            let needs_comma = !trimmed.ends_with(',') && !trimmed.ends_with('{');

            if needs_comma {
                // Find where to add the comma (right before any whitespace at the end)
                let content_end = before_close.trim_end().len();
                let with_comma = format!("{},", &src[..content_end]);
                return format!(
                    "{}\n{}{}: None,\n{}",
                    with_comma,
                    indent,
                    field,
                    &src[close..]
                );
            } else {
                return format!(
                    "{}{}{}: None,\n{}",
                    &src[..close],
                    indent,
                    field,
                    &src[close..]
                );
            }
        }
    }
    src.to_string()
}

pub(crate) fn inject_list_variable(
    src: &str,
    entity: &str,
    field: &str,
    elements: &str,
) -> Result<String, String> {
    let field_block = format!(
        "    #[planning_list_variable(element_collection = \"{}\")]\n    pub {}: Vec<usize>,",
        elements, field
    );
    if src.contains(&format!("pub {}: Vec<usize>", field)) {
        return Err(format!("field '{}' already exists in {}", field, entity));
    }

    let src = insert_struct_field(src, entity, &field_block)?;
    let field_init = format!("{}: Vec::new(),", field);
    let src = if src.contains(&field_init) {
        src
    } else {
        add_self_vec_init(&src, field)
    };

    Ok(src)
}

pub(crate) fn remove_variable_field(src: &str, field: &str) -> Result<String, String> {
    if !src.contains(&format!("pub {}", field)) {
        return Err(format!("field '{}' not found", field));
    }

    let mut lines: Vec<String> = src.lines().map(|line| line.to_string()).collect();
    let mut i = 0;
    while i < lines.len() {
        let trimmed = lines[i].trim().to_string();
        if trimmed.starts_with(&format!("pub {}:", field)) {
            let mut start = i;
            while start > 0 && lines[start - 1].trim_start().starts_with("#[") {
                start -= 1;
            }
            lines.drain(start..=i);
            i = start;
            continue;
        }
        if trimmed.contains(&format!("{}: None,", field))
            || trimmed.contains(&format!("{}: Vec::new(),", field))
        {
            lines.remove(i);
            continue;
        }
        i += 1;
    }

    Ok(lines.join("\n") + "\n")
}

fn add_self_vec_init(src: &str, field: &str) -> String {
    let field_init = format!("{}: Vec::new(),", field);
    if src.contains(&field_init) {
        return src.to_string();
    }

    let self_pos = src
        .match_indices("Self {")
        .find(|(pos, _)| {
            let after_brace = &src[pos + "Self {".len()..];
            after_brace
                .chars()
                .next()
                .map(|c| c != '\n' && c != '\r')
                .unwrap_or(false)
        })
        .map(|(pos, _)| pos);

    if let Some(self_pos) = self_pos {
        let after_self = &src[self_pos..];
        let mut depth = 0;
        let mut close_pos = None;
        for (i, ch) in after_self.char_indices() {
            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        close_pos = Some(self_pos + i);
                        break;
                    }
                }
                _ => {}
            }
        }
        if let Some(close) = close_pos {
            let indent = "            ";
            let before_close = &src[..close];
            let trimmed = before_close.trim_end();
            let needs_comma = !trimmed.ends_with(',') && !trimmed.ends_with('{');
            if needs_comma {
                let content_end = before_close.trim_end().len();
                let with_comma = format!("{},", &src[..content_end]);
                return format!(
                    "{}\n{}{}: Vec::new(),\n{}",
                    with_comma,
                    indent,
                    field,
                    &src[close..]
                );
            }
            return format!(
                "{}{}{}: Vec::new(),\n{}",
                &src[..close],
                indent,
                field,
                &src[close..]
            );
        }
    }

    src.to_string()
}

/// Replaces the score type in the solution file.
pub(crate) fn replace_score_type(
    src: &str,
    old_score: &str,
    new_score: &str,
) -> Result<String, String> {
    if !src.contains(old_score) {
        return Err(format!(
            "score type '{}' not found in solution file",
            old_score
        ));
    }
    Ok(src.replace(old_score, new_score))
}