summaryrefslogtreecommitdiff
path: root/src/init.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/init.rs
parent49e4a1fab04560cf0e868ab8214dfc94e76b9f4b (diff)
Fix the generated Cargo.toml after rustlings init
Diffstat (limited to 'src/init.rs')
-rw-r--r--src/init.rs26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/init.rs b/src/init.rs
index 52315e2..f210db7 100644
--- a/src/init.rs
+++ b/src/init.rs
@@ -6,17 +6,7 @@ use std::{
path::Path,
};
-use crate::embedded::EMBEDDED_FILES;
-
-const CARGO_TOML: &[u8] = {
- let cargo_toml = include_bytes!("../dev/Cargo.toml");
- // Skip the first line (comment).
- let mut start_ind = 0;
- while cargo_toml[start_ind] != b'\n' {
- start_ind += 1;
- }
- cargo_toml.split_at(start_ind + 1).1
-};
+use crate::{cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile};
pub fn init() -> Result<()> {
if Path::new("exercises").is_dir() && Path::new("Cargo.toml").is_file() {
@@ -38,7 +28,19 @@ pub fn init() -> Result<()> {
.init_exercises_dir()
.context("Failed to initialize the `rustlings/exercises` directory")?;
- fs::write("Cargo.toml", CARGO_TOML)
+ let info_file = InfoFile::parse()?;
+ let current_cargo_toml = include_str!("../dev/Cargo.toml");
+ // Skip the first line (comment).
+ let newline_ind = current_cargo_toml
+ .as_bytes()
+ .iter()
+ .position(|c| *c == b'\n')
+ .context("The embedded `Cargo.toml` is empty or contains only one line.")?;
+ let current_cargo_toml =
+ &current_cargo_toml[(newline_ind + 1).min(current_cargo_toml.len() - 1)..];
+ let updated_cargo_toml = updated_cargo_toml(&info_file.exercises, current_cargo_toml, b"")
+ .context("Failed to generate `Cargo.toml`")?;
+ fs::write("Cargo.toml", updated_cargo_toml)
.context("Failed to create the file `rustlings/Cargo.toml`")?;
fs::write(".gitignore", GITIGNORE)