handle arbitrary data in files

This commit is contained in:
132nd-Professor
2022-08-12 21:55:13 +02:00
parent 43b1bacaab
commit 1cbf11a468
+13 -3
View File
@@ -6,6 +6,7 @@ use crate::tacview::{Coalition, CoalitionIDs};
const COMMENT: char = '#'; const COMMENT: char = '#';
const MINUS: char = '-'; const MINUS: char = '-';
const ZERO: char = '0';
pub fn split_into_header_and_body<S>(mut lines: Vec<S>) -> Result<(Vec<S>, Vec<S>), ProcessingError> pub fn split_into_header_and_body<S>(mut lines: Vec<S>) -> Result<(Vec<S>, Vec<S>), ProcessingError>
where where
@@ -57,12 +58,18 @@ impl Line {
current_line: &'a S, current_line: &'a S,
coalition_ids: &mut CoalitionIDs<'a>, coalition_ids: &mut CoalitionIDs<'a>,
) -> Result<Self, ProcessingError> { ) -> Result<Self, ProcessingError> {
println!("{}", current_line.as_ref());
let line_type = match old_line.continued { let line_type = match old_line.continued {
true => old_line.line_type, true => old_line.line_type,
false => LineType::find_type(current_line)?, false => LineType::find_type(current_line)?,
}; };
match line_type { match line_type {
LineType::ArbitraryData => Ok(Line::new(
line_type,
Self::will_line_continue(current_line),
Coalition::All,
)),
LineType::Telemetry => Ok(Line::from_content(line_type, current_line, coalition_ids)?), LineType::Telemetry => Ok(Line::from_content(line_type, current_line, coalition_ids)?),
LineType::Timestamp => Ok(Line::new(line_type, false, Coalition::All)), LineType::Timestamp => Ok(Line::new(line_type, false, Coalition::All)),
LineType::Destruction => { LineType::Destruction => {
@@ -116,7 +123,7 @@ impl Line {
} else { } else {
Ok(local_line Ok(local_line
.split_once(',') .split_once(',')
.ok_or(ProcessingError::CannotGetIDFromLine)? .ok_or_else(|| ProcessingError::CannotGetIDFromLine(local_line.to_owned()))?
.0) .0)
} }
} }
@@ -142,6 +149,7 @@ enum LineType {
Timestamp, Timestamp,
Destruction, Destruction,
Telemetry, Telemetry,
ArbitraryData,
} }
impl LineType { impl LineType {
@@ -155,6 +163,8 @@ impl LineType {
Ok(Self::Timestamp) Ok(Self::Timestamp)
} else if first_char == MINUS { } else if first_char == MINUS {
Ok(Self::Destruction) Ok(Self::Destruction)
} else if first_char == ZERO {
Ok(LineType::ArbitraryData)
} else { } else {
Ok(LineType::Telemetry) Ok(LineType::Telemetry)
} }
@@ -166,7 +176,7 @@ pub enum ProcessingError {
LineIsEmptyError, LineIsEmptyError,
UnknownLineType, UnknownLineType,
CannotSplitIntoHeaderAndBody, CannotSplitIntoHeaderAndBody,
CannotGetIDFromLine, CannotGetIDFromLine(String),
} }
impl Error for ProcessingError {} impl Error for ProcessingError {}
@@ -179,7 +189,7 @@ impl Display for ProcessingError {
Self::CannotSplitIntoHeaderAndBody => { Self::CannotSplitIntoHeaderAndBody => {
write!(f, "cannot split the file into header and body") write!(f, "cannot split the file into header and body")
} }
Self::CannotGetIDFromLine => write!(f, "cannot get unit ID from line"), Self::CannotGetIDFromLine(l) => write!(f, "cannot get unit ID from line: \n{l}"),
} }
} }
} }