-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from paireks/develop/allow_different_las_formats
Allow different las versions
- Loading branch information
Showing
11 changed files
with
188 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("Invalid LAS version {0}")] | ||
InvalidLasVersion(String), | ||
#[error(transparent)] | ||
UnexpectedError(#[from] anyhow::Error), | ||
} | ||
|
||
pub type Result<T> = core::result::Result<T, Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::{Error, Result}; | ||
|
||
const ALLOWED_VERSIONS: [(u8, u8); 5] = [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]; | ||
|
||
pub struct LasVersion { | ||
major: u8, | ||
minor: u8, | ||
} | ||
|
||
impl LasVersion { | ||
pub fn new(major: u8, minor: u8) -> Result<Self> { | ||
if !ALLOWED_VERSIONS.contains(&(major, minor)) { | ||
return Err(Error::InvalidLasVersion(format!( | ||
"must be between {} and {}", | ||
format_version(ALLOWED_VERSIONS[0]), | ||
format_version(ALLOWED_VERSIONS[4]), | ||
))); | ||
} | ||
|
||
Ok(LasVersion { major, minor }) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for LasVersion { | ||
type Error = Error; | ||
|
||
fn try_from(value: &str) -> std::prelude::v1::Result<Self, Self::Error> { | ||
let parts: Vec<&str> = value.split('.').collect(); | ||
|
||
if parts.len() != 2 { | ||
return Err(Error::InvalidLasVersion( | ||
"expected format `major.minor` (e.g. 1.4)".into(), | ||
)); | ||
} | ||
|
||
let major = parts[0] | ||
.parse::<u8>() | ||
.map_err(|_| Error::InvalidLasVersion("invalid major number".into()))?; | ||
let minor = parts[1] | ||
.parse::<u8>() | ||
.map_err(|_| Error::InvalidLasVersion("invalid minor number".into()))?; | ||
|
||
Self::new(major, minor) | ||
} | ||
} | ||
|
||
impl From<&LasVersion> for las::Version { | ||
fn from(value: &LasVersion) -> Self { | ||
Self { | ||
major: value.major, | ||
minor: value.minor, | ||
} | ||
} | ||
} | ||
|
||
fn format_version((major, minor): (u8, u8)) -> String { | ||
format!("{}.{}", major, minor) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::las_version; | ||
|
||
#[test] | ||
fn test_unsupported_las_version() { | ||
let las_version = las_version::LasVersion::new(2, 3); | ||
|
||
assert!(las_version.is_err()) | ||
} | ||
|
||
#[test] | ||
fn test_invalid_las_version_major() { | ||
let las_version = las_version::LasVersion::try_from("b.4"); | ||
|
||
assert!(las_version.is_err()) | ||
} | ||
|
||
#[test] | ||
fn test_invalid_las_version_minor() { | ||
let las_version = las_version::LasVersion::try_from("2.c"); | ||
|
||
assert!(las_version.is_err()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.