mirror of
https://github.com/132nd-vWing/tacview-splitter.git
synced 2026-08-26 07:48:37 +02:00
fix output filename generation
This commit is contained in:
@@ -10,3 +10,4 @@ zip = {version = ">= 0.6.2", default-features = false, features = ["deflate"]}
|
||||
[profile.release]
|
||||
lto = true
|
||||
opt-level = 3
|
||||
strip = "symbols"
|
||||
|
||||
+4
-3
@@ -18,20 +18,21 @@ fn main() {
|
||||
fn main_inner() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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(())
|
||||
|
||||
+17
-7
@@ -76,7 +76,7 @@ impl Line {
|
||||
current_line: &'a S,
|
||||
coalition_ids: &mut CoalitionIDs<'a>,
|
||||
) -> Result<Self, ProcessingError> {
|
||||
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<S: AsRef<str>>(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<str>,
|
||||
{
|
||||
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 {
|
||||
|
||||
@@ -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>,
|
||||
|
||||
+8
-3
@@ -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<dyn Write>, Box<dyn ErrTrait>> {
|
||||
pub fn create_writer(
|
||||
is_zip: bool,
|
||||
filename: &str,
|
||||
coalition: &Coalition,
|
||||
) -> Result<Box<dyn Write>, Box<dyn ErrTrait>> {
|
||||
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<File, Error> {
|
||||
}
|
||||
|
||||
fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result<ZipWriter<File>, Error> {
|
||||
println!("{outer_name} {inner_name}");
|
||||
let mut writer = ZipWriter::new(create_textwriter(outer_name)?);
|
||||
writer.start_file(
|
||||
inner_name,
|
||||
|
||||
Reference in New Issue
Block a user