kinavis-kernel 1.0.1

The value types, invariants and error type of the KINAVIS navigation crates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Errors of the value types and numeric kernels.
//!
//! [`KernelError`] uses only the kernel's vocabulary: non-finite or
//! out-of-range numbers, undersized buffers or stores, unsolvable systems,
//! undefined quantities, missing inputs, reversed clocks. Algorithm-specific
//! errors (deviation tables, sailings) belong to the crate that owns the
//! algorithm, which wraps this type via `From`.
//!
//! `#[non_exhaustive]`: variants may be added in a minor release; match with a
//! wildcard arm.
//!
//! [`ensure_finite`] and [`ensure_range`] are public so adapters validate
//! sensor input the same way the value types do.

use crate::inline::InlineStr;
use core::fmt;

/// Result alias.
pub type Result<T> = core::result::Result<T, KernelError>;

/// Maximum bytes of offending input carried by an error.
///
/// Enough to identify the input; bounded so errors need no allocator and cannot
/// be inflated by input.
pub const EXCERPT_BYTES: usize = 32;

/// Excerpt of offending input.
pub type Excerpt = InlineStr<EXCERPT_BYTES>;

/// Kernel operation failure.
///
/// No operation panics on caller data; every failure is reported through this
/// type.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum KernelError {
    /// Parameter is `NaN` or infinite.
    NotFinite {
        /// Parameter name.
        parameter: &'static str,
        /// Value supplied.
        value: f64,
    },

    /// Parameter is finite but out of range.
    OutOfRange {
        /// Parameter name.
        parameter: &'static str,
        /// Value supplied.
        value: f64,
        /// Minimum, inclusive.
        min: f64,
        /// Maximum, inclusive.
        max: f64,
    },

    /// Fewer items than required: interpolation nodes, route waypoints, fit
    /// samples.
    InsufficientData {
        /// Items present.
        found: usize,
        /// Items required.
        required: usize,
        /// Operation that required them.
        context: &'static str,
    },

    /// Not one of the eight cardinal/intercardinal abbreviations.
    UnknownCardinalDirection {
        /// Input, truncated.
        direction: Excerpt,
    },

    /// Caller-supplied output buffer too small.
    ///
    /// Returned by slice-writing calls so a short buffer is an error, not a
    /// silent truncation.
    BufferTooSmall {
        /// Values produced.
        needed: usize,
        /// Buffer length.
        found: usize,
    },

    /// Inline store capacity exceeded.
    ///
    /// Collections are fixed-capacity inline stores; their bounds are public
    /// constants, so callers can check beforehand.
    CapacityExceeded {
        /// What was being built.
        context: &'static str,
        /// Items required.
        needed: usize,
        /// Capacity.
        capacity: usize,
    },

    /// Linear system has no unique solution.
    ///
    /// For a parametric fit: the sample courses do not constrain the requested
    /// coefficients (e.g. five coefficients from nodes on one semicircle).
    SingularSystem {
        /// System being solved.
        context: &'static str,
    },

    /// Matrix is not a valid covariance (symmetric positive definite): e.g.
    /// zero observation variance, collapsed innovation covariance.
    NotCovariance {
        /// Intended role of the matrix.
        context: &'static str,
    },

    /// Iterative solver did not converge.
    ///
    /// The deviation inverse fails this way when deviation changes faster than
    /// 1° per degree of heading, so several compass courses map to one magnetic
    /// course; the compass needs re-swinging.
    NotConverged {
        /// Iterations run.
        iterations: u32,
        /// Final residual.
        residual: f64,
    },

    /// String could not be parsed.
    Parse {
        /// Expected value type.
        what: &'static str,
        /// Input, truncated.
        input: Excerpt,
    },

    /// Mathematically undefined for the inputs: rhumb line through a pole,
    /// direction of a zero vector, great circle between antipodes.
    Indeterminate {
        /// Undefined quantity.
        quantity: &'static str,
    },

    /// Required input unavailable: snapshot without position, environment
    /// sample without tide, empty estimator history.
    Missing {
        /// Missing input.
        what: &'static str,
    },

    /// Result exists but is not representable: instant beyond range, counter
    /// overflow.
    Unrepresentable {
        /// Unrepresentable quantity.
        what: &'static str,
    },

    /// Instants in the wrong order for the elapsed time requested (clock
    /// stepped back, or arguments swapped).
    TimeReversed {
        /// Amount by which the later instant precedes the earlier.
        by: core::time::Duration,
    },

    /// Height referred to a different vertical datum than required.
    VerticalDatumMismatch {
        /// Required datum.
        required: crate::geodesy::VerticalDatum,
        /// Datum of the height.
        found: crate::geodesy::VerticalDatum,
    },

    /// Data requested outside its validity interval (leap-second table past
    /// expiry, magnetic model past epoch).
    OutsideValidity {
        /// Requested data.
        data: &'static str,
    },
}

/// Checks that a value is finite.
///
/// # Errors
///
/// [`KernelError::NotFinite`] for `NaN` or infinity.
pub fn ensure_finite(parameter: &'static str, value: f64) -> Result<()> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(KernelError::NotFinite { parameter, value })
    }
}

/// Checks that a value is finite and within `[min, max]`.
///
/// # Errors
///
/// [`KernelError::NotFinite`] for `NaN` or infinity;
/// [`KernelError::OutOfRange`] outside the interval.
pub fn ensure_range(parameter: &'static str, value: f64, min: f64, max: f64) -> Result<()> {
    ensure_finite(parameter, value)?;
    if value < min || value > max {
        return Err(KernelError::OutOfRange {
            parameter,
            value,
            min,
            max,
        });
    }
    Ok(())
}

/// Allowed interval in words. `f64::MIN_POSITIVE` as the minimum reads as
/// "greater than 0", and `f64::MIN` or `f64::MAX` as unbounded, so no bound is
/// printed as hundreds of digits.
fn write_interval(f: &mut fmt::Formatter<'_>, min: f64, max: f64) -> fmt::Result {
    let positive = min > 0.0 && min <= f64::MIN_POSITIVE;
    let below = min > f64::MIN;
    let above = max < f64::MAX;
    match (positive, below, above) {
        (true, _, true) => write!(f, "greater than 0 and at most {max}"),
        (true, _, false) => f.write_str("greater than 0"),
        (false, true, true) => write!(f, "between {min} and {max}"),
        (false, true, false) => write!(f, "at least {min}"),
        (false, false, true) => write!(f, "at most {max}"),
        (false, false, false) => f.write_str("finite"),
    }
}

impl fmt::Display for KernelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotFinite { parameter, value } => {
                write!(f, "{parameter} must be a finite number, got {value}")
            }
            Self::OutOfRange {
                parameter,
                value,
                min,
                max,
            } => {
                write!(f, "{parameter} out of range: {value}. Must be ")?;
                write_interval(f, *min, *max)
            }
            Self::InsufficientData {
                found,
                required,
                context,
            } => write!(f, "{context} needs at least {required}, and has {found}"),
            Self::UnknownCardinalDirection { direction } => write!(
                f,
                "unknown cardinal direction: {direction}. Expected one of N, NE, E, SE, S, SW, W, NW"
            ),
            Self::CapacityExceeded {
                context,
                needed,
                capacity,
            } => write!(
                f,
                "{context} needs room for {needed}, and the limit is {capacity}"
            ),
            Self::BufferTooSmall { needed, found } => write!(
                f,
                "output buffer holds {found} values, {needed} are needed"
            ),
            Self::SingularSystem { context } => {
                write!(f, "singular system while solving {context}")
            }
            Self::NotCovariance { context } => {
                write!(f, "{context} is not a covariance matrix")
            }
            Self::NotConverged {
                iterations,
                residual,
            } => write!(
                f,
                "solver did not converge after {iterations} iterations, residual {residual}"
            ),
            Self::Parse { what, input } => {
                write!(f, "could not read {input:?} as a {what}")
            }
            Self::Indeterminate { quantity } => {
                write!(f, "{quantity} is indeterminate for these inputs")
            }
            Self::Missing { what } => write!(f, "{what} is not available"),
            Self::Unrepresentable { what } => write!(f, "{what} cannot be represented"),
            Self::TimeReversed { by } => {
                write!(f, "time ran backwards by {} s", by.as_secs_f64())
            }
            Self::VerticalDatumMismatch { required, found } => write!(
                f,
                "a height above {found:?} was given where one above {required:?} is required"
            ),
            Self::OutsideValidity { data } => {
                write!(f, "the {data} is not valid for the requested moment")
            }
        }
    }
}

// `std::error::Error` re-exports `core::error::Error` since Rust 1.81; one impl
// covers `std` and `no_std`.
impl core::error::Error for KernelError {}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::ToString;

    #[test]
    fn every_variant_has_a_message() {
        let errors = [
            KernelError::NotFinite {
                parameter: "course",
                value: f64::NAN,
            },
            KernelError::OutOfRange {
                parameter: "course",
                value: 400.0,
                min: 0.0,
                max: 360.0,
            },
            KernelError::InsufficientData {
                found: 1,
                required: 2,
                context: "interpolation",
            },
            KernelError::UnknownCardinalDirection {
                direction: Excerpt::new("XYZ"),
            },
            KernelError::BufferTooSmall {
                needed: 36,
                found: 8,
            },
            KernelError::CapacityExceeded {
                context: "a deviation table",
                needed: 90,
                capacity: 72,
            },
            KernelError::SingularSystem {
                context: "parametric fit",
            },
            KernelError::NotCovariance {
                context: "the observation noise",
            },
            KernelError::TimeReversed {
                by: core::time::Duration::from_secs(18),
            },
            KernelError::OutsideValidity {
                data: "leap second table",
            },
            KernelError::VerticalDatumMismatch {
                required: crate::geodesy::VerticalDatum::Ellipsoid,
                found: crate::geodesy::VerticalDatum::MeanSeaLevel,
            },
            KernelError::NotConverged {
                iterations: 64,
                residual: 1.0,
            },
            KernelError::Parse {
                what: "latitude",
                input: Excerpt::new("north-ish"),
            },
            KernelError::Indeterminate {
                quantity: "a rhumb line through a pole",
            },
            KernelError::Missing {
                what: "the vessel's position",
            },
            KernelError::Unrepresentable {
                what: "a moment beyond the end of time",
            },
        ];
        for error in errors {
            assert!(!error.to_string().is_empty(), "{error:?}");
        }
    }

    #[test]
    fn errors_compare_by_value() {
        let a = KernelError::OutOfRange {
            parameter: "x",
            value: 1.0,
            min: 0.0,
            max: 0.5,
        };
        assert_eq!(a, a.clone());
        assert_ne!(
            a,
            KernelError::Missing {
                what: "the vessel's position"
            }
        );
    }

    #[test]
    fn an_unbounded_side_is_not_printed_as_a_number() {
        let message = |min, max| {
            KernelError::OutOfRange {
                parameter: "x",
                value: 0.0,
                min,
                max,
            }
            .to_string()
        };
        assert_eq!(
            message(0.0, 1.0),
            "x out of range: 0. Must be between 0 and 1"
        );
        assert_eq!(
            message(f64::MIN_POSITIVE, f64::MAX),
            "x out of range: 0. Must be greater than 0"
        );
        assert_eq!(
            message(f64::MIN_POSITIVE, 1e6),
            "x out of range: 0. Must be greater than 0 and at most 1000000"
        );
        assert_eq!(
            message(1.0, f64::MAX),
            "x out of range: 0. Must be at least 1"
        );
        assert_eq!(
            message(f64::MIN, -1.0),
            "x out of range: 0. Must be at most -1"
        );
    }

    #[test]
    fn the_checks_report_what_they_reject() {
        assert!(ensure_finite("x", 1.0).is_ok());
        assert!(matches!(
            ensure_finite("x", f64::NAN),
            Err(KernelError::NotFinite { parameter: "x", .. })
        ));
        assert!(matches!(
            ensure_range("x", 2.0, 0.0, 1.0),
            Err(KernelError::OutOfRange { parameter: "x", .. })
        ));
        assert!(matches!(
            ensure_range("x", f64::INFINITY, 0.0, 1.0),
            Err(KernelError::NotFinite { .. })
        ));
    }
}