summaryrefslogtreecommitdiff
path: root/src/cargo_toml.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-21 20:22:01 +0200
committermo8it <mo8it@proton.me>2024-04-21 20:22:01 +0200
commit642c3bd37e3195f7f744a5fa60a53e59d8da5526 (patch)
tree03415dd39c601142be147551d421d2648a15682e /src/cargo_toml.rs
parent49e4a1fab04560cf0e868ab8214dfc94e76b9f4b (diff)
Fix the generated Cargo.toml after rustlings init
Diffstat (limited to 'src/cargo_toml.rs')
-rw-r--r--src/cargo_toml.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/cargo_toml.rs b/src/cargo_toml.rs
new file mode 100644
index 0000000..2345a7e
--- /dev/null
+++ b/src/cargo_toml.rs
@@ -0,0 +1,57 @@
+use anyhow::{Context, Result};
+
+use crate::info_file::ExerciseInfo;
+
+pub fn bins_start_end_ind(cargo_toml: &str) -> Result<(usize, usize)> {
+ let start_ind = cargo_toml
+ .find("bin = [")
+ .context("Failed to find the start of the `bin` list (`bin = [`)")?
+ + 7;
+ let end_ind = start_ind
+ + cargo_toml
+ .get(start_ind..)
+ .and_then(|slice| slice.as_bytes().iter().position(|c| *c == b']'))
+ .context("Failed to find the end of the `bin` list (`]`)")?;
+
+ Ok((start_ind, end_ind))
+}
+
+pub fn append_bins(
+ buf: &mut Vec<u8>,
+ exercise_infos: &[ExerciseInfo],
+ exercise_path_prefix: &[u8],
+) {
+ buf.push(b'\n');
+ for exercise_info in exercise_infos {
+ buf.extend_from_slice(b" { name = \"");
+ buf.extend_from_slice(exercise_info.name.as_bytes());
+ buf.extend_from_slice(b"\", path = \"");
+ buf.extend_from_slice(exercise_path_prefix);
+ buf.extend_from_slice(b"exercises/");
+ if let Some(dir) = &exercise_info.dir {
+ buf.extend_from_slice(dir.as_bytes());
+ buf.push(b'/');
+ }
+ buf.extend_from_slice(exercise_info.name.as_bytes());
+ buf.extend_from_slice(b".rs\" },\n");
+ }
+}
+
+pub fn updated_cargo_toml(
+ exercise_infos: &[ExerciseInfo],
+ current_cargo_toml: &str,
+ exercise_path_prefix: &[u8],
+) -> Result<Vec<u8>> {
+ let (bins_start_ind, bins_end_ind) = bins_start_end_ind(current_cargo_toml)?;
+
+ let mut updated_cargo_toml = Vec::with_capacity(1 << 13);
+ updated_cargo_toml.extend_from_slice(current_cargo_toml[..bins_start_ind].as_bytes());
+ append_bins(
+ &mut updated_cargo_toml,
+ exercise_infos,
+ exercise_path_prefix,
+ );
+ updated_cargo_toml.extend_from_slice(current_cargo_toml[bins_end_ind..].as_bytes());
+
+ Ok(updated_cargo_toml)
+}