mirror of
https://github.com/132nd-vWing/tacview-splitter.git
synced 2026-08-26 08:48:38 +02:00
fix clippy warnings
This commit is contained in:
+38
-28
@@ -1,6 +1,6 @@
|
||||
pub mod lib {
|
||||
use std::io::Write;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
|
||||
use zip;
|
||||
|
||||
@@ -8,7 +8,7 @@ pub mod lib {
|
||||
const ERR_CANNOT_OPEN_OUTPUT: &str = "Could not open output file";
|
||||
const ERR_CANNOT_BEGIN_FILE: &str = "Could not begin file in zip archive";
|
||||
|
||||
pub struct IDs<'a> {
|
||||
pub struct IDs<'a> {
|
||||
pub blue: Vec<&'a str>,
|
||||
pub red: Vec<&'a str>,
|
||||
pub violet: Vec<&'a str>,
|
||||
@@ -51,18 +51,18 @@ pub mod lib {
|
||||
impl<T: Write> Handling for Descriptors<T> {
|
||||
fn write(&mut self, header: Vec<String>, bodies_by_coalition: BodiesByCoalition) {
|
||||
for line in &header {
|
||||
write!(self.blue, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
write!(self.red, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
write!(self.violet, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.blue, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.red, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.violet, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.blue {
|
||||
write!(self.blue, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.blue, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.red {
|
||||
write!(self.red, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.red, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.violet {
|
||||
write!(self.violet, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
writeln!(self.violet, "{}", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,49 +72,57 @@ pub mod lib {
|
||||
let blue = fs::File::create(&filenames.txt.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let red = fs::File::create(&filenames.txt.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let violet = fs::File::create(&filenames.txt.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let descriptors = Descriptors { blue, red, violet };
|
||||
return descriptors
|
||||
Descriptors { blue, red, violet }
|
||||
}
|
||||
}
|
||||
|
||||
impl Descriptors<zip::ZipWriter<fs::File>> {
|
||||
pub fn new(filenames: OutputFilenames) -> Descriptors<zip::ZipWriter<fs::File>> {
|
||||
let options = zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
let options = zip::write::FileOptions::default()
|
||||
.compression_method(zip::CompressionMethod::Deflated);
|
||||
|
||||
let file = fs::File::create(&filenames.zip.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut blue = zip::ZipWriter::new(file);
|
||||
blue.start_file(&filenames.txt.blue, options).expect(ERR_CANNOT_BEGIN_FILE);
|
||||
blue.start_file(&filenames.txt.blue, options)
|
||||
.expect(ERR_CANNOT_BEGIN_FILE);
|
||||
|
||||
let file = fs::File::create(&filenames.zip.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut red = zip::ZipWriter::new(file);
|
||||
red.start_file(&filenames.txt.red, options).expect(ERR_CANNOT_BEGIN_FILE);
|
||||
red.start_file(&filenames.txt.red, options)
|
||||
.expect(ERR_CANNOT_BEGIN_FILE);
|
||||
|
||||
let file = fs::File::create(&filenames.zip.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut violet = zip::ZipWriter::new(file);
|
||||
violet.start_file(&filenames.txt.violet, options).expect(ERR_CANNOT_BEGIN_FILE);
|
||||
violet
|
||||
.start_file(&filenames.txt.violet, options)
|
||||
.expect(ERR_CANNOT_BEGIN_FILE);
|
||||
|
||||
let descriptors = Descriptors{blue, red, violet};
|
||||
descriptors
|
||||
Descriptors { blue, red, violet }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sanity_check_output_filenames(input_filename: &String, output_filenames: &FilenamesVariant) {
|
||||
if input_filename == &output_filenames.blue ||
|
||||
input_filename == &output_filenames.red ||
|
||||
input_filename == &output_filenames.violet {
|
||||
pub fn sanity_check_output_filenames(
|
||||
input_filename: &String,
|
||||
output_filenames: &FilenamesVariant,
|
||||
) {
|
||||
if input_filename == &output_filenames.blue
|
||||
|| input_filename == &output_filenames.red
|
||||
|| input_filename == &output_filenames.violet
|
||||
{
|
||||
panic!("Output filenames were the same as input filenames")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_output_filenames_individual(input_filename: &String, old_extension: &str, new_extension: &str, coalition: &str) -> String {
|
||||
pub fn get_output_filenames_individual(
|
||||
input_filename: &str,
|
||||
old_extension: &str,
|
||||
new_extension: &str,
|
||||
coalition: &str,
|
||||
) -> String {
|
||||
let mut output_extension = coalition.to_owned();
|
||||
output_extension.push_str(new_extension);
|
||||
let output_filename = input_filename.replace(old_extension, &output_extension);
|
||||
output_filename
|
||||
input_filename.replace(old_extension, &output_extension)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -125,12 +133,14 @@ mod tests {
|
||||
let input_filename = "something.txt.acmi".to_string();
|
||||
let extension = ".txt.acmi";
|
||||
let coalition = "_blue";
|
||||
let result = get_output_filenames_individual(&input_filename, extension, extension, coalition);
|
||||
let result =
|
||||
get_output_filenames_individual(&input_filename, extension, extension, coalition);
|
||||
let correct_result = "something_blue.txt.acmi";
|
||||
assert_eq!(result, correct_result);
|
||||
|
||||
let extension_wrong = ".tXT.acmi";
|
||||
let result = get_output_filenames_individual(&input_filename, extension_wrong, extension, coalition);
|
||||
let result =
|
||||
get_output_filenames_individual(&input_filename, extension_wrong, extension, coalition);
|
||||
assert_ne!(result, correct_result);
|
||||
}
|
||||
}
|
||||
|
||||
+72
-57
@@ -1,7 +1,5 @@
|
||||
use std::{fs, str};
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
use zip;
|
||||
use std::{fs, str};
|
||||
|
||||
use tacview_splitter::lib;
|
||||
use tacview_splitter::lib::Handling;
|
||||
@@ -17,7 +15,7 @@ enum LineType {
|
||||
Unknown,
|
||||
Timestamp,
|
||||
Destruction,
|
||||
Telemetry
|
||||
Telemetry,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -38,21 +36,30 @@ fn main() {
|
||||
}
|
||||
|
||||
fn split_into_header_and_body(lines: Vec<String>) -> (Vec<String>, Vec<String>) {
|
||||
let mut i=0;
|
||||
let mut i = 0;
|
||||
for line in &lines {
|
||||
if line.chars().nth(0).expect("malformed line") == COMMENT {
|
||||
break
|
||||
if line.chars().next().expect("malformed line") == COMMENT {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return (lines[..i].to_vec(), lines[i..].to_vec());
|
||||
(lines[..i].to_vec(), lines[i..].to_vec())
|
||||
}
|
||||
|
||||
fn divide_body_by_coalition(body: &Vec<String>) -> lib::BodiesByCoalition {
|
||||
let mut bbc = lib::BodiesByCoalition{blue: Vec::new(), red: Vec::new(), violet: Vec::new()};
|
||||
let mut bbc = lib::BodiesByCoalition {
|
||||
blue: Vec::new(),
|
||||
red: Vec::new(),
|
||||
violet: Vec::new(),
|
||||
};
|
||||
let mut continued = false;
|
||||
let mut line_type = LineType::Unknown;
|
||||
let mut coalitions = lib::IDs{blue: Vec::new(), red: Vec::new(), violet: Vec::new(), unknown: Vec::new()};
|
||||
let mut coalitions = lib::IDs {
|
||||
blue: Vec::new(),
|
||||
red: Vec::new(),
|
||||
violet: Vec::new(),
|
||||
unknown: Vec::new(),
|
||||
};
|
||||
for line in body {
|
||||
let result = process_line(continued, &mut coalitions, line, line_type);
|
||||
line_type = result.0;
|
||||
@@ -62,7 +69,8 @@ fn divide_body_by_coalition(body: &Vec<String>) -> lib::BodiesByCoalition {
|
||||
bbc.blue.push(line);
|
||||
bbc.red.push(line);
|
||||
bbc.violet.push(line);
|
||||
} else { // destruction or telemetry
|
||||
} else {
|
||||
// destruction or telemetry
|
||||
if coalitions.blue.contains(&id) {
|
||||
bbc.blue.push(line);
|
||||
} else if coalitions.red.contains(&id) {
|
||||
@@ -75,7 +83,12 @@ fn divide_body_by_coalition(body: &Vec<String>) -> lib::BodiesByCoalition {
|
||||
bbc
|
||||
}
|
||||
|
||||
fn process_line<'a>(continued: bool, coalitions: &mut lib::IDs<'a>, line: &'a String, last_line_type: LineType) -> (LineType, bool, &'a str) {
|
||||
fn process_line<'a>(
|
||||
continued: bool,
|
||||
coalitions: &mut lib::IDs<'a>,
|
||||
line: &'a str,
|
||||
last_line_type: LineType,
|
||||
) -> (LineType, bool, &'a str) {
|
||||
let mut id = "";
|
||||
let line_type;
|
||||
if !continued {
|
||||
@@ -91,27 +104,20 @@ fn process_line<'a>(continued: bool, coalitions: &mut lib::IDs<'a>, line: &'a St
|
||||
(line_type, line_will_continue, id)
|
||||
}
|
||||
|
||||
fn will_line_continue(line: &String) -> bool {
|
||||
let line_will_continue: bool;
|
||||
if line.ends_with("\\") {
|
||||
line_will_continue = true;
|
||||
} else {
|
||||
line_will_continue = false;
|
||||
}
|
||||
line_will_continue
|
||||
fn will_line_continue(line: &str) -> bool {
|
||||
line.ends_with('\\')
|
||||
}
|
||||
|
||||
fn get_id_from_line(line: &String) -> &str {
|
||||
fn get_id_from_line(line: &str) -> &str {
|
||||
let result = line.split_once(',');
|
||||
let split = match result {
|
||||
Some(t) => t,
|
||||
None => panic!("Could not get ID from line!")
|
||||
None => panic!("Could not get ID from line!"),
|
||||
};
|
||||
let id = split.0;
|
||||
id
|
||||
split.0 as _
|
||||
}
|
||||
|
||||
fn assign_id_to_coalitions<'a>(coalitions: &mut lib::IDs<'a>, line: &'a String, id: &'a str) {
|
||||
fn assign_id_to_coalitions<'a>(coalitions: &mut lib::IDs<'a>, line: &'a str, id: &'a str) {
|
||||
if line.contains("Color=") {
|
||||
if line.contains("Color=Blue") {
|
||||
coalitions.blue.push(id);
|
||||
@@ -125,18 +131,15 @@ fn assign_id_to_coalitions<'a>(coalitions: &mut lib::IDs<'a>, line: &'a String,
|
||||
}
|
||||
}
|
||||
|
||||
fn determine_line_type(line: &String) -> LineType {
|
||||
let line_type: LineType;
|
||||
|
||||
let first_char = line.chars().nth(0).expect("malformed line");
|
||||
fn determine_line_type(line: &str) -> LineType {
|
||||
let first_char = line.chars().next().expect("malformed line");
|
||||
if first_char == COMMENT {
|
||||
line_type = LineType::Timestamp;
|
||||
LineType::Timestamp
|
||||
} else if first_char == MINUS {
|
||||
line_type = LineType::Destruction;
|
||||
LineType::Destruction
|
||||
} else {
|
||||
line_type = LineType::Telemetry;
|
||||
LineType::Telemetry
|
||||
}
|
||||
return line_type
|
||||
}
|
||||
|
||||
fn get_output_filenames(input_filename: &String, is_zip: bool) -> lib::OutputFilenames {
|
||||
@@ -144,36 +147,45 @@ fn get_output_filenames(input_filename: &String, is_zip: bool) -> lib::OutputFil
|
||||
let output_filenames_txt: lib::FilenamesVariant;
|
||||
|
||||
if is_zip {
|
||||
output_filenames_zip = get_output_filenames_for_extension(
|
||||
input_filename, EXTENSION_ZIP, EXTENSION_ZIP
|
||||
);
|
||||
output_filenames_txt = get_output_filenames_for_extension(
|
||||
input_filename, EXTENSION_ZIP, EXTENSION_TXT
|
||||
);
|
||||
output_filenames_zip =
|
||||
get_output_filenames_for_extension(input_filename, EXTENSION_ZIP, EXTENSION_ZIP);
|
||||
output_filenames_txt =
|
||||
get_output_filenames_for_extension(input_filename, EXTENSION_ZIP, EXTENSION_TXT);
|
||||
} else {
|
||||
output_filenames_zip = get_output_filenames_dummy();
|
||||
output_filenames_txt = get_output_filenames_for_extension(
|
||||
input_filename, EXTENSION_TXT, EXTENSION_TXT
|
||||
);
|
||||
output_filenames_txt =
|
||||
get_output_filenames_for_extension(input_filename, EXTENSION_TXT, EXTENSION_TXT);
|
||||
}
|
||||
|
||||
let output_filenames = lib::OutputFilenames{txt: output_filenames_txt, zip: output_filenames_zip};
|
||||
output_filenames
|
||||
lib::OutputFilenames {
|
||||
txt: output_filenames_txt,
|
||||
zip: output_filenames_zip,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_output_filenames_dummy() -> lib::FilenamesVariant {
|
||||
let blue = "".to_string();
|
||||
let red = "".to_string();
|
||||
let violet = "".to_string();
|
||||
let output_filenames = lib::FilenamesVariant{blue, red, violet};
|
||||
output_filenames
|
||||
lib::FilenamesVariant { blue, red, violet }
|
||||
}
|
||||
|
||||
fn get_output_filenames_for_extension(input_filename: &String, old_extension: &str, new_extension: &str) -> lib::FilenamesVariant {
|
||||
let blue = lib::get_output_filenames_individual(input_filename, old_extension, new_extension, "_blue");
|
||||
let red = lib::get_output_filenames_individual(input_filename, old_extension, new_extension,"_red");
|
||||
let violet = lib::get_output_filenames_individual(input_filename, old_extension, new_extension,"_violet");
|
||||
let output_filenames_zip = lib::FilenamesVariant{blue, red, violet};
|
||||
fn get_output_filenames_for_extension(
|
||||
input_filename: &String,
|
||||
old_extension: &str,
|
||||
new_extension: &str,
|
||||
) -> lib::FilenamesVariant {
|
||||
let blue =
|
||||
lib::get_output_filenames_individual(input_filename, old_extension, new_extension, "_blue");
|
||||
let red =
|
||||
lib::get_output_filenames_individual(input_filename, old_extension, new_extension, "_red");
|
||||
let violet = lib::get_output_filenames_individual(
|
||||
input_filename,
|
||||
old_extension,
|
||||
new_extension,
|
||||
"_violet",
|
||||
);
|
||||
let output_filenames_zip = lib::FilenamesVariant { blue, red, violet };
|
||||
lib::sanity_check_output_filenames(input_filename, &output_filenames_zip);
|
||||
output_filenames_zip
|
||||
}
|
||||
@@ -189,27 +201,30 @@ fn find_input_file() -> (String, bool) {
|
||||
} else if filename.ends_with(EXTENSION_ZIP) {
|
||||
return (filename, true);
|
||||
}
|
||||
};
|
||||
}
|
||||
println!("No tacview input file found in current directory.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
|
||||
fn read_data(filename: &String, is_zip: bool) -> Vec<String> {
|
||||
let file = fs::File::open(filename).expect("Could not read from input file");
|
||||
let buf = BufReader::new(file);
|
||||
return if is_zip {
|
||||
let mut archive = zip::ZipArchive::new(buf).expect("Could not read zip data");
|
||||
let inner_file = archive.by_index(0).expect("Could not read telemetry file from zip archive");
|
||||
let inner_file = archive
|
||||
.by_index(0)
|
||||
.expect("Could not read telemetry file from zip archive");
|
||||
let inner_buf = BufReader::new(inner_file);
|
||||
let lines: Vec<String> = inner_buf.lines()
|
||||
let lines: Vec<String> = inner_buf
|
||||
.lines()
|
||||
.map(|l| l.expect("Could not parse line"))
|
||||
.collect();
|
||||
lines
|
||||
} else {
|
||||
let lines: Vec<String> = buf.lines()
|
||||
let lines: Vec<String> = buf
|
||||
.lines()
|
||||
.map(|l| l.expect("Could not parse line"))
|
||||
.collect();
|
||||
lines
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user