fix output filename generation

This commit is contained in:
132nd-Professor
2022-08-12 17:23:16 +02:00
parent fd800ce394
commit c1ef22f3a3
5 changed files with 46 additions and 13 deletions
+1
View File
@@ -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
View File
@@ -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(())
+14 -4
View File
@@ -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,13 +100,23 @@ 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()
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 {
Self {
+16
View File
@@ -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
View File
@@ -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,