summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-01 02:11:52 +0200
committermo8it <mo8it@proton.me>2024-04-01 02:11:52 +0200
commit14f3585816ae12091956efcc45c1e4aefc2f91ce (patch)
tree78820c4df79df450a80b572031e6dc561a9d242b /src
parent8ad18de54cdad2e94d40d7d4cb67e4a6a274c293 (diff)
Make `cargo run` work
Diffstat (limited to 'src')
-rw-r--r--src/bin/gen-dev-cargo-toml.rs56
-rw-r--r--src/exercise.rs14
2 files changed, 67 insertions, 3 deletions
diff --git a/src/bin/gen-dev-cargo-toml.rs b/src/bin/gen-dev-cargo-toml.rs
new file mode 100644
index 0000000..20167a1
--- /dev/null
+++ b/src/bin/gen-dev-cargo-toml.rs
@@ -0,0 +1,56 @@
+use anyhow::{bail, Context, Result};
+use serde::Deserialize;
+use std::{
+ fs::{self, create_dir},
+ io::ErrorKind,
+};
+
+#[derive(Deserialize)]
+struct Exercise {
+ name: String,
+ path: String,
+}
+
+#[derive(Deserialize)]
+struct InfoToml {
+ exercises: Vec<Exercise>,
+}
+
+fn main() -> Result<()> {
+ let exercises = toml_edit::de::from_str::<InfoToml>(
+ &fs::read_to_string("info.toml").context("Failed to read `info.toml`")?,
+ )
+ .context("Failed to deserialize `info.toml`")?
+ .exercises;
+
+ let mut buf = Vec::with_capacity(1 << 14);
+
+ buf.extend_from_slice(b"bin = [\n");
+
+ for exercise in exercises {
+ buf.extend_from_slice(b" { name = \"");
+ buf.extend_from_slice(exercise.name.as_bytes());
+ buf.extend_from_slice(b"\", path = \"../");
+ buf.extend_from_slice(exercise.path.as_bytes());
+ buf.extend_from_slice(b"\" },\n");
+ }
+
+ buf.extend_from_slice(
+ br#"]
+
+[package]
+name = "rustlings"
+version = "0.0.0"
+edition = "2021"
+publish = false
+"#,
+ );
+
+ if let Err(e) = create_dir("dev") {
+ if e.kind() != ErrorKind::AlreadyExists {
+ bail!("Failed to create the `dev` directory: {e}");
+ }
+ }
+
+ fs::write("dev/Cargo.toml", buf).context("Failed to write `dev/Cargo.toml`")
+}
diff --git a/src/exercise.rs b/src/exercise.rs
index e7045d6..450acf4 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -91,9 +91,17 @@ pub struct ContextLine {
impl Exercise {
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
- Command::new("cargo")
- .arg(command)
- .arg("--color")
+ let mut cmd = Command::new("cargo");
+ cmd.arg(command);
+
+ // A hack to make `cargo run` work when developing Rustlings.
+ // Use `dev/Cargo.toml` when in the directory of the repository.
+ #[cfg(debug_assertions)]
+ if std::path::Path::new("tests").exists() {
+ cmd.arg("--manifest-path").arg("dev/Cargo.toml");
+ }
+
+ cmd.arg("--color")
.arg("always")
.arg("-q")
.arg("--bin")