rustlings/src/dev/init.rs

110 lines
3.8 KiB
Rust
Raw Normal View History

2024-04-15 21:08:45 -04:00
use anyhow::{Context, Result};
2024-04-17 09:55:50 -04:00
use std::fs::{self, create_dir};
2024-04-15 21:08:45 -04:00
use crate::CURRENT_FORMAT_VERSION;
2024-04-15 17:54:57 -04:00
pub fn init() -> Result<()> {
2024-04-15 21:08:45 -04:00
create_dir("rustlings").context("Failed to create the directory `rustlings`")?;
create_dir("rustlings/exercises")
.context("Failed to create the directory `rustlings/exercises`")?;
create_dir("rustlings/solutions")
.context("Failed to create the directory `rustlings/solutions`")?;
fs::write(
"rustlings/info.toml",
format!("{INFO_FILE_BEFORE_FORMAT_VERSION}{CURRENT_FORMAT_VERSION}{INFO_FILE_AFTER_FORMAT_VERSION}"),
)
.context("Failed to create the file `rustlings/info.toml`")?;
2024-04-17 09:55:50 -04:00
fs::write("rustlings/Cargo.toml", CARGO_TOML)
.context("Failed to create the file `rustlings/Cargo.toml`")?;
2024-04-15 21:08:45 -04:00
fs::write("rustlings/.gitignore", crate::init::GITIGNORE)
.context("Failed to create the file `rustlings/.gitignore`")?;
fs::write("rustlings/README.md", README)
.context("Failed to create the file `rustlings/README.md`")?;
create_dir("rustlings/.vscode")
2024-04-15 21:18:06 -04:00
.context("Failed to create the directory `rustlings/.vscode`")?;
2024-04-15 21:08:45 -04:00
fs::write(
"rustlings/.vscode/extensions.json",
crate::init::VS_CODE_EXTENSIONS_JSON,
)
.context("Failed to create the file `rustlings/.vscode/extensions.json`")?;
println!("{INIT_DONE}");
Ok(())
2024-04-15 17:54:57 -04:00
}
2024-04-15 21:08:45 -04:00
const INFO_FILE_BEFORE_FORMAT_VERSION: &str =
"# The format version is an indicator of the compatibility of third-party exercises with the
# Rustlings program.
# The format version is not the same as the version of the Rustlings program.
# In case Rustlings makes an unavoidable breaking change to the expected format of third-party
# exercises, you would need to raise this version and adapt to the new format.
# Otherwise, the newest version of the Rustlings program won't be able to run these exercises.
format_version = ";
const INFO_FILE_AFTER_FORMAT_VERSION: &str = r#"
# Optional multi-line message to be shown to users when just starting with the exercises.
welcome_message = """Welcome to these third-party Rustlings exercises."""
# Optional multi-line message to be shown to users after finishing all exercises.
final_message = """We hope that you found the exercises helpful :D"""
# Repeat this section for every exercise.
[[exercises]]
# Exercise name which is the exercise file name without the `.rs` extension.
name = "???"
# Optional directory name to be provided if you want to organize exercises in directories.
# If `dir` is specified, the exercise path is `exercises/DIR/NAME.rs`
# Otherwise, the path is `exercises/NAME.rs`
# dir = "???"
2024-04-15 21:37:58 -04:00
# The mode to run the exercise in.
# The mode "test" (preferred) runs the exercise's tests.
# The mode "run" only checks if the exercise compiles and runs it.
mode = "test"
2024-04-15 22:00:42 -04:00
# A multi-line hint to be shown to users on request.
2024-04-15 21:08:45 -04:00
hint = """???"""
"#;
2024-04-17 09:55:50 -04:00
const CARGO_TOML: &[u8] =
br#"# Don't edit the `bin` list manually! It is updated by `rustlings dev update`
bin = []
2024-04-15 21:08:45 -04:00
2024-04-17 09:55:50 -04:00
[package]
name = "rustlings"
edition = "2021"
publish = false
[dependencies]
"#;
2024-04-15 21:08:45 -04:00
const README: &str = "# Rustlings 🦀
Welcome to these third-party Rustlings exercises 😃
First,
[install Rustlings using the official instructions in the README of the Rustlings project](https://github.com/rust-lang/rustlings) ✅
Then, open your terminal in this directory and run `rustlings` to get started with the exercises 🚀
";
const INIT_DONE: &str = r#"Initialization done!
You can start developing third-party Rustlings exercises in the `rustlings` directory :D
If the initialization was done in a Rust project which is a Cargo workspace, you need to add the
path to the `rustlings` directory to the `workspace.exclude` list in the project's `Cargo.toml`
file. For example:
[workspace]
exclude = ["rustlings"]"#;