Skip to content

Commit cd97a40

Browse files
committed
WIP: Make Bounds::try_from() more generic
## TBD I'm not certain what is the best pattern here - the bounds parsing is nearly identical for converting from a string and from an iterable of numbers. It would also be good to accept both f64 and f32.
1 parent b286beb commit cd97a40

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
<a name="v0.4.0"></a>
12
### Pending
2-
*
3+
* BREAKING: `Bounds::try_from` now accepts a `&[f64]` instead of a `Vec<f64>`
34

45
<a name="v0.3.1"></a>
56
### v0.3.1 (2022-05-29)

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tilejson"
3-
version = "0.3.1"
3+
version = "0.4.0-alpha.1"
44
description = "Library for serializing the TileJSON file format"
55
authors = [
66
"Stepan Kuzmin <to.stepan.kuzmin@gmail.com>",

src/bounds.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ impl Display for ParseBoundsError {
156156
}
157157
}
158158

159-
impl TryFrom<Vec<f64>> for Bounds {
159+
impl TryFrom<&[f64]> for Bounds {
160160
type Error = ParseBoundsError;
161161

162162
/// Parse four f64 values as a Bounds value, same order as the [Bounds::new] constructor.
163-
fn try_from(value: Vec<f64>) -> Result<Self, Self::Error> {
163+
fn try_from(value: &[f64]) -> Result<Self, Self::Error> {
164164
if value.len() == 4 {
165165
Ok(Self {
166166
left: value[0],
@@ -200,9 +200,9 @@ impl FromStr for Bounds {
200200
right: next_val()?,
201201
top: next_val()?,
202202
};
203-
match vals.next() {
204-
Some(_) => Err(ParseBoundsError::BadLen),
205-
None => Ok(bounds),
203+
match next_val() {
204+
Err(ParseBoundsError::BadLen) => Ok(bounds),
205+
_ => Err(ParseBoundsError::BadLen),
206206
}
207207
}
208208
}
@@ -232,4 +232,17 @@ mod tests {
232232
assert_eq!(val("0,0,0,0"), Bounds::new(0.0, 0.0, 0.0, 0.0));
233233
assert_eq!(val(" 1 ,2.0, 3.0, 4.0 "), Bounds::new(1.0, 2.0, 3.0, 4.0));
234234
}
235+
236+
#[test]
237+
fn test_from() {
238+
let expected = Bounds::new(1.0, 2.0, 3.0, 4.0);
239+
assert_eq!(
240+
expected,
241+
Bounds::try_from([1.0, 2.0, 3.0, 4.0].as_slice()).unwrap()
242+
);
243+
assert_eq!(
244+
expected,
245+
Bounds::try_from(vec![1.0, 2.0, 3.0, 4.0].as_slice()).unwrap()
246+
);
247+
}
235248
}

0 commit comments

Comments
 (0)