summaryrefslogtreecommitdiff
path: root/src/batchmode.rs
diff options
context:
space:
mode:
authorstuebinm2024-04-03 22:22:38 +0200
committerstuebinm2024-04-03 23:36:33 +0200
commit0567f916d4365c8dc0be99d194fe6d157befbc81 (patch)
tree8e1123ae8112abab0f3726da75bec2c08787ce0e /src/batchmode.rs
parent48534f8c321cb33190a3cc80a9c364ffbf68c878 (diff)
very basic query language
Diffstat (limited to '')
-rw-r--r--src/batchmode.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/batchmode.rs b/src/batchmode.rs
new file mode 100644
index 0000000..8398bce
--- /dev/null
+++ b/src/batchmode.rs
@@ -0,0 +1,61 @@
+use std::{path::PathBuf, fs, sync::{Arc, Mutex}};
+use threadpool::ThreadPool;
+
+use crate::status_reporter::*;
+
+// TODO: make this usable
+// (this module just here to keep old code around for a bit)
+pub enum Task {}
+
+#[allow(unreachable_code, unused)]
+pub fn batchmode(tasks: Vec<(PathBuf, Task)>) {
+
+ let pool = ThreadPool::new(16);
+
+ let results = Arc::new(Mutex::new(vec![]));
+ let printer = Arc::new(StatusReport::new(tasks.len(), tasks.len()));
+
+ for (path, task) in tasks {
+ pool.execute({
+ let results = Arc::clone(&results);
+ let printer = Arc::clone(&printer);
+
+ move || {
+ printer.enter_file(&format!("{path:?}"));
+
+ let result: anyhow::Result<(PathBuf, String)> = todo!();
+
+ results.lock().unwrap().push(result);
+ }
+ });
+ }
+
+ pool.join();
+
+ println!("\n\nSummary:");
+ let mut c_errors = 0;
+ let mut c_total = 0;
+ for r in results.lock().unwrap().iter() {
+ match r {
+ Err(e) => {
+ println!(" {}", e);
+ c_errors += 1;
+ },
+ _ => ()
+ }
+ c_total += 1;
+ }
+
+ println!("\n ({c_total} sites total, {c_errors} errors, generated {} edits)", c_total - c_errors);
+
+ let edits: Vec<_> = Arc::into_inner(results).unwrap().into_inner().unwrap()
+ .into_iter()
+ .filter_map(|r| r.ok())
+ .collect();
+
+ println!("applying changes ...");
+ for (filename, content) in edits {
+ fs::write(&filename, content.as_bytes()).unwrap();
+ // println!("{}", content);
+ }
+}