Skip to content

Commit

Permalink
feat: Allow replacing 2 variables in the next line
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Nov 28, 2024
1 parent 0d80a90 commit bd644f1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
40 changes: 26 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ fn process_file(
) -> Option<String> {
let mut res = String::new();

let mut replace = None;
let mut replacement = None;
let mut replace: Option<Vec<(String, String)>> = None;
let mut include = vec![true];
let mut first_line = true;

Expand Down Expand Up @@ -373,13 +372,25 @@ fn process_file(
.strip_prefix("#REPLACE ")
.or_else(|| trimmed.strip_prefix("//REPLACE "))
{
let mut split = what.split_terminator(" ");
replace = Some(split.next().unwrap().to_string());
let var = split.next().unwrap().to_string();

// Find the replacement value from the variables map
if let Some((_, value)) = variables.iter().find(|(key, _)| key == &var) {
replacement = Some(value);
let replacements = what
.split(" && ")
.filter_map(|pair| {
let mut parts = pair.split_whitespace();
if let (Some(pattern), Some(var_name)) = (parts.next(), parts.next()) {
if let Some((_, value)) = variables.iter().find(|(key, _)| key == var_name)
{
Some((pattern.to_string(), value.clone()))
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>();

if !replacements.is_empty() {
replace = Some(replacements);
}
// Check if we should include the next line(s)
} else if trimmed.starts_with("#IF ") || trimmed.starts_with("//IF ") {
Expand All @@ -401,22 +412,23 @@ fn process_file(
let mut line = line.to_string();

if trimmed.starts_with("#+") {
line = line.replace("#+", "").to_string();
line = line.replace("#+", "");
}

if trimmed.starts_with("//+") {
line = line.replace("//+", "").to_string();
line = line.replace("//+", "");
}

if let (Some(replace), Some(replacement)) = (replace, replacement) {
line = line.replace(&replace, replacement);
if let Some(replacements) = &replace {
for (pattern, value) in replacements {
line = line.replace(pattern, value);
}
}

res.push_str(&line);
res.push('\n');

replace = None;
replacement = None;
}
}

Expand Down
6 changes: 4 additions & 2 deletions template/wokwi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
[wokwi]
version = 1
gdbServerPort = 3333
elf = "target/{{ rust_target }}/debug/{{ project-name }}"
firmware = "target/{{ rust_target }}/debug/{{ project-name }}"
#REPLACE project-name project-name && rust_target rust_target
elf = "target/rust_target/debug/project-name"
#REPLACE project-name project-name && rust_target rust_target
firmware = "target/rust_target/debug/project-name"
20 changes: 10 additions & 10 deletions xtask/Cargo.lock

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

0 comments on commit bd644f1

Please sign in to comment.