use std::collections::HashMap;
use std::fs;
use std::io::{self, Write};
pub struct DataProcessor {
data: HashMap<String, Vec<u8>>,
config: ProcessorConfig,
}
pub struct ProcessorConfig {
batch_size: usize,
compression: bool,
parallel: bool,
}
impl DataProcessor {
pub fn new(config: ProcessorConfig) -> Self {
Self {
data: HashMap::new(),
config,
}
}
pub fn process_file(&mut self, path: &str) -> io::Result<()> {
let content = fs::read(path)?;
self.data.insert(path.to_string(), content);
Ok(())
}
pub fn batch_process(&mut self, paths: Vec<&str>) -> io::Result<Vec<String>> {
let mut results = Vec::new();
for path in paths {
match self.process_file(path) {
Ok(_) => results.push(format!("Processed: {}", path)),
Err(e) => results.push(format!("Error {}: {}", path, e)),
}
}
Ok(results)
}
pub fn export_data(&self, format: &str) -> String {
match format {
"json" => self.to_json(),
"csv" => self.to_csv(),
_ => "Unsupported format".to_string(),
}
}
fn to_json(&self) -> String {
let mut json = String::from("{\n");
for (key, value) in &self.data {
json.push_str(&format!(" \"{}\": {},\n", key, value.len()));
}
json.push_str("}");
json
}
fn to_csv(&self) -> String {
let mut csv = String::from("file,size\n");
for (key, value) in &self.data {
csv.push_str(&format!("{},{}\n", key, value.len()));
}
csv
}
}
fn main() {
let config = ProcessorConfig {
batch_size: 1000,
compression: true,
parallel: false,
};
let mut processor = DataProcessor::new(config);
println!("Data processor initialized");
}