Skip to content

Commit

Permalink
WIP: Make Bounds::try_from() more generic
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
nyurik committed May 29, 2022
1 parent b286beb commit cd97a40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<a name="v0.4.0"></a>
### Pending
*
* BREAKING: `Bounds::try_from` now accepts a `&[f64]` instead of a `Vec<f64>`

<a name="v0.3.1"></a>
### v0.3.1 (2022-05-29)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tilejson"
version = "0.3.1"
version = "0.4.0-alpha.1"
description = "Library for serializing the TileJSON file format"
authors = [
"Stepan Kuzmin <[email protected]>",
Expand Down
23 changes: 18 additions & 5 deletions src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ impl Display for ParseBoundsError {
}
}

impl TryFrom<Vec<f64>> for Bounds {
impl TryFrom<&[f64]> for Bounds {
type Error = ParseBoundsError;

/// Parse four f64 values as a Bounds value, same order as the [Bounds::new] constructor.
fn try_from(value: Vec<f64>) -> Result<Self, Self::Error> {
fn try_from(value: &[f64]) -> Result<Self, Self::Error> {
if value.len() == 4 {
Ok(Self {
left: value[0],
Expand Down Expand Up @@ -200,9 +200,9 @@ impl FromStr for Bounds {
right: next_val()?,
top: next_val()?,
};
match vals.next() {
Some(_) => Err(ParseBoundsError::BadLen),
None => Ok(bounds),
match next_val() {
Err(ParseBoundsError::BadLen) => Ok(bounds),
_ => Err(ParseBoundsError::BadLen),
}
}
}
Expand Down Expand Up @@ -232,4 +232,17 @@ mod tests {
assert_eq!(val("0,0,0,0"), Bounds::new(0.0, 0.0, 0.0, 0.0));
assert_eq!(val(" 1 ,2.0, 3.0, 4.0 "), Bounds::new(1.0, 2.0, 3.0, 4.0));
}

#[test]
fn test_from() {
let expected = Bounds::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(
expected,
Bounds::try_from([1.0, 2.0, 3.0, 4.0].as_slice()).unwrap()
);
assert_eq!(
expected,
Bounds::try_from(vec![1.0, 2.0, 3.0, 4.0].as_slice()).unwrap()
);
}
}

0 comments on commit cd97a40

Please sign in to comment.