Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed a few unwrap() calls to avoid panics if the file is malformed #2

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions cggtts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ pub enum Error {
NonAsciiData(#[from] CrcError),
#[error("checksum error, got \"{0}\" but \"{1}\" locally computed")]
ChecksumError(u8, u8),
#[error("file format error")]
FormatError,
}

impl Default for Cggtts {
Expand Down Expand Up @@ -632,17 +634,15 @@ impl Cggtts {

/// Builds Self from given `Cggtts` file.
pub fn from_file (fp: &str) -> Result<Self, Error> {
let file_content = std::fs::read_to_string(fp)
.unwrap();
let file_content = std::fs::read_to_string(fp)?;
let mut lines = file_content.split("\n")
.map(|x| x.to_string())
//.map(|x| x.to_string() +"\n")
//.map(|x| x.to_string() +"\r"+"\n")
.into_iter();

// init variables
let mut line = lines.next()
.unwrap();
let mut line = lines.next().ok_or(Error::VersionFormatError)?;
let mut system_delay = SystemDelay::new();

// VERSION must be first
Expand All @@ -668,16 +668,15 @@ impl Cggtts {
let mut time_reference = TimeSystem::default();
let (mut x, mut y, mut z) : (f64,f64,f64) = (0.0, 0.0, 0.0);

line = lines.next()
.unwrap();
line = lines.next().ok_or(Error::FormatError)?;

loop {
if line.starts_with("REV DATE = ") {
match scan_fmt! (&line, "REV DATE = {d} {d} {d}", i32, u32, u32) {
(Some(year),
Some(month),
Some(day)) => {
rev_date = chrono::NaiveDate::from_ymd(year, month, day);
rev_date = chrono::NaiveDate::from_ymd_opt(year, month, day).ok_or(Error::RevisionDateParsingError)?;
},
_ => {}
}
Expand Down Expand Up @@ -784,8 +783,8 @@ impl Cggtts {
}

match items[0] {
"CAB" => system_delay.rf_cable_delay = f64::from_str(items[3]).unwrap(),
"REF" => system_delay.ref_delay = f64::from_str(items[3]).unwrap(),
"CAB" => system_delay.rf_cable_delay = f64::from_str(items[3])?,
"REF" => system_delay.ref_delay = f64::from_str(items[3])?,
"SYS" => {
if line.contains("CAL_ID") {
let offset = line.rfind("=").unwrap();
Expand Down Expand Up @@ -922,9 +921,9 @@ impl Cggtts {
}

// BLANKS
let _ = lines.next().unwrap(); // Blank
let _ = lines.next().unwrap(); // labels
let _ = lines.next().unwrap(); // units currently discarded
let _ = lines.next(); // Blank
let _ = lines.next(); // labels
let _ = lines.next(); // units currently discarded
// tracks parsing
let mut tracks: Vec<Track> = Vec::new();
loop {
Expand Down