1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-04-27 12:22:47 +02:00
rustlings/src/main.rs

215 lines
6.9 KiB
Rust
Raw Normal View History

2024-04-16 01:22:54 +02:00
use anyhow::{bail, Context, Result};
use app_state::StateFileStatus;
2023-08-25 23:18:01 +02:00
use clap::{Parser, Subcommand};
2024-04-14 16:03:49 +02:00
use std::{
2024-08-08 02:45:18 +02:00
io::{self, IsTerminal, Write},
2024-04-27 17:31:51 +02:00
path::Path,
2024-10-13 22:02:41 +02:00
process::ExitCode,
2024-04-14 16:03:49 +02:00
};
2024-08-08 02:45:18 +02:00
use term::{clear_terminal, press_enter_prompt};
2019-01-09 20:33:43 +01:00
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile};
2024-04-18 11:31:08 +02:00
mod app_state;
mod cargo_toml;
mod cmd;
2024-08-08 23:46:21 +02:00
mod collections;
2024-04-15 23:54:57 +02:00
mod dev;
2024-03-28 21:06:36 +01:00
mod embedded;
mod exercise;
2024-04-14 01:15:43 +02:00
mod info_file;
mod init;
2024-04-07 03:03:37 +02:00
mod list;
2019-01-09 20:33:43 +01:00
mod run;
2024-08-08 02:45:18 +02:00
mod term;
mod watch;
2018-05-14 18:41:58 +02:00
2024-04-16 01:22:54 +02:00
const CURRENT_FORMAT_VERSION: u8 = 1;
2024-04-29 17:01:47 +02:00
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
2023-08-25 23:18:01 +02:00
#[derive(Parser)]
#[command(version)]
struct Args {
2023-08-25 23:18:01 +02:00
#[command(subcommand)]
command: Option<Subcommands>,
2024-05-13 02:37:32 +02:00
/// Manually run the current exercise using `r` in the watch mode.
2024-04-14 17:10:53 +02:00
/// Only use this if Rustlings fails to detect exercise file changes.
#[arg(long)]
manual_run: bool,
}
2023-08-25 23:18:01 +02:00
#[derive(Subcommand)]
enum Subcommands {
2024-04-29 17:01:47 +02:00
/// Initialize the official Rustlings exercises
Init,
2024-04-22 00:45:16 +02:00
/// Run a single exercise. Runs the next pending exercise if the exercise name is not specified
2023-08-25 23:18:01 +02:00
Run {
/// The name of the exercise
name: Option<String>,
2023-08-25 23:18:01 +02:00
},
2024-10-13 22:02:41 +02:00
/// Check all the exercises, marking them as done or pending accordingly.
CheckAll,
2024-03-28 22:11:16 +01:00
/// Reset a single exercise
2023-08-25 23:18:01 +02:00
Reset {
/// The name of the exercise
name: String,
},
2024-04-22 00:45:16 +02:00
/// Show a hint. Shows the hint of the next pending exercise if the exercise name is not specified
2023-08-25 23:18:01 +02:00
Hint {
/// The name of the exercise
name: Option<String>,
2023-08-25 23:18:01 +02:00
},
2024-04-22 00:45:16 +02:00
/// Commands for developing (third-party) Rustlings exercises
2024-04-15 23:54:57 +02:00
#[command(subcommand)]
Dev(DevCommands),
}
2024-10-13 22:02:41 +02:00
fn main() -> Result<ExitCode> {
2023-08-25 23:18:01 +02:00
let args = Args::parse();
2024-08-01 15:23:54 +02:00
if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() {
2024-04-21 19:34:55 +02:00
bail!("{OLD_METHOD_ERR}");
}
2024-10-13 22:02:41 +02:00
'priority_cmd: {
match args.command {
Some(Subcommands::Init) => init::init().context("Initialization failed")?,
Some(Subcommands::Dev(dev_command)) => dev_command.run()?,
_ => break 'priority_cmd,
}
return Ok(ExitCode::SUCCESS);
}
if !Path::new("exercises").is_dir() {
2024-04-12 01:24:01 +02:00
println!("{PRE_INIT_MSG}");
2024-10-13 22:02:41 +02:00
return Ok(ExitCode::FAILURE);
}
2024-04-17 15:55:50 +02:00
let info_file = InfoFile::parse()?;
if info_file.format_version > CURRENT_FORMAT_VERSION {
bail!(FORMAT_VERSION_HIGHER_ERR);
}
2024-04-14 16:03:49 +02:00
let (mut app_state, state_file_status) = AppState::new(
info_file.exercises,
info_file.final_message.unwrap_or_default(),
2024-04-27 17:31:51 +02:00
)?;
2024-04-14 16:03:49 +02:00
2024-04-29 17:01:47 +02:00
// Show the welcome message if the state file doesn't exist yet.
2024-04-14 16:03:49 +02:00
if let Some(welcome_message) = info_file.welcome_message {
match state_file_status {
StateFileStatus::NotRead => {
let mut stdout = io::stdout().lock();
2024-04-30 01:41:08 +02:00
clear_terminal(&mut stdout)?;
2024-04-14 16:03:49 +02:00
2024-08-02 16:28:05 +02:00
let welcome_message = welcome_message.trim_ascii();
2024-04-14 16:03:49 +02:00
write!(stdout, "{welcome_message}\n\nPress ENTER to continue ")?;
2024-08-08 02:45:18 +02:00
press_enter_prompt(&mut stdout)?;
2024-04-30 01:41:08 +02:00
clear_terminal(&mut stdout)?;
2024-08-09 01:08:52 +02:00
// Flush to be able to show errors occurring before printing a newline to stdout.
stdout.flush()?;
2024-04-14 16:03:49 +02:00
}
StateFileStatus::Read => (),
}
}
2024-04-05 03:04:53 +02:00
match args.command {
2024-04-14 01:15:43 +02:00
None => {
2024-07-08 12:53:44 +02:00
if !io::stdout().is_terminal() {
bail!("Unsupported or missing terminal/TTY");
}
2024-04-25 14:44:12 +02:00
let notify_exercise_names = if args.manual_run {
2024-04-14 17:10:53 +02:00
None
} else {
2024-06-14 13:32:37 +02:00
// For the notify event handler thread.
2024-04-14 17:10:53 +02:00
// Leaking is not a problem because the slice lives until the end of the program.
Some(
2024-04-25 14:44:12 +02:00
&*app_state
2024-04-14 17:10:53 +02:00
.exercises()
.iter()
2024-04-25 14:44:12 +02:00
.map(|exercise| exercise.name.as_bytes())
2024-04-14 17:10:53 +02:00
.collect::<Vec<_>>()
.leak(),
)
};
2024-04-14 01:15:43 +02:00
watch::watch(&mut app_state, notify_exercise_names)?;
2024-04-14 01:15:43 +02:00
}
2024-04-05 03:04:53 +02:00
Some(Subcommands::Run { name }) => {
if let Some(name) = name {
app_state.set_current_exercise_by_name(&name)?;
}
2024-10-13 22:02:41 +02:00
return run::run(&mut app_state);
}
2024-10-13 22:02:41 +02:00
Some(Subcommands::CheckAll) => {
let mut stdout = io::stdout().lock();
2024-10-13 22:02:41 +02:00
if let Some(first_pending_exercise_ind) = app_state.check_all_exercises(&mut stdout)? {
if app_state.current_exercise().done {
2024-10-13 22:02:41 +02:00
app_state.set_current_exercise_ind(first_pending_exercise_ind)?;
}
2024-10-13 22:02:41 +02:00
2024-10-14 00:42:49 +02:00
stdout.write_all(b"\n\n")?;
2024-10-13 22:02:41 +02:00
let pending = app_state.n_pending();
if pending == 1 {
2024-10-13 23:28:17 +02:00
stdout.write_all(b"One exercise pending: ")?;
} else {
2024-10-13 23:28:17 +02:00
write!(
stdout,
2024-10-14 00:42:49 +02:00
"{pending}/{} exercises pending. The first: ",
2024-10-13 23:28:17 +02:00
app_state.exercises().len(),
)?;
}
app_state
.current_exercise()
.terminal_file_link(&mut stdout)?;
2024-10-13 22:02:41 +02:00
stdout.write_all(b"\n")?;
2024-10-14 01:28:34 +02:00
2024-10-13 22:02:41 +02:00
return Ok(ExitCode::FAILURE);
} else {
app_state.render_final_message(&mut stdout)?;
}
}
2024-04-05 03:04:53 +02:00
Some(Subcommands::Reset { name }) => {
app_state.set_current_exercise_by_name(&name)?;
2024-04-18 12:41:17 +02:00
let exercise_path = app_state.reset_current_exercise()?;
println!("The exercise {exercise_path} has been reset");
}
2024-04-05 03:04:53 +02:00
Some(Subcommands::Hint { name }) => {
if let Some(name) = name {
app_state.set_current_exercise_by_name(&name)?;
}
println!("{}", app_state.current_exercise().hint);
}
2024-04-16 03:15:14 +02:00
// Handled in an earlier match.
Some(Subcommands::Init | Subcommands::Dev(_)) => (),
}
2024-03-25 03:46:56 +01:00
2024-10-13 22:02:41 +02:00
Ok(ExitCode::SUCCESS)
2018-05-06 18:59:50 +02:00
}
2024-04-12 01:24:01 +02:00
2024-07-07 15:53:48 +02:00
const OLD_METHOD_ERR: &str =
"You are trying to run Rustlings using the old method before version 6.
2024-04-21 19:34:55 +02:00
The new method doesn't include cloning the Rustlings' repository.
2024-07-07 15:53:48 +02:00
Please follow the instructions in `README.md`:
2024-04-21 19:34:55 +02:00
https://github.com/rust-lang/rustlings#getting-started";
2024-04-16 01:22:54 +02:00
const FORMAT_VERSION_HIGHER_ERR: &str =
"The format version specified in the `info.toml` file is higher than the last one supported.
It is possible that you have an outdated version of Rustlings.
Try to install the latest Rustlings version first.";
2024-04-12 01:24:01 +02:00
const PRE_INIT_MSG: &str = r"
Welcome to...
2024-04-12 01:24:01 +02:00
_ _ _
_ __ _ _ ___| |_| (_)_ __ __ _ ___
| '__| | | / __| __| | | '_ \ / _` / __|
| | | |_| \__ \ |_| | | | | | (_| \__ \
|_| \__,_|___/\__|_|_|_| |_|\__, |___/
|___/
2024-07-07 15:53:48 +02:00
The `exercises/` directory couldn't be found in the current directory.
2024-04-12 01:24:01 +02:00
If you are just starting with Rustlings, run the command `rustlings init` to initialize it.";