Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/solverforge-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ clap_complete = { workspace = true }
dialoguer = { workspace = true }
include_dir = { workspace = true }
owo-colors = { workspace = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
strsim = { workspace = true }
toml = { workspace = true }

Expand Down
98 changes: 76 additions & 22 deletions crates/solverforge-cli/src/commands/destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub fn run_entity(name: &str, skip_confirm: bool) -> CliResult {

remove_from_domain_mod(&file_name)?;
unwire_collection_from_solution(&entity.field_name, &entity.item_type, &domain.solution_type)?;
crate::commands::sf_config::remove_entity(&snake)?;

output::print_remove(&format!("src/domain/{}.rs", file_name));
output::print_update("src/domain/mod.rs");
Expand Down Expand Up @@ -154,6 +155,7 @@ pub fn run_fact(name: &str, skip_confirm: bool) -> CliResult {

remove_from_domain_mod(&file_name)?;
unwire_collection_from_solution(&fact.field_name, &fact.item_type, &domain.solution_type)?;
crate::commands::sf_config::remove_fact(&snake)?;

output::print_remove(&format!("src/domain/{}.rs", file_name));
output::print_update("src/domain/mod.rs");
Expand Down Expand Up @@ -182,6 +184,7 @@ pub fn run_constraint(name: &str, skip_confirm: bool) -> CliResult {
})?;

remove_constraint_from_mod(&snake)?;
crate::commands::sf_config::remove_constraint(&snake)?;

output::print_remove(&file_path);
output::print_update("src/constraints/mod.rs");
Expand All @@ -200,15 +203,15 @@ fn remove_from_domain_mod(mod_name: &str) -> CliResult {
})?;

let lines: Vec<&str> = content.lines().collect();
let mut new_lines = Vec::new();
let mut new_lines: Vec<String> = Vec::new();

for line in lines {
if line.trim() == format!("mod {};", mod_name)
|| line.trim().starts_with(&format!("pub use {}::", mod_name))
{
continue;
}
new_lines.push(line);
new_lines.push(line.to_string());
}

let new_content = new_lines.join("\n");
Expand Down Expand Up @@ -283,35 +286,21 @@ fn remove_constraint_from_mod(name: &str) -> CliResult {
})?;

let lines: Vec<&str> = content.lines().collect();
let mut new_lines = Vec::new();
let mut in_tuple = false;
let mut removed_item = false;
let mut new_lines: Vec<String> = Vec::new();

for line in lines {
if line.trim() == format!("mod {};", name) {
continue;
}

if line.contains("pub fn all() ->") || line.contains("impl Constraint") {
in_tuple = true;
new_lines.push(line);
} else if in_tuple && line.contains(')') {
if removed_item && !new_lines.is_empty() {
let last_idx = new_lines.len() - 1;
if let Some(last) = new_lines.get_mut(last_idx) {
if last.trim().ends_with(',') {
*last = last.trim().trim_end_matches(',');
}
}
if let Some(updated_line) = remove_constraint_call_from_line(line, name) {
if updated_line.trim().is_empty() {
continue;
}
in_tuple = false;
new_lines.push(line);
} else if in_tuple && line.contains(&format!("{}::", name)) {
removed_item = true;
new_lines.push(updated_line);
continue;
} else {
new_lines.push(line);
}
new_lines.push(line.to_string());
}

let result = new_lines.join("\n");
Expand All @@ -323,3 +312,68 @@ fn remove_constraint_from_mod(name: &str) -> CliResult {

Ok(())
}

fn remove_constraint_call_from_line(line: &str, name: &str) -> Option<String> {
let needle = format!("{name}::constraint()");
if !line.contains(&needle) {
return None;
}

let indent: String = line.chars().take_while(|c| c.is_whitespace()).collect();
let trimmed = line.trim();
let had_trailing_comma = trimmed.ends_with(',');
let without_trailing_comma = trimmed.trim_end_matches(',');
let has_tuple_wrapper =
without_trailing_comma.starts_with('(') && without_trailing_comma.ends_with(')');
let inner = if has_tuple_wrapper {
&without_trailing_comma[1..without_trailing_comma.len() - 1]
} else {
without_trailing_comma
};

let kept_parts: Vec<&str> = inner
.split(',')
.map(str::trim)
.filter(|part| !part.is_empty() && *part != needle)
.collect();

if kept_parts.is_empty() {
return Some(String::new());
}

let mut rebuilt = if has_tuple_wrapper {
format!("({})", kept_parts.join(", "))
} else {
kept_parts.join(", ")
};

if had_trailing_comma {
rebuilt.push(',');
}

Some(format!("{indent}{rebuilt}"))
}

#[cfg(test)]
mod tests {
use super::remove_constraint_call_from_line;

#[test]
fn removes_constraint_from_multiline_tuple_entry() {
let line = " all_assigned::constraint(),";
let updated = remove_constraint_call_from_line(line, "all_assigned")
.expect("line should be rewritten");
assert!(updated.is_empty());
}

#[test]
fn removes_constraint_from_flat_tuple_line() {
let line = " (capacity::constraint(), extra::constraint(), distance::constraint())";
let updated =
remove_constraint_call_from_line(line, "extra").expect("line should be rewritten");
assert_eq!(
updated,
" (capacity::constraint(), distance::constraint())"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub fn run(
source: e,
})?;

crate::commands::sf_config::add_constraint(name)?;

output::print_create(&format!("src/constraints/{}.rs", name));
print_diff_verbose("", &skeleton);
output::print_update("src/constraints/mod.rs");
Expand Down
4 changes: 4 additions & 0 deletions crates/solverforge-cli/src/commands/generate_domain/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub fn run_entity(

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);
Expand Down Expand Up @@ -94,6 +96,8 @@ pub fn run_fact(name: &str, fields: &[String], force: bool, pretend: bool) -> Cl

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);
Expand Down
10 changes: 3 additions & 7 deletions crates/solverforge-cli/src/commands/generate_domain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
utils::{pluralize, snake_to_pascal, validate_score_type},
wiring::{add_import, replace_score_type},
};
use std::sync::{Mutex, OnceLock};
use crate::test_support;

#[test]
fn test_snake_to_pascal() {
Expand Down Expand Up @@ -151,12 +151,7 @@ fn test_generate_data_loader_stub_is_compile_safe() {

#[test]
fn test_remove_default_scaffold_rewrites_data_module_without_todo() {
static CWD_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

let _guard = CWD_LOCK
.get_or_init(|| Mutex::new(()))
.lock()
.expect("cwd lock poisoned");
let guard = test_support::lock_cwd();

let tmp = tempfile::tempdir().expect("failed to create temp dir");
let original_dir = std::env::current_dir().expect("failed to get current dir");
Expand Down Expand Up @@ -194,6 +189,7 @@ fn test_remove_default_scaffold_rewrites_data_module_without_todo() {
std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
let result = remove_default_scaffold();
std::env::set_current_dir(original_dir).expect("failed to restore current dir");
drop(guard);

result.expect("remove_default_scaffold should succeed");

Expand Down
1 change: 1 addition & 0 deletions crates/solverforge-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub mod info;
pub mod new;
pub mod routes;
pub mod server;
pub mod sf_config;
pub mod test;
Loading
Loading