summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-06-13 06:43:41 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-06-13 06:43:41 +0200
commitb80182a348578fbe54cb8cd255b5087d75a44a7c (patch)
tree6dc42f16fd7c1b1cd69fa4ae40fa58560c2d3ecb
parentfeat(config_file): Allow specifying the name of the `main.tex` file (diff)
downloadlpm-b80182a348578fbe54cb8cd255b5087d75a44a7c.tar.gz
lpm-b80182a348578fbe54cb8cd255b5087d75a44a7c.zip
fix(new::section): Avoid overriding already existing section file
-rw-r--r--src/file_tree/mod.rs47
-rw-r--r--src/new/section.rs6
2 files changed, 43 insertions, 10 deletions
diff --git a/src/file_tree/mod.rs b/src/file_tree/mod.rs
index d6f0c3c..96637b3 100644
--- a/src/file_tree/mod.rs
+++ b/src/file_tree/mod.rs
@@ -24,10 +24,13 @@
 //! you.
 
 use std::{
-    fs, io,
+    fs::{self, File},
+    io,
     path::{Path, PathBuf},
 };
 
+use anyhow::bail;
+
 /// A file tree containing all files that were generated. These are separated into host and
 /// auxiliary files. See their respective descriptions about what differentiates them.
 #[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -47,18 +50,48 @@ pub struct GeneratedFile {
     ///
     /// This should already be formatted and ready to be used.
     pub value: String,
+
+    /// Whether to override this file silently, if it already exists.
+    pub clobber: bool,
 }
 
 impl GeneratedFile {
     pub fn new(path: PathBuf, value: String) -> Self {
-        Self { path, value }
+        Self {
+            path,
+            value,
+            clobber: true,
+        }
+    }
+    pub fn new_clobber(path: PathBuf, value: String, clobber: bool) -> Self {
+        Self {
+            path,
+            value,
+            clobber,
+        }
     }
     pub fn new_in_out_dir(name: String, value: String, out_dir: &Path) -> Self {
         let path = out_dir.join(name);
-        Self { path, value }
+        Self {
+            path,
+            value,
+            clobber: true,
+        }
     }
 
-    pub fn materialize(self) -> io::Result<()> {
+    pub fn materialize(self) -> anyhow::Result<()> {
+        if !self.clobber {
+            // Check if the file exists
+            if self.path.try_exists()? {
+                bail!(
+                    "Path at '{}' already exists,\n       \
+                    but I don't want to override the file there!\n       \
+                    Please move it out of the way manually.",
+                    self.path.display()
+                )
+            }
+        }
+
         fs::create_dir_all(self.path.parent().expect("This path should have a parent"))?;
         fs::write(self.path, self.value.as_bytes())?;
         Ok(())
@@ -78,11 +111,11 @@ impl FileTree {
         files.into_iter().for_each(|file| self.add_file(file));
     }
 
-    pub fn materialize(self) -> io::Result<()> {
+    pub fn materialize(self) -> anyhow::Result<()> {
         self.files
             .into_iter()
-            .map(|file| -> io::Result<()> { file.materialize() })
-            .collect::<io::Result<()>>()?;
+            .map(|file| -> anyhow::Result<()> { file.materialize() })
+            .collect::<anyhow::Result<()>>()?;
         Ok(())
     }
 }
diff --git a/src/new/section.rs b/src/new/section.rs
index 86d347c..bfa2b2c 100644
--- a/src/new/section.rs
+++ b/src/new/section.rs
@@ -39,13 +39,13 @@ pub fn generate_new_section(
             ),
         );
 
-    let new_section_file = GeneratedFile::new(
+    file_tree.add_file(GeneratedFile::new_clobber(
         chapter_root
             .join("sections")
             .join(format!("{}.tex", MangledName::new(&name))),
         new_section_text,
-    );
-    file_tree.add_file(new_section_file);
+        false,
+    ));
 
     let chapter_file_path = chapter_root.join("chapter.tex");
     let mut chapter_file_text = fs::read_to_string(&chapter_file_path).with_context(|| {