diff options
| author | mo8it <mo8it@proton.me> | 2024-04-04 23:16:57 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-04-04 23:16:57 +0200 |
| commit | 445441ce25ec8658bcdec6b2038d17e893a5903f (patch) | |
| tree | b930469e506939e69409587c778a0fc788f73728 /gen-dev-cargo-toml | |
| parent | 34375b2ebfbdb0b6504a56c82635c8c9d3d6ce59 (diff) | |
Make gen-dev-cargo-toml a separate package
so that `cargo install` only installs `rustlings`
Diffstat (limited to 'gen-dev-cargo-toml')
| -rw-r--r-- | gen-dev-cargo-toml/Cargo.toml | 10 | ||||
| -rw-r--r-- | gen-dev-cargo-toml/src/main.rs | 64 |
2 files changed, 74 insertions, 0 deletions
diff --git a/gen-dev-cargo-toml/Cargo.toml b/gen-dev-cargo-toml/Cargo.toml new file mode 100644 index 0000000..8922ae8 --- /dev/null +++ b/gen-dev-cargo-toml/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "gen-dev-cargo-toml" +publish = false +license.workspace = true +edition.workspace = true + +[dependencies] +anyhow.workspace = true +serde.workspace = true +toml_edit.workspace = true diff --git a/gen-dev-cargo-toml/src/main.rs b/gen-dev-cargo-toml/src/main.rs new file mode 100644 index 0000000..622762a --- /dev/null +++ b/gen-dev-cargo-toml/src/main.rs @@ -0,0 +1,64 @@ +// Generates `dev/Cargo.toml` such that it is synced with `info.toml`. +// `dev/Cargo.toml` is a hack to allow using `cargo run` to test `rustlings` +// during development. + +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"# This file is a hack to allow using `cargo run` to test `rustlings` during development. +# You shouldn't edit it manually. It is created and updated by running `cargo run -p gen-dev-cargo-toml`. + +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" +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`") +} |
