summaryrefslogtreecommitdiff
path: root/src/init.rs
diff options
context:
space:
mode:
authorMo <76752051+mo8it@users.noreply.github.com>2024-04-14 17:13:32 +0200
committerGitHub <noreply@github.com>2024-04-14 17:13:32 +0200
commitdc02c38a945fcafacf6d2d35f5d3e317e7185cb0 (patch)
treebd3ad843a575650881b220c4b008fc7509917d24 /src/init.rs
parent8c8f30d8ce3b732de649938d8945496bd769ac22 (diff)
parent7526c6b1f92626df6ab8b4853535b73711bfada4 (diff)
Merge pull request #1942 from rust-lang/tui
TUI
Diffstat (limited to 'src/init.rs')
-rw-r--r--src/init.rs63
1 files changed, 37 insertions, 26 deletions
diff --git a/src/init.rs b/src/init.rs
index 6af3235..459519d 100644
--- a/src/init.rs
+++ b/src/init.rs
@@ -6,17 +6,21 @@ use std::{
path::Path,
};
-use crate::{embedded::EMBEDDED_FILES, exercise::Exercise};
+use crate::{embedded::EMBEDDED_FILES, info_file::ExerciseInfo};
-fn create_cargo_toml(exercises: &[Exercise]) -> io::Result<()> {
+fn create_cargo_toml(exercise_infos: &[ExerciseInfo]) -> io::Result<()> {
let mut cargo_toml = Vec::with_capacity(1 << 13);
cargo_toml.extend_from_slice(b"bin = [\n");
- for exercise in exercises {
+ for exercise_info in exercise_infos {
cargo_toml.extend_from_slice(b" { name = \"");
- cargo_toml.extend_from_slice(exercise.name.as_bytes());
- cargo_toml.extend_from_slice(b"\", path = \"");
- cargo_toml.extend_from_slice(exercise.path.to_str().unwrap().as_bytes());
- cargo_toml.extend_from_slice(b"\" },\n");
+ cargo_toml.extend_from_slice(exercise_info.name.as_bytes());
+ cargo_toml.extend_from_slice(b"\", path = \"exercises/");
+ if let Some(dir) = &exercise_info.dir {
+ cargo_toml.extend_from_slice(dir.as_bytes());
+ cargo_toml.push(b'/');
+ }
+ cargo_toml.extend_from_slice(exercise_info.name.as_bytes());
+ cargo_toml.extend_from_slice(b".rs\" },\n");
}
cargo_toml.extend_from_slice(
@@ -36,46 +40,33 @@ publish = false
}
fn create_gitignore() -> io::Result<()> {
- let gitignore = b"/target";
OpenOptions::new()
.create_new(true)
.write(true)
.open(".gitignore")?
- .write_all(gitignore)
+ .write_all(GITIGNORE)
}
fn create_vscode_dir() -> Result<()> {
create_dir(".vscode").context("Failed to create the directory `.vscode`")?;
- let vs_code_extensions_json = br#"{"recommendations":["rust-lang.rust-analyzer"]}"#;
OpenOptions::new()
.create_new(true)
.write(true)
.open(".vscode/extensions.json")?
- .write_all(vs_code_extensions_json)?;
+ .write_all(VS_CODE_EXTENSIONS_JSON)?;
Ok(())
}
-pub fn init_rustlings(exercises: &[Exercise]) -> Result<()> {
+pub fn init(exercise_infos: &[ExerciseInfo]) -> Result<()> {
if Path::new("exercises").is_dir() && Path::new("Cargo.toml").is_file() {
- bail!(
- "A directory with the name `exercises` and a file with the name `Cargo.toml` already exist
-in the current directory. It looks like Rustlings was already initialized here.
-Run `rustlings` for instructions on getting started with the exercises.
-
-If you didn't already initialize Rustlings, please initialize it in another directory."
- );
+ bail!(PROBABLY_IN_RUSTLINGS_DIR_ERR);
}
let rustlings_path = Path::new("rustlings");
if let Err(e) = create_dir(rustlings_path) {
if e.kind() == ErrorKind::AlreadyExists {
- bail!(
- "A directory with the name `rustlings` already exists in the current directory.
-You probably already initialized Rustlings.
-Run `cd rustlings`
-Then run `rustlings` again"
- );
+ bail!(RUSTLINGS_DIR_ALREADY_EXISTS_ERR);
}
return Err(e.into());
}
@@ -87,7 +78,8 @@ Then run `rustlings` again"
.init_exercises_dir()
.context("Failed to initialize the `rustlings/exercises` directory")?;
- create_cargo_toml(exercises).context("Failed to create the file `rustlings/Cargo.toml`")?;
+ create_cargo_toml(exercise_infos)
+ .context("Failed to create the file `rustlings/Cargo.toml`")?;
create_gitignore().context("Failed to create the file `rustlings/.gitignore`")?;
@@ -95,3 +87,22 @@ Then run `rustlings` again"
Ok(())
}
+
+const GITIGNORE: &[u8] = b"/target
+/.rustlings-state.txt
+";
+
+const VS_CODE_EXTENSIONS_JSON: &[u8] = br#"{"recommendations":["rust-lang.rust-analyzer"]}"#;
+
+const PROBABLY_IN_RUSTLINGS_DIR_ERR: &str =
+ "A directory with the name `exercises` and a file with the name `Cargo.toml` already exist
+in the current directory. It looks like Rustlings was already initialized here.
+Run `rustlings` for instructions on getting started with the exercises.
+
+If you didn't already initialize Rustlings, please initialize it in another directory.";
+
+const RUSTLINGS_DIR_ALREADY_EXISTS_ERR: &str =
+ "A directory with the name `rustlings` already exists in the current directory.
+You probably already initialized Rustlings.
+Run `cd rustlings`
+Then run `rustlings` again";