From a6b3f99c534b8ac558c00601f08b1fd7dc361150 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 7 Jul 2015 11:46:49 +0200 Subject: [PATCH] Init now creates 'src' and 'book' folder. In 'src' SUMMARY.md is created with one dummy chapter --- src/main.rs | 4 +++- src/mdbook.rs | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9cfad581..df2948af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,7 +115,9 @@ fn init(args: Vec) { let dir = std::env::current_dir().unwrap(); let book = MDBook::new(); - book.init(&dir); + if let Err(e) = book.init(&dir) { + println!("Error: {}", e); + } } diff --git a/src/mdbook.rs b/src/mdbook.rs index e3629ec2..4a1d815b 100644 --- a/src/mdbook.rs +++ b/src/mdbook.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; use std::fs::{self, File, metadata}; +use std::io::Write; pub struct MDBook { dest: PathBuf, @@ -37,31 +38,43 @@ impl MDBook { // Hacky way to check if the directory exists... Until PathExt moves to stable match metadata(&dest) { Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - fs::create_dir(&dest).unwrap(); - }, + // There is a very high chance that the error is due to the fact that + // the directory / file does not exist + fs::create_dir(&dest).unwrap(); + }, Ok(_) => { /* If there is no error, the directory / file does exist */ } } // Hacky way to check if the directory exists... Until PathExt moves to stable match metadata(&src) { Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - fs::create_dir(&src).unwrap(); - }, + // There is a very high chance that the error is due to the fact that + // the directory / file does not exist + fs::create_dir(&src).unwrap(); + }, Ok(_) => { /* If there is no error, the directory / file does exist */ } } // Hacky way to check if the directory exists... Until PathExt moves to stable - match metadata(dir.join("src/SUMMARY.md")) { + let summary = match metadata(&src.join("SUMMARY.md")) { Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - File::create("src/SUMMARY.md").unwrap(); - }, - Ok(_) => { /* If there is no error, the directory / file does exist */ } + // There is a very high chance that the error is due to the fact that + // the directory / file does not exist + Result::Ok(File::create(&src.join("SUMMARY.md")).unwrap()) + }, + Ok(_) => { + /* If there is no error, the directory / file does exist */ + Result::Err("SUMMARY.md does already exist") + } + }; + + if let Ok(mut f) = summary { + writeln!(f, "# Summary"); + writeln!(f, ""); + writeln!(f, "[Chapter 1](./chapter_1.md)"); + + let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap(); + writeln!(chapter_1, "# Chapter 1"); } return Ok(());