Compare commits

..

1 Commits

Author SHA1 Message Date
5f44cb2f4e add: rewriter logic and tests 2025-03-29 22:57:31 -07:00
4 changed files with 23 additions and 8 deletions

7
Cargo.lock generated
View File

@@ -61,6 +61,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
[[package]]
name = "clap"
version = "4.5.34"
@@ -384,6 +390,7 @@ dependencies = [
name = "skubelb"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"dir-diff",
"env_logger",

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0.97"
clap = { version = "4.5.34", features = ["derive"] }
dir-diff = "0.3.3"
env_logger = "0.11.7"

View File

@@ -12,13 +12,19 @@ use rewriter::Rewriter;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Name of the person to greet
/// The needle that will be replaced. Anytime a line
/// is encountered with this needle, the line is dropped
/// and instead N lines (one per replacement) is added to
/// the output.
#[arg(short, long)]
rewrite_string: String,
/// The folder which contains the templates that
/// will be be searched for the needle.
#[arg(short, long)]
source_folder: String,
/// Where to write the replaced lines.
#[arg(short, long)]
dest_folder: String,
}

View File

@@ -1,8 +1,9 @@
use anyhow::Result;
use std::collections::HashSet;
use std::path::Path;
use std::{
fs::{self, File},
io::{self, BufReader, prelude::*},
io::{BufReader, prelude::*},
};
pub struct Rewriter {
@@ -26,7 +27,7 @@ impl Rewriter {
self.replacements.remove(replacement);
}
pub fn rewrite_folder(&self, src: &str, dst: &str) -> Result<(), io::Error> {
pub fn rewrite_folder(&self, src: &str, dst: &str) -> Result<()> {
// Make sure we are deterministic; construct a list of strings and sort
// them
let mut replacements: Vec<String> = self.replacements.iter().map(|s| s.clone()).collect();
@@ -43,7 +44,7 @@ impl Rewriter {
// First, calculate the 'relative' path after we trim the
// the src prefix, then join that with the dst prefix
let src_path = entry.path();
let src_part_path = src_path.strip_prefix(&src).unwrap();
let src_part_path = src_path.strip_prefix(&src)?;
let dst_path = dst_base.join(src_part_path);
if metadata.is_dir() {
@@ -96,9 +97,7 @@ mod tests {
#[test]
fn basic_test() {
let testdata = TempDir::new("").unwrap();
TEST_FILES
.extract(testdata.path())
.unwrap();
TEST_FILES.extract(testdata.path()).unwrap();
let src = testdata.path().join("testsrc");
let dst = TempDir::new("").unwrap();
@@ -115,6 +114,8 @@ mod tests {
.unwrap();
// Validate that everything matches
assert!(dir_diff::is_different(testdata.path().join("testdst"), dst.path()).unwrap() == false);
assert!(
dir_diff::is_different(testdata.path().join("testdst"), dst.path()).unwrap() == false
);
}
}