Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercises/10_modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod delicious_snacks {
fn main() {
println!(
"favorite snacks: {} and {}",
delicious_snacks::fruit,
delicious_snacks::veggie,
delicious_snacks::FRUIT,
delicious_snacks::VEGGIE,
);
}
8 changes: 4 additions & 4 deletions solutions/10_modules/modules2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod delicious_snacks {
// Added `pub` and used the expected alias after `as`.
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
pub use self::fruits::PEAR as FRUIT;
pub use self::veggies::CUCUMBER as VEGGIE;

mod fruits {
pub const PEAR: &str = "Pear";
Expand All @@ -17,7 +17,7 @@ mod delicious_snacks {
fn main() {
println!(
"favorite snacks: {} and {}",
delicious_snacks::fruit,
delicious_snacks::veggie,
delicious_snacks::FRUIT,
delicious_snacks::VEGGIE,
);
}
25 changes: 9 additions & 16 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crossterm::{
QueueableCommand,
style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
};
use serde::Deserialize;
use std::{
env::{current_dir, set_current_dir},
fs::{self, create_dir},
Expand All @@ -17,22 +16,17 @@ use crate::{
info_file::InfoFile, term::press_enter_prompt,
};

#[derive(Deserialize)]
struct CargoLocateProject<'a> {
#[serde(borrow)]
root: &'a Path,
}

pub fn init() -> Result<()> {
let rustlings_dir = Path::new("rustlings");
if rustlings_dir.exists() {
bail!(RUSTLINGS_DIR_ALREADY_EXISTS_ERR);
}

let locate_project_output = Command::new("cargo")
let mut locate_project_output = Command::new("cargo")
.arg("locate-project")
.arg("-q")
.arg("--workspace")
.arg("--message-format=plain")
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
Expand Down Expand Up @@ -66,15 +60,14 @@ pub fn init() -> Result<()> {
bail!(IN_INITIALIZED_DIR_ERR);
}

let workspace_manifest =
serde_json::de::from_slice::<CargoLocateProject>(&locate_project_output.stdout)
.context(
"Failed to read the field `root` from the output of `cargo locate-project 鈥",
)?
.root;
// Remove newline
locate_project_output.stdout.pop();

let workspace_manifest = String::try_from(locate_project_output.stdout)
.context("Failed to convert the output of `cargo locate-project 鈥 to a string")?;

let workspace_manifest_content = fs::read_to_string(workspace_manifest)
.with_context(|| format!("Failed to read the file {}", workspace_manifest.display()))?;
let workspace_manifest_content = fs::read_to_string(&workspace_manifest)
.with_context(|| format!("Failed to read the file {workspace_manifest}"))?;
if !workspace_manifest_content.contains("[workspace]")
&& !workspace_manifest_content.contains("workspace.")
{
Expand Down
Loading