Skip to content

Commit a1b9d5e

Browse files
authored
Add Serde support behind feature flag (#5)
* 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
1 parent d559e32 commit a1b9d5e

File tree

17 files changed

+39
-13
lines changed

17 files changed

+39
-13
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ name = "jord"
33
version = "0.15.0"
44
edition = "2021"
55
rust-version = "1.65"
6-
authors = ["Cedric Liegeois <[email protected]>",
7-
]
6+
authors = ["Cedric Liegeois <[email protected]>"]
87
repository = "https://github.com/ofmooseandmen/jord-rs"
98
documentation = "https://docs.rs/jord"
109
keywords = ["geo", "geography", "geospatial", "n-vector"]
1110
description = "Geographical Position Calculations (Ellipsoidal and Spherical models)"
1211
license = "MIT"
1312
readme = "README.md"
13+
[features]
14+
serde = ["dep:serde"]
1415

1516
[dependencies]
17+
serde = { version = "1", optional = true, features = ["derive"] }
1618

1719
[dev-dependencies]
1820
criterion = { version = "0.5.1", features = ["html_reports"] }

src/angle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{impl_measurement, Measurement};
22
use std::f64::consts::PI;
33

44
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
56
/// A one-dimensional angle.
67
///
78
/// It primarely exists to unambigously represent an angle as opposed to a bare

src/ellipsoidal/ellipsoid.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44

55
/// An ellipsoid.
66
#[derive(PartialEq, Clone, Copy, Debug, Default)]
7+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
78
pub struct Ellipsoid {
89
equatorial_radius: Length,
910
polar_radius: Length,

src/length.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{impl_measurement, Angle, Measurement};
22

33
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
4+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
45
/// A length.
56
///
67
/// It primarely exists to unambigously represent a length as opposed to a bare

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
2-
#![forbid(
2+
#![deny(
33
anonymous_parameters,
4-
illegal_floating_point_literal_pattern,
54
late_bound_lifetime_arguments,
65
path_statements,
76
patterns_in_fns_without_body,

src/local_frame.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
};
55

66
#[derive(PartialEq, Clone, Copy, Debug, Default)]
7+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
78
enum Orientation {
89
// x = north (or forward), y = east (or right), z = down.
910
#[default]
@@ -18,9 +19,11 @@ enum Orientation {
1819
/// The orientation of the x, y and z axis depends on the [local Cartesian coordinate frame](crate::LocalFrame):
1920
/// - 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),
2021
/// - x = east, y = north, z = up: [ENU](crate::LocalFrame::enu).
22+
///
2123
/// However, the [azimuth](crate::LocalPositionVector::azimuth) is always relative to 'north' and the elevation is always positive if above the local
2224
/// tangent plane and negative if below.
2325
#[derive(PartialEq, Clone, Copy, Debug, Default)]
26+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
2427
pub struct LocalPositionVector {
2528
x: Length,
2629
y: Length,
@@ -185,6 +188,7 @@ impl Cartesian3DVector for LocalPositionVector {
185188
/// calculations are needed in a limited area, position calculations can be performed
186189
/// relative to this system to get approximate horizontal and vertical components
187190
#[derive(PartialEq, Clone, Copy, Debug, Default)]
191+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
188192
pub struct LocalFrame<S> {
189193
origin: Vec3,
190194
dir_rm: Mat33,
@@ -200,7 +204,7 @@ where
200204
/// East-North-Up (local level) frame. This frame is useful for many targeting and tracking applications.
201205
///
202206
/// - Orientation: The x-axis points towards east, the y-axis points towards north (both are
203-
/// horizontal), and the z-axis is pointing up.
207+
/// horizontal), and the z-axis is pointing up.
204208
///
205209
/// See also [NED](crate::LocalFrame::ned)
206210
pub fn enu(origin: GeodeticPos, surface: S) -> Self {
@@ -228,7 +232,7 @@ where
228232
///
229233
/// - The origin is directly beneath or above the vehicle (B), at Earth’s surface.
230234
/// - Orientation: The x-axis points towards north, the y-axis points towards east (both are
231-
/// horizontal), and the z-axis is pointing down.
235+
/// horizontal), and the z-axis is pointing down.
232236
///
233237
/// Note: When moving relative to the Earth, the frame rotates about its z-axis to allow the
234238
/// x-axis to always point towards north. When getting close to the poles this rotation rate
@@ -279,10 +283,10 @@ where
279283
///
280284
/// - The origin is directly beneath or above the vehicle (B), at Earth’s surface.
281285
/// - Orientation: The z-axis is pointing down. Initially, the x-axis points towards north, and the
282-
/// y-axis points towards east, but as the vehicle moves they are not rotating about the z-axis
283-
/// (their angular velocity relative to the Earth has zero component along the z-axis).
284-
/// (Note: Any initial horizontal direction of the x- and y-axes is valid for L, but if the
285-
/// initial position is outside the poles, north and east are usually chosen for convenience.)
286+
/// y-axis points towards east, but as the vehicle moves they are not rotating about the z-axis
287+
/// (their angular velocity relative to the Earth has zero component along the z-axis).
288+
/// (Note: Any initial horizontal direction of the x- and y-axes is valid for L, but if the
289+
/// initial position is outside the poles, north and east are usually chosen for convenience.)
286290
///
287291
/// Notes: The L-frame is equal to the N-frame except for the rotation about the z-axis,
288292
/// which is always zero for this frame (relative to Earth). Hence, at a given time, the only
@@ -304,7 +308,7 @@ where
304308
}
305309

306310
/// Converts the given [GeodeticPos] into a [LocalPositionVector]: the exact vector between this frame
307-
/// origin and the given position. The resulting [LocalPositionVector] orientation is the one of this frame.
311+
/// origin and the given position. The resulting [LocalPositionVector] orientation is the one of this frame.
308312
pub fn geodetic_to_local_pos(&self, p: GeodeticPos) -> LocalPositionVector {
309313
let p_geocentric = self.surface.geodetic_to_geocentric(p).as_metres();
310314
// delta in 'Earth' frame.

src/mat33.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::Vec3;
22

33
/// A 3*3 matrix.
44
#[derive(PartialEq, Clone, Copy, Debug, Default)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
56
pub struct Mat33 {
67
r0: Vec3,
78
r1: Vec3,

src/positions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub trait Cartesian3DVector: Sized {
5151

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

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

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

228232
impl NVector {

src/speed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::time::Duration;
33
use crate::{impl_measurement, Length, Measurement};
44

55
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug, Default)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
67
/// A speed.
78
///
89
/// It primarely exists to unambigously represent a speed as opposed to a bare
@@ -54,7 +55,7 @@ use crate::{impl_measurement, Length, Measurement};
5455
/// Length::from_nautical_miles(2.0),
5556
/// (Speed::from_knots(1.0) * Duration::from_secs(7200)).round_mm()
5657
/// );
57-
///
58+
///
5859
/// ```
5960
pub struct Speed {
6061
mps: f64,

src/spherical/cap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::Sphere;
77
/// A [spherical cap](https://en.wikipedia.org/wiki/Spherical_cap): a portion of a sphere cut off by a plane.
88
/// This struct and implementation is very much based on [S2Cap](https://github.com/google/s2geometry/blob/master/src/s2/s2cap.h).
99
#[derive(PartialEq, Clone, Copy, Debug, Default)]
10+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
1011
pub struct Cap {
1112
centre: NVector,
1213
chord_radius2: f64,

src/spherical/great_circle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::base::easting;
99
/// It is internally represented as its normal vector - i.e. the normal vector
1010
/// to the plane containing the great circle.
1111
#[derive(PartialEq, Clone, Copy, Debug, Default)]
12+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
1213
pub struct GreatCircle {
1314
normal: Vec3,
1415
}

src/spherical/minor_arc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::base::{angle_radians_between, exact_side};
88
/// Oriented minor arc of a great circle between two positions: shortest path between positions
99
/// on a great circle.
1010
#[derive(PartialEq, Clone, Copy, Debug, Default)]
11+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
1112
pub struct MinorArc {
1213
start: NVector,
1314
end: NVector,

src/spherical/rectangle.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use super::MinorArc;
1111
///
1212
/// This struct and implementation is very much based on [S2LatLngRect](https://github.com/google/s2geometry/blob/master/src/s2/s2latlng_rect.h).
1313
#[derive(PartialEq, Clone, Copy, Debug, Default)]
14+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
1415
pub struct Rectangle {
1516
lat: LatitudeInterval,
1617
lng: LongitudeInterval,
@@ -317,6 +318,7 @@ impl Rectangle {
317318

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

452454
#[derive(PartialEq, Clone, Copy, Debug, Default)]
455+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
453456
struct LongitudeInterval {
454457
lo: Angle,
455458
hi: Angle,

src/spherical/sloop.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::{
1717
/// - [simple](crate::spherical::Loop::is_simple) - this property is not enforced at runtime, therefore operations are undefined on non-simple loops
1818
/// - or, [empty](crate::spherical::Loop::is_empty).
1919
#[derive(PartialEq, Clone, Debug, Default)]
20+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
2021
pub struct Loop {
2122
/// vertices in clockwise order.
2223
vertices: Vec<Vertex>,
@@ -531,7 +532,7 @@ impl Loop {
531532
}
532533

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

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

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

675678
/// if first == last, returns [first ... last - 1] otherwise returns given array.

src/spherical/sphere.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::{
1414
///
1515
/// [Sphere] implements several usefull navigation algorithms.
1616
#[derive(PartialEq, Clone, Copy, Debug, Default)]
17+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
1718
pub struct Sphere {
1819
radius: Length,
1920
}

src/vec3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// [Vec3] implements many traits, including [Add](::std::ops::Add), [Sub](::std::ops::Sub),
44
/// [Mul](::std::ops::Mul) and [Div](::std::ops::Div), among others.
55
#[derive(PartialEq, Clone, Copy, Debug, Default)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
67
pub struct Vec3 {
78
x: f64,
89
y: f64,

src/vehicle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{Angle, NVector, Speed};
22

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

0 commit comments

Comments
 (0)