summary refs log tree commit diff stats
path: root/src/new/section.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/new/section.rs')
-rw-r--r--src/new/section.rs102
1 files changed, 51 insertions, 51 deletions
diff --git a/src/new/section.rs b/src/new/section.rs
index 31742c2..a359fb0 100644
--- a/src/new/section.rs
+++ b/src/new/section.rs
@@ -1,63 +1,63 @@
-use std::{
-    env,
-    fs::{self, read_dir},
-    io::{self, ErrorKind},
-    path::PathBuf,
-    time::SystemTime,
-};
+use std::{fs, path::Path, time::SystemTime};
 
+use anyhow::Context;
 use chrono::{DateTime, Local};
 use convert_case::{Case, Casing};
+use log::debug;
 
-use super::SECTION;
+use crate::{
+    config_file::Config,
+    file_tree::{FileTree, GeneratedFile},
+};
 
-pub fn generate_new_section(name: String) -> io::Result<()> {
-    let chapter_root = get_section_root()?;
-    let chapter_main_file_path = read_dir(&chapter_root)?
-        .into_iter()
-        .map(|path| path.unwrap())
-        .filter(|path| path.file_name().to_str().unwrap().contains("chapter_"))
-        .last()
-        .unwrap()
-        .path();
-    let mut main_file = fs::read_to_string(&chapter_main_file_path).unwrap();
+pub fn generate_new_section(
+    config: &Config,
+    name: String,
+    project_root: &Path,
+    chapter_name: &str,
+) -> anyhow::Result<FileTree> {
+    let chapter_root = project_root.join("content").join(chapter_name);
+    debug!("Chapter root is: {}", chapter_root.display());
 
-    main_file.push_str(&format!(
-        "\\input{{content/{}/sections/{}}}\n",
-        chapter_root.file_name().unwrap().to_str().unwrap(),
-        &name.to_case(Case::Snake)
-    ));
-    fs::write(chapter_main_file_path, main_file)?;
-    fs::write(
-        chapter_root.join(format!("sections/{}.tex", name.to_case(Case::Snake))),
-        SECTION.replace("REPLACMENT_SECTION_TITLE", &name).replace(
+    let mut file_tree = FileTree::new();
+
+    let new_section_text = config
+        .templates
+        .section
+        .replace("REPLACMENT_SECTION_TITLE", &name)
+        .replace(
             "DATE",
             &format!(
                 "{}",
-                DateTime::<Local>::from(SystemTime::now()).format("%Y-%m-%d")
+                // FIXME: The time is not really precise enough to display the time.  <2024-03-31>
+                DateTime::<Local>::from(SystemTime::now()).format("%Y-%m-%d (%_H:%_S)")
             ),
-        ),
-    )?;
-    Ok(())
-}
+        );
+
+    let new_section_file = GeneratedFile::new(
+        chapter_root
+            .join("sections")
+            .join(format!("{}.tex", name.to_case(Case::Snake))),
+        new_section_text,
+    );
+    file_tree.add_file(new_section_file);
+
+    let chapter_file_path = chapter_root.join("chapter.tex");
+    let mut chapter_file_text = fs::read_to_string(&chapter_file_path).with_context(|| {
+        format!(
+            "Failed to read the chapter main file ('{}') to string",
+            &chapter_file_path.display(),
+        )
+    })?;
+
+    chapter_file_text.push_str(&format!(
+        "\\input{{content/{}/sections/{}}}\n",
+        chapter_name,
+        &name.to_case(Case::Snake)
+    ));
+
+    let chapter_file = GeneratedFile::new(chapter_file_path, chapter_file_text);
+    file_tree.add_file(chapter_file);
 
-pub fn get_section_root() -> io::Result<PathBuf> {
-    let path = env::current_dir()?;
-    let mut path_ancestors = path.as_path().ancestors();
-
-    while let Some(path_segment) = path_ancestors.next() {
-        if read_dir(path_segment)?.into_iter().any(|path| {
-            path.expect("The read_dir shouldn't error out here")
-                .file_name()
-                .to_str()
-                .unwrap()
-                .contains("chapter_")
-        }) {
-            return Ok(PathBuf::from(path_segment));
-        }
-    }
-    Err(io::Error::new(
-        ErrorKind::NotFound,
-        "Ran out of places to find chapter_root",
-    ))
+    Ok(file_tree)
 }