diff --git a/Cargo.toml b/Cargo.toml index fe09a15..2c33aa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ zip = {version = ">= 0.6.2", default-features = false, features = ["deflate"]} [profile.release] lto = true opt-level = 3 +strip = "symbols" diff --git a/src/main.rs b/src/main.rs index f805302..caf39b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,20 +18,21 @@ fn main() { fn main_inner() -> Result<(), Box> { let (input_filename, is_zip) = reader::find_input_file()?; println!("Processing {}", input_filename); + let lines = reader::read_data(&input_filename, is_zip)?; let (header, body) = processor::split_into_header_and_body(&lines)?; let coalition_per_line = processor::divide_body_by_coalition(body)?; - let mut blue_writer = writer::create_writer(is_zip, &input_filename)?; + let mut blue_writer = writer::create_writer(is_zip, &input_filename, &Coalition::Blue)?; blue_writer.write_strings(header)?; blue_writer.write_for_coalition(body, &coalition_per_line, Coalition::Blue)?; - let mut red_writer = writer::create_writer(is_zip, &input_filename)?; + let mut red_writer = writer::create_writer(is_zip, &input_filename, &Coalition::Red)?; red_writer.write_strings(header)?; red_writer.write_for_coalition(body, &coalition_per_line, Coalition::Red)?; - let mut purple_writer = writer::create_writer(is_zip, &input_filename)?; + let mut purple_writer = writer::create_writer(is_zip, &input_filename, &Coalition::Purple)?; purple_writer.write_strings(header)?; purple_writer.write_for_coalition(body, &coalition_per_line, Coalition::Purple)?; Ok(()) diff --git a/src/processor.rs b/src/processor.rs index 3aa1ce9..4b94c66 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -76,7 +76,7 @@ impl Line { current_line: &'a S, coalition_ids: &mut CoalitionIDs<'a>, ) -> Result { - let id = Self::get_id_from_line(current_line)?; + let id = Self::get_id_from_line(current_line, &line_type)?; let continued = Self::will_line_continue(current_line); let coalition = Self::assign_id_to_coalitions(coalition_ids, current_line, id); Ok(Line::new(line_type, continued, coalition)) @@ -100,12 +100,22 @@ impl Line { current_line.as_ref().ends_with('\\') } - fn get_id_from_line>(line: &S) -> Result<&str, ProcessingError> { - Ok(line - .as_ref() - .split_once(',') - .ok_or(ProcessingError::CannotGetIDFromLine)? - .0) + fn get_id_from_line<'a, S>( + line: &'a S, + line_type: &LineType, + ) -> Result<&'a str, ProcessingError> + where + S: AsRef, + { + let local_line = line.as_ref(); + if line_type == &LineType::Destruction { + Ok(&local_line[1..local_line.len()]) + } else { + Ok(local_line + .split_once(',') + .ok_or(ProcessingError::CannotGetIDFromLine)? + .0) + } } fn new(line_type: LineType, continued: bool, coalition: Coalition) -> Self { diff --git a/src/tacview.rs b/src/tacview.rs index 44f2908..838e213 100644 --- a/src/tacview.rs +++ b/src/tacview.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::fmt::Display; #[derive(PartialEq, Clone)] pub enum Coalition { @@ -28,6 +29,21 @@ impl Coalition { } } +impl Display for Coalition { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Blue => "blue".to_string(), + Self::Red => "red".to_string(), + Self::Purple => "purple".to_string(), + _ => unreachable!("We never need these variants"), + } + ) + } +} + pub struct CoalitionIDs<'a> { pub blue: HashSet<&'a str>, pub red: HashSet<&'a str>, diff --git a/src/writer.rs b/src/writer.rs index 7fd8819..354503b 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -6,12 +6,16 @@ use std::io::{Error, Write}; use zip::write::FileOptions; use zip::ZipWriter; -pub fn create_writer(is_zip: bool, filename: &str) -> Result, Box> { +pub fn create_writer( + is_zip: bool, + filename: &str, + coalition: &Coalition, +) -> Result, Box> { let base_name = remove_extension(filename, is_zip); if is_zip { Ok(Box::new(create_zipwriter( - &format!("{base_name}{EXTENSION_ZIP}"), - &format!("{base_name}{EXTENSION_TXT}"), + &format!("{base_name}_{coalition}{EXTENSION_ZIP}"), + &format!("{base_name}_{coalition}{EXTENSION_TXT}"), )?)) } else { Ok(Box::new(create_textwriter(base_name)?)) @@ -31,6 +35,7 @@ fn create_textwriter(filename: &str) -> Result { } fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result, Error> { + println!("{outer_name} {inner_name}"); let mut writer = ZipWriter::new(create_textwriter(outer_name)?); writer.start_file( inner_name,