Skip to content

Commit

Permalink
feat: upgrade version when reading when needed
Browse files Browse the repository at this point in the history
Allows for more permissive reading
  • Loading branch information
gadomski committed May 30, 2024
1 parent 4eb5e12 commit c960370
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ edition = "2021"
[dependencies]
byteorder = "1.4"
chrono = "0.4"
laz = { version = "0.9.1", optional = true }
log = "0.4"
num-traits = "0.2"
thiserror = "1.0"
uuid = "1"
laz = { version = "0.9.1", optional = true }

[dev-dependencies]
criterion = "0.5"
Expand Down
22 changes: 22 additions & 0 deletions src/header/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,28 @@ impl Builder {
};
Ok(header)
}

/// Returns the minimum supported version for this builder, as determined by its features.
///
/// # Examples
///
/// ```
/// use las::{Builder, Version};
///
/// assert_eq!(Builder::default().minimum_supported_version().unwrap(), Version::new(1, 0));
/// ```
pub fn minimum_supported_version(&self) -> Option<Version> {
// TODO can we make a validity check that doesn't involve a full
// conversion into a header, without duplicating a lot of logic?
for minor in [0, 1, 2, 3, 4] {
let mut builder = self.clone();
builder.version.minor = minor;
if builder.into_header().is_ok() {
return Some(Version::new(1, minor));
}
}
None
}
}

impl<V: Into<Version>> From<V> for Builder {
Expand Down
10 changes: 10 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ impl<'a> Reader<'a> {

let _ = read.seek(SeekFrom::Start(offset_to_point_data))?;

if let Some(version) = builder.minimum_supported_version() {
if version > builder.version {
log::warn!(
"upgrading las version to {} (from {})",
version,
builder.version
);
builder.version = version;
}
}
let header = builder.into_header()?;

#[cfg(feature = "laz")]
Expand Down
Binary file added tests/data/32-1-472-150-76.laz
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/permissive_reading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
#[cfg(feature = "laz")]
fn read_invalid_file() {
// https://github.com/gadomski/las-rs/pull/61
use las::Read;

let mut reader = las::Reader::from_path("tests/data/32-1-472-150-76.laz").unwrap();
let _ = reader.points().next().unwrap().unwrap();
}

0 comments on commit c960370

Please sign in to comment.