From 37aa23720923b93f15079603edc3ea726151afbf Mon Sep 17 00:00:00 2001 From: Lyle Mantooth Date: Wed, 31 Jan 2024 15:49:41 -0500 Subject: [PATCH] Parse "#REPLACE" macro in MML. --- Cargo.lock | 2 +- Cargo.toml | 2 +- flake.nix | 2 +- src/lib.rs | 1 + src/mfvi.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/mfvi.rs diff --git a/Cargo.lock b/Cargo.lock index b3a9899..8670b04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "quick-start" +name = "slamshuffle" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3e2467c..564d871 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "quick-start" +name = "slamshuffle" version = "0.1.0" edition = "2021" license = "MIT" diff --git a/flake.nix b/flake.nix index 5eb191d..284615d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Build a cargo project"; + description = "Music randomizer for Worlds Collide, based on johnnydmad"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b0011ee --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod mfvi; diff --git a/src/mfvi.rs b/src/mfvi.rs new file mode 100644 index 0000000..474aea1 --- /dev/null +++ b/src/mfvi.rs @@ -0,0 +1,28 @@ +struct Translation<'a> { + find: &'a str, + replacement: &'a str, +} + +pub fn replace_chars(input: &str) -> String { + let trs: Vec> = input + .lines() + .filter_map(|line| { + line.strip_prefix("#REPLACE ") + .and_then(|l| l.rsplit_once(' ')) + .and_then(|(first, second)| { + let length = std::cmp::min(first.len(), second.len()); + Some(Translation { + find: &first[0..length], + replacement: &second[0..length], + }) + }) + }) + .collect(); + + let mut new = input.to_string(); + for tr in trs { + new = new.replace(tr.find, tr.replacement); + } + + new +}