From aa813fbce1305bb1beac88bff47f4279948cb3ac Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 16 Apr 2024 03:30:28 +0200 Subject: [PATCH] Update Cargo.toml on `dev check` --- src/dev.rs | 6 ++++-- src/dev/check.rs | 17 ++++++++++++++--- src/init.rs | 2 +- src/main.rs | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/dev.rs b/src/dev.rs index e09996f..7905f38 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -1,6 +1,8 @@ use anyhow::{Context, Result}; use clap::Subcommand; +use crate::info_file::InfoFile; + mod check; mod init; @@ -11,10 +13,10 @@ pub enum DevCommands { } impl DevCommands { - pub fn run(self) -> Result<()> { + pub fn run(self, info_file: InfoFile) -> Result<()> { match self { DevCommands::Init => init::init().context(INIT_ERR), - DevCommands::Check => check::check(), + DevCommands::Check => check::check(info_file), } } } diff --git a/src/dev/check.rs b/src/dev/check.rs index 46d3ffe..9ae066b 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -1,5 +1,16 @@ -use anyhow::Result; +use std::fs; -pub fn check() -> Result<()> { - todo!() +use anyhow::{Context, Result}; + +use crate::{info_file::InfoFile, init::cargo_toml}; + +pub fn check(info_file: InfoFile) -> Result<()> { + // TODO: Add checks + + fs::write("Cargo.toml", cargo_toml(&info_file.exercises)) + .context("Failed to update the file `Cargo.toml`")?; + + println!("Everything looks fine!"); + + Ok(()) } diff --git a/src/init.rs b/src/init.rs index 7648202..5fa44d4 100644 --- a/src/init.rs +++ b/src/init.rs @@ -8,7 +8,7 @@ use std::{ use crate::{embedded::EMBEDDED_FILES, info_file::ExerciseInfo}; -fn cargo_toml(exercise_infos: &[ExerciseInfo]) -> Vec { +pub fn cargo_toml(exercise_infos: &[ExerciseInfo]) -> Vec { let mut cargo_toml = Vec::with_capacity(1 << 13); cargo_toml.extend_from_slice(b"bin = [\n"); for exercise_info in exercise_infos { diff --git a/src/main.rs b/src/main.rs index 5188ee1..8b3f28f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ fn main() -> Result<()> { Some(Subcommands::Init) => { return init::init(&info_file.exercises).context("Initialization failed"); } - Some(Subcommands::Dev(dev_command)) => return dev_command.run(), + Some(Subcommands::Dev(dev_command)) => return dev_command.run(info_file), _ => (), }