diff options
| author | Stanisław Zagórowski <hasohasacz@gmail.com> | 2024-01-12 22:52:18 +0100 |
|---|---|---|
| committer | Stanisław Zagórowski <hasohasacz@gmail.com> | 2024-01-12 22:52:18 +0100 |
| commit | 14e2cc8846c844a234fb7688f84a874607dc96e9 (patch) | |
| tree | 4bcf1fdee1d85a971c8593a24bf06dfb4df752ea /src/luax/mod.rs | |
| parent | a56af29e6808ae4194f4a7fc5f7c906b146bb88e (diff) | |
add CLI tools
Diffstat (limited to 'src/luax/mod.rs')
| -rw-r--r-- | src/luax/mod.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/luax/mod.rs b/src/luax/mod.rs index ddbb2ef..1e87124 100644 --- a/src/luax/mod.rs +++ b/src/luax/mod.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use rlua::Lua; use crate::{error::LuaXError, Result}; @@ -91,13 +93,24 @@ pub fn preprocess(s: &str) -> Result<String> { Ok(s) } -pub fn preprocess_dir(path: &std::path::Path) -> Result<usize> { +pub fn preprocess_dir(path: &Path, output_path: &Path) -> Result<usize> { let mut preprocessed = 0; for entry in std::fs::read_dir(path)? { let entry = entry?; let path = entry.path(); if path.is_dir() { - preprocessed += preprocess_dir(&path)?; + let output_dir = output_path.join(path.file_name().unwrap()); + if !output_dir.is_dir() && output_dir.exists() { + return Err(std::io::Error::new( + std::io::ErrorKind::AlreadyExists, + format!("{} already exists", output_dir.display()), + ) + .into()); + } + if !output_dir.exists() { + std::fs::create_dir(&output_dir)?; + } + preprocessed += preprocess_dir(&path, &output_path.join(path.file_name().unwrap()))?; } else { if path.extension().unwrap_or_default() != "luax" { continue; @@ -105,7 +118,9 @@ pub fn preprocess_dir(path: &std::path::Path) -> Result<usize> { let s = std::fs::read_to_string(&path)?; let s = preprocess(&s)?; - let out_path = path.with_extension("lua"); + let out_path = output_path + .join(path.file_name().unwrap()) + .with_extension("lua"); std::fs::write(out_path, s)?; preprocessed += 1; |
