-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathversion.rs
199 lines (182 loc) · 5.77 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::{point::Format, Error, Feature, Result};
use std::fmt;
/// LAS version.
///
/// Defaults to 1.2.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
/// The major version.
///
/// Should always be 1.
pub major: u8,
/// The minor version.
///
/// Should be between 0 and 4.
pub minor: u8,
}
impl Version {
/// Creates a new version.
///
/// Doesn't do any checking that its an actual las version
///
/// # Examples
///
/// ```
/// use las::Version;
/// let version = Version::new(1, 2);
/// ```
pub fn new(major: u8, minor: u8) -> Version {
Version { major, minor }
}
/// Does this version require the point data start signature?
///
/// Only 1.0 does.
///
/// ```
/// use las::Version;
/// assert!(Version::new(1, 0).requires_point_data_start_signature());
/// assert!(!Version::new(1, 1).requires_point_data_start_signature());
/// ```
pub fn requires_point_data_start_signature(&self) -> bool {
self == &Version::new(1, 0)
}
/// Returns this version's header size.
///
/// # Examples
///
/// ```
/// use las::Version;
/// assert_eq!(227, Version::new(1, 2).header_size());
/// assert_eq!(235, Version::new(1, 3).header_size());
/// assert_eq!(375, Version::new(1, 4).header_size());
/// ```
pub fn header_size(&self) -> u16 {
if self <= &Version::new(1, 2) {
227
} else if self == &Version::new(1, 3) {
235
} else {
375
}
}
/// Checks whether this version supports the feature, returning an error if not.
///
/// # Examples
///
/// ```
/// use las::Version;
/// use las::feature::Waveforms;
/// Version::new(1, 4).verify_support_for::<Waveforms>().unwrap();
/// assert!(Version::new(1, 2).verify_support_for::<Waveforms>().is_err());
/// ```
pub fn verify_support_for<F: Feature>(&self) -> Result<()> {
if self.supports::<F>() {
Ok(())
} else {
Err(Error::UnsupportedFeature {
version: *self,
feature: F::name(),
})
}
}
/// Checks whether this version supports the feature.
///
/// # Examples
///
/// ```
/// use las::Version;
/// use las::feature::Waveforms;
/// assert!(Version::new(1, 4).supports::<Waveforms>());
/// assert!(!Version::new(1, 2).supports::<Waveforms>());
/// ```
pub fn supports<F: Feature>(&self) -> bool {
F::is_supported_by(*self)
}
/// Checks whether this version supports the given point format.
///
/// # Examples
///
/// ```
/// use las::Version;
/// use las::point::Format;
/// let las_1_2 = Version::new(1, 2);
/// let las_1_4 = Version::new(1, 4);
/// assert!(las_1_2.supports_point_format(Format::new(3).unwrap()));
/// assert!(!las_1_2.supports_point_format(Format::new(4).unwrap()));
/// assert!(las_1_4.supports_point_format(Format::new(4).unwrap()));
/// ```
pub fn supports_point_format(&self, format: Format) -> bool {
if self.major != 1 {
return false;
}
match self.minor {
0 | 1 => {
!(format.has_color || format.is_extended || format.has_waveform || format.has_nir)
}
2 => !(format.is_extended || format.has_waveform || format.has_nir),
3 => !(format.is_extended || format.has_nir),
4 => true,
_ => false,
}
}
}
impl Default for Version {
fn default() -> Version {
Version { major: 1, minor: 2 }
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
impl From<(u8, u8)> for Version {
fn from((major, minor): (u8, u8)) -> Version {
Version { major, minor }
}
}
impl From<Version> for (u8, u8) {
fn from(version: Version) -> (u8, u8) {
(version.major, version.minor)
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! version {
($name:ident, $major:expr, $minor:expr, $supports:expr, $max_point_format:expr) => {
mod $name {
use super::*;
use crate::feature::*;
#[test]
fn features() {
let version = Version::new($major, $minor);
assert_eq!($supports[0], version.supports::<FileSourceId>());
assert_eq!($supports[1], version.supports::<GpsStandardTime>());
assert_eq!($supports[2], version.supports::<Waveforms>());
assert_eq!($supports[3], version.supports::<LargeFiles>());
assert_eq!($supports[4], version.supports::<Evlrs>());
}
#[test]
fn point_formats() {
let version = Version::new($major, $minor);
for n in 0i8..11 {
let format = Format::new(n as u8).unwrap();
if n <= $max_point_format {
assert!(version.supports_point_format(format));
} else {
assert!(!version.supports_point_format(format));
}
}
}
}
};
}
version!(las_1_0, 1, 0, [false; 5], 1);
version!(las_1_1, 1, 1, [true, false, false, false, false], 1);
version!(las_1_2, 1, 2, [true, true, false, false, false], 3);
version!(las_1_3, 1, 3, [true, true, true, false, false], 5);
version!(las_1_4, 1, 4, [true; 5], 10);
version!(las_1_5, 1, 5, [false; 5], -1);
version!(las_2_0, 2, 0, [false; 5], -1);
}