Skip to content

Commit

Permalink
Add Serde support behind feature flag (#5)
Browse files Browse the repository at this point in the history
* start adding serde feature

* Finish adding serde derives

* Switch from forbid to deny, false positive is resolved with newer machinery

rust-lang/rust#81670

* ask codecov to ignore the derives

* Fix some clippy warnings from newer rust version
  • Loading branch information
zheylmun authored Aug 29, 2024
1 parent d559e32 commit a1b9d5e
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ name = "jord"
version = "0.15.0"
edition = "2021"
rust-version = "1.65"
authors = ["Cedric Liegeois <[email protected]>",
]
authors = ["Cedric Liegeois <[email protected]>"]
repository = "https://github.com/ofmooseandmen/jord-rs"
documentation = "https://docs.rs/jord"
keywords = ["geo", "geography", "geospatial", "n-vector"]
description = "Geographical Position Calculations (Ellipsoidal and Spherical models)"
license = "MIT"
readme = "README.md"
[features]
serde = ["dep:serde"]

[dependencies]
serde = { version = "1", optional = true, features = ["derive"] }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
1 change: 1 addition & 0 deletions src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{impl_measurement, Measurement};
use std::f64::consts::PI;

#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
/// A one-dimensional angle.
///
/// It primarely exists to unambigously represent an angle as opposed to a bare
Expand Down
1 change: 1 addition & 0 deletions src/ellipsoidal/ellipsoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{

/// An ellipsoid.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Ellipsoid {
equatorial_radius: Length,
polar_radius: Length,
Expand Down
1 change: 1 addition & 0 deletions src/length.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{impl_measurement, Angle, Measurement};

#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
/// A length.
///
/// It primarely exists to unambigously represent a length as opposed to a bare
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#![forbid(
#![deny(
anonymous_parameters,
illegal_floating_point_literal_pattern,
late_bound_lifetime_arguments,
path_statements,
patterns_in_fns_without_body,
Expand Down
18 changes: 11 additions & 7 deletions src/local_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};

#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
enum Orientation {
// x = north (or forward), y = east (or right), z = down.
#[default]
Expand All @@ -18,9 +19,11 @@ enum Orientation {
/// The orientation of the x, y and z axis depends on the [local Cartesian coordinate frame](crate::LocalFrame):
/// - x = north (or forward), y = east (or right), z = down: [NED](crate::LocalFrame::ned), [Body](crate::LocalFrame::body) and [Local Level](crate::LocalFrame::local_level),
/// - x = east, y = north, z = up: [ENU](crate::LocalFrame::enu).
///
/// However, the [azimuth](crate::LocalPositionVector::azimuth) is always relative to 'north' and the elevation is always positive if above the local
/// tangent plane and negative if below.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct LocalPositionVector {
x: Length,
y: Length,
Expand Down Expand Up @@ -185,6 +188,7 @@ impl Cartesian3DVector for LocalPositionVector {
/// calculations are needed in a limited area, position calculations can be performed
/// relative to this system to get approximate horizontal and vertical components
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct LocalFrame<S> {
origin: Vec3,
dir_rm: Mat33,
Expand All @@ -200,7 +204,7 @@ where
/// East-North-Up (local level) frame. This frame is useful for many targeting and tracking applications.
///
/// - Orientation: The x-axis points towards east, the y-axis points towards north (both are
/// horizontal), and the z-axis is pointing up.
/// horizontal), and the z-axis is pointing up.
///
/// See also [NED](crate::LocalFrame::ned)
pub fn enu(origin: GeodeticPos, surface: S) -> Self {
Expand Down Expand Up @@ -228,7 +232,7 @@ where
///
/// - The origin is directly beneath or above the vehicle (B), at Earth’s surface.
/// - Orientation: The x-axis points towards north, the y-axis points towards east (both are
/// horizontal), and the z-axis is pointing down.
/// horizontal), and the z-axis is pointing down.
///
/// Note: When moving relative to the Earth, the frame rotates about its z-axis to allow the
/// x-axis to always point towards north. When getting close to the poles this rotation rate
Expand Down Expand Up @@ -279,10 +283,10 @@ where
///
/// - The origin is directly beneath or above the vehicle (B), at Earth’s surface.
/// - Orientation: The z-axis is pointing down. Initially, the x-axis points towards north, and the
/// y-axis points towards east, but as the vehicle moves they are not rotating about the z-axis
/// (their angular velocity relative to the Earth has zero component along the z-axis).
/// (Note: Any initial horizontal direction of the x- and y-axes is valid for L, but if the
/// initial position is outside the poles, north and east are usually chosen for convenience.)
/// y-axis points towards east, but as the vehicle moves they are not rotating about the z-axis
/// (their angular velocity relative to the Earth has zero component along the z-axis).
/// (Note: Any initial horizontal direction of the x- and y-axes is valid for L, but if the
/// initial position is outside the poles, north and east are usually chosen for convenience.)
///
/// Notes: The L-frame is equal to the N-frame except for the rotation about the z-axis,
/// which is always zero for this frame (relative to Earth). Hence, at a given time, the only
Expand All @@ -304,7 +308,7 @@ where
}

/// Converts the given [GeodeticPos] into a [LocalPositionVector]: the exact vector between this frame
/// origin and the given position. The resulting [LocalPositionVector] orientation is the one of this frame.
/// origin and the given position. The resulting [LocalPositionVector] orientation is the one of this frame.
pub fn geodetic_to_local_pos(&self, p: GeodeticPos) -> LocalPositionVector {
let p_geocentric = self.surface.geodetic_to_geocentric(p).as_metres();
// delta in 'Earth' frame.
Expand Down
1 change: 1 addition & 0 deletions src/mat33.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::Vec3;

/// A 3*3 matrix.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Mat33 {
r0: Vec3,
r1: Vec3,
Expand Down
4 changes: 4 additions & 0 deletions src/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub trait Cartesian3DVector: Sized {

/// A geocentric position or Earth Centred Earth Fixed (ECEF) vector.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct GeocentricPos {
x: Length,
y: Length,
Expand Down Expand Up @@ -104,6 +105,7 @@ impl Cartesian3DVector for GeocentricPos {

/// A geodetic position: the horiztonal coordinates (as a [NVector]) and height above the surface.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct GeodeticPos {
hp: NVector,
height: Length,
Expand All @@ -130,6 +132,7 @@ impl GeodeticPos {

/// An horizontal position represented by a pair of latitude-longitude.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct LatLong {
latitude: Angle,
longitude: Angle,
Expand Down Expand Up @@ -223,6 +226,7 @@ impl LatLong {
/// - z-axis points to the North Pole along the body's rotation axis,
/// - x-axis points towards the point where latitude = longitude = 0
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct NVector(Vec3);

impl NVector {
Expand Down
3 changes: 2 additions & 1 deletion src/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::time::Duration;
use crate::{impl_measurement, Length, Measurement};

#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
/// A speed.
///
/// It primarely exists to unambigously represent a speed as opposed to a bare
Expand Down Expand Up @@ -54,7 +55,7 @@ use crate::{impl_measurement, Length, Measurement};
/// Length::from_nautical_miles(2.0),
/// (Speed::from_knots(1.0) * Duration::from_secs(7200)).round_mm()
/// );
///
///
/// ```
pub struct Speed {
mps: f64,
Expand Down
1 change: 1 addition & 0 deletions src/spherical/cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::Sphere;
/// A [spherical cap](https://en.wikipedia.org/wiki/Spherical_cap): a portion of a sphere cut off by a plane.
/// This struct and implementation is very much based on [S2Cap](https://github.com/google/s2geometry/blob/master/src/s2/s2cap.h).
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Cap {
centre: NVector,
chord_radius2: f64,
Expand Down
1 change: 1 addition & 0 deletions src/spherical/great_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::base::easting;
/// It is internally represented as its normal vector - i.e. the normal vector
/// to the plane containing the great circle.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct GreatCircle {
normal: Vec3,
}
Expand Down
1 change: 1 addition & 0 deletions src/spherical/minor_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::base::{angle_radians_between, exact_side};
/// Oriented minor arc of a great circle between two positions: shortest path between positions
/// on a great circle.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct MinorArc {
start: NVector,
end: NVector,
Expand Down
3 changes: 3 additions & 0 deletions src/spherical/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::MinorArc;
///
/// This struct and implementation is very much based on [S2LatLngRect](https://github.com/google/s2geometry/blob/master/src/s2/s2latlng_rect.h).
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Rectangle {
lat: LatitudeInterval,
lng: LongitudeInterval,
Expand Down Expand Up @@ -317,6 +318,7 @@ impl Rectangle {

/// latitude interval: {@link #lo} is assumed to be less than {@link #hi}, otherwise the interval is empty.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
struct LatitudeInterval {
lo: Angle,
hi: Angle,
Expand Down Expand Up @@ -450,6 +452,7 @@ impl LatitudeInterval {
}

#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
struct LongitudeInterval {
lo: Angle,
hi: Angle,
Expand Down
5 changes: 4 additions & 1 deletion src/spherical/sloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{
/// - [simple](crate::spherical::Loop::is_simple) - this property is not enforced at runtime, therefore operations are undefined on non-simple loops
/// - or, [empty](crate::spherical::Loop::is_empty).
#[derive(PartialEq, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Loop {
/// vertices in clockwise order.
vertices: Vec<Vertex>,
Expand Down Expand Up @@ -531,7 +532,7 @@ impl Loop {
}

/// Triangulates this loop using the [Ear Clipping](https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) method.
///
///
/// This method returns either ([loop number vertices](crate::spherical::Loop::num_vertices) - 2) triangles - as triples of [NVector]s, if
/// the triangulation succeeds, or [empty](Vec::new) if the triangulation fails - which should only occur for [non simple](crate::spherical::Loop::is_simple) loops.
///
Expand Down Expand Up @@ -661,6 +662,7 @@ pub fn is_loop_clockwise(vs: &[NVector]) -> bool {
}

#[derive(PartialEq, Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
enum Classification {
Convex,
Reflex,
Expand All @@ -670,6 +672,7 @@ enum Classification {

/// A vertex of a loop: position + classification.
#[derive(PartialEq, Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
struct Vertex(NVector, Classification);

/// if first == last, returns [first ... last - 1] otherwise returns given array.
Expand Down
1 change: 1 addition & 0 deletions src/spherical/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{
///
/// [Sphere] implements several usefull navigation algorithms.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Sphere {
radius: Length,
}
Expand Down
1 change: 1 addition & 0 deletions src/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// [Vec3] implements many traits, including [Add](::std::ops::Add), [Sub](::std::ops::Sub),
/// [Mul](::std::ops::Mul) and [Div](::std::ops::Div), among others.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Vec3 {
x: f64,
y: f64,
Expand Down
1 change: 1 addition & 0 deletions src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Angle, NVector, Speed};

/// The state of a vehicle: its horizontal position and velocity (bearing and speed).
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Vehicle {
position: NVector,
bearing: Angle,
Expand Down

0 comments on commit a1b9d5e

Please sign in to comment.