Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Bounds::try_from() more generic #18

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,23 @@ on:
pull_request:
schedule: [cron: "45 6 * * *"]

env:
RUST_TOOLCHAIN: stable
TOOLCHAIN_PROFILE: default

name: Run tests
jobs:
lints:
name: Run cargo fmt and cargo clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: ${{ env.TOOLCHAIN_PROFILE }}
toolchain: ${{ env.RUST_TOOLCHAIN }}
override: true
components: rustfmt, clippy
- name: Cache
uses: Swatinem/rust-cache@v1
uses: actions/checkout@v3
- name: Install fmt & clippy
run: rustup component add clippy rustfmt
- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
run: cargo fmt --all -- --check
- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
run: cargo test
- name: Run cargo docs
uses: actions-rs/cargo@v1
run: cargo doc --no-deps
env:
RUSTDOCFLAGS: -D warnings
with:
command: doc
args: --no-deps
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<a name="v0.3.2"></a>
### Pending
*
* Add `Bounds::from` for `[f64; 4]`, `[f32; 4]`, `[i32; 4]`
* Add `Bounds::try_from` now also supports `&[f64]`, `&[f32]`, `&[i32]` in addition to `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.3.2"
description = "Library for serializing the TileJSON file format"
authors = [
"Stepan Kuzmin <[email protected]>",
Expand Down
206 changes: 177 additions & 29 deletions src/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ParseBoundsError::{BadLen, ParseCoordError};
use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use std::fmt::{Display, Formatter};
use std::num::ParseFloatError;
Expand Down Expand Up @@ -148,10 +149,68 @@ pub enum ParseBoundsError {
impl Display for ParseBoundsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ParseBoundsError::BadLen => {
f.write_str("Incorrect number of values. Bounds expects four f64 values.")
}
ParseBoundsError::ParseCoordError(e) => e.fmt(f),
BadLen => f.write_str("Incorrect number of values. Bounds expects four f64 values."),
ParseCoordError(e) => e.fmt(f),
}
}
}

impl From<[f64; 4]> for Bounds {
/// Parse four f64 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::from([1., 2., 3., 4.])
/// );
/// ```
fn from(value: [f64; 4]) -> Self {
Self {
left: value[0],
bottom: value[1],
right: value[2],
top: value[3],
}
}
}

impl From<[f32; 4]> for Bounds {
/// Parse four f32 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::from([1.0f32, 2.0f32, 3.0f32, 4.0f32])
/// );
/// ```
fn from(value: [f32; 4]) -> Self {
Self {
left: value[0] as f64,
bottom: value[1] as f64,
right: value[2] as f64,
top: value[3] as f64,
}
}
}

impl From<[i32; 4]> for Bounds {
/// Parse four i32 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::from([1, 2, 3, 4])
/// );
/// ```
fn from(value: [i32; 4]) -> Self {
Self {
left: value[0] as f64,
bottom: value[1] as f64,
right: value[2] as f64,
top: value[3] as f64,
}
}
}
Expand All @@ -160,17 +219,71 @@ impl TryFrom<Vec<f64>> for Bounds {
type Error = ParseBoundsError;

/// Parse four f64 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::try_from(vec![1., 2., 3., 4.]).unwrap()
/// );
/// ```
fn try_from(value: Vec<f64>) -> Result<Self, Self::Error> {
if value.len() == 4 {
Ok(Self {
left: value[0],
bottom: value[1],
right: value[2],
top: value[3],
})
} else {
Err(ParseBoundsError::BadLen)
}
let arr: [f64; 4] = value.try_into().map_err(|_| BadLen)?;
Ok(arr.into())
}
}

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

/// Parse four f64 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::try_from(vec![1., 2., 3., 4.].as_slice()).unwrap()
/// );
/// ```
fn try_from(value: &[f64]) -> Result<Self, Self::Error> {
let arr: [f64; 4] = value.try_into().map_err(|_| BadLen)?;
Ok(arr.into())
}
}

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

/// Parse four f32 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::try_from(vec![1.0f32, 2.0f32, 3.0f32, 4.0f32].as_slice()).unwrap()
/// );
/// ```
fn try_from(value: &[f32]) -> Result<Self, Self::Error> {
let arr: [f32; 4] = value.try_into().map_err(|_| BadLen)?;
Ok(arr.into())
}
}

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

/// Parse four i32 values as a Bounds value, same order as the [Bounds::new] constructor.
///
/// ```
/// # use tilejson::Bounds;
/// assert_eq!(
/// Bounds::new(1., 2., 3., 4.),
/// Bounds::try_from(vec![1, 2, 3, 4].as_slice()).unwrap()
/// );
/// ```
fn try_from(value: &[i32]) -> Result<Self, Self::Error> {
let arr: [i32; 4] = value.try_into().map_err(|_| BadLen)?;
Ok(arr.into())
}
}

Expand All @@ -188,28 +301,26 @@ impl FromStr for Bounds {
/// assert_eq!(bounds, Bounds::new(-1.0, -2.0, 3.0, 4.0));
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut vals = s.split(',').map(|s| s.trim());
let mut next_val = || {
vals.next().map_or(Err(ParseBoundsError::BadLen), |v| {
v.parse().map_err(ParseBoundsError::ParseCoordError)
})
};
let bounds = Self {
left: next_val()?,
bottom: next_val()?,
right: next_val()?,
top: next_val()?,
};
match vals.next() {
Some(_) => Err(ParseBoundsError::BadLen),
None => Ok(bounds),
let mut values = s.split(',');
let mut result = [0.; 4];
for i in 0..4 {
result[i] = values
.next()
.ok_or(ParseBoundsError::BadLen)?
.trim()
.parse()
.map_err(ParseBoundsError::ParseCoordError)?;
}
values
.next()
.map_or(Ok(result.into()), |_| Err(ParseBoundsError::BadLen))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ParseBoundsError::BadLen;

#[test]
fn test_parse_err() {
Expand All @@ -232,4 +343,41 @@ 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_parse_errors() {
let err = |s| Bounds::from_str(s).unwrap_err();
assert_eq!(err("0,0,0"), BadLen);
assert_eq!(err("0,0,0,0,0"), BadLen);
assert!(matches!(err(""), ParseCoordError(_)));
assert!(matches!(err("a"), ParseCoordError(_)));
assert!(matches!(err("0,0,0,1a"), ParseCoordError(_)));
}

#[test]
fn test_from() -> Result<(), ParseBoundsError> {
let exp = Bounds::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(exp, Bounds::from([1.0, 2.0, 3.0, 4.0]));
assert_eq!(exp, Bounds::try_from([1.0, 2.0, 3.0, 4.0].as_slice())?);
assert_eq!(exp, Bounds::try_from(vec![1.0, 2.0, 3.0, 4.0])?);
let val = vec![1.0, 2.0, 3.0, 4.0];
assert_eq!(exp, Bounds::try_from((&val).as_slice())?);
assert_eq!(exp, Bounds::try_from(val.as_slice())?);

// f32
assert_eq!(exp, Bounds::from([1.0f32, 2.0f32, 3.0f32, 4.0f32]));
let val_array = [1.0f32, 2.0f32, 3.0f32, 4.0f32];
assert_eq!(exp, Bounds::try_from(val_array.as_slice())?);
let val = vec![1.0f32, 2.0f32, 3.0f32, 4.0f32];
assert_eq!(exp, Bounds::try_from((&val).as_slice())?);
assert_eq!(exp, Bounds::try_from(val.as_slice())?);

// i32
assert_eq!(exp, Bounds::from([1, 2, 3, 4]));
assert_eq!(exp, Bounds::try_from([1, 2, 3, 4].as_slice())?);
let val = vec![1, 2, 3, 4];
assert_eq!(exp, Bounds::try_from((&val).as_slice())?);
assert_eq!(exp, Bounds::try_from(val.as_slice())?);
Ok(())
}
}
14 changes: 7 additions & 7 deletions src/tilejson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ mod tests {
]
}"#;

let mut tilejson: TileJSON = serde_json::from_str(&tilejson_str).unwrap();
let mut tilejson: TileJSON = serde_json::from_str(tilejson_str).unwrap();

assert_eq!(
tilejson,
Expand Down Expand Up @@ -388,11 +388,11 @@ mod tests {

#[test]
fn test_bad_json() {
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[]}"#).unwrap_err();
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[1,2]}"#).unwrap_err();
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[1,2,3,4]}"#).unwrap_err();
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[]}"#).unwrap_err();
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[1,2,3]}"#).unwrap_err();
parse(&r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[1,2,3,4,5]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[1,2]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "center":[1,2,3,4]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[1,2,3]}"#).unwrap_err();
parse(r#"{"tilejson":"3.0.0", "tiles":["x"], "bounds":[1,2,3,4,5]}"#).unwrap_err();
}
}