lat-long 0.1.4

Geographic latitude/longitude coordinate types with multiple numeric backends
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Formatting primitives for geographic angles.
//!
//! This module exposes the [`Formatter`] trait, the [`FormatOptions`] builder,
//! and the [`FormatKind`] enum that together drive how [`crate::Latitude`],
//! [`crate::Longitude`], and [`crate::Coordinate`] values are rendered.
//!
//! Four output styles are supported (see [`FormatKind`] for examples):
//!
//! * [`FormatKind::Decimal`] — plain decimal degrees, e.g. `48.858222`.
//! * [`FormatKind::DmsSigned`] — DMS with a leading sign, e.g. `48° 51′ 29.6″`.
//! * [`FormatKind::DmsLabeled`] — DMS with a cardinal label, e.g. `48° 51′ 29.6″ N`.
//! * [`FormatKind::DmsBare`] — fixed-width DMS without symbols, e.g. `+048:51:29.600000`.
//!
//! The [`Display`](core::fmt::Display) implementations on the public types
//! delegate to [`Formatter::format`] with sensible defaults. Use
//! [`Formatter::to_formatted_string`] when you need an owned `String` and want
//! to pick a non-default style or precision.
//!
//! # Examples
//!
//! ```rust
//! use lat_long::{Angle, Latitude, fmt::{FormatOptions, Formatter}};
//!
//! let lat = Latitude::new(48, 51, 29.6).unwrap();
//! let dms = lat.to_formatted_string(&FormatOptions::dms_labeled().with_latitude_labels());
//! assert!(dms.ends_with(" N"));
//! ```
//!

use crate::inner;
use core::{
    fmt::{Debug, Write},
    hash::Hash,
};
use ordered_float::OrderedFloat;

// ---------------------------------------------------------------------------
// Public Types
// ---------------------------------------------------------------------------

///
/// Common interface for writing a value to a [`Write`] target using a
/// [`FormatOptions`] descriptor.
///
/// Implementations exist for [`OrderedFloat<f64>`] (the underlying numeric
/// representation of an angle) and for each public coordinate type. Most
/// callers will reach for [`Formatter::to_formatted_string`] when they want
/// an owned `String`.
///
/// # Examples
///
/// ```rust
/// use lat_long::{Angle, Longitude, fmt::{FormatOptions, Formatter}};
///
/// let lon = Longitude::new(-122, 19, 59.0).unwrap();
/// let bare = lon.to_formatted_string(&FormatOptions::dms_bare());
/// assert!(bare.starts_with('-'));
/// ```
///
pub trait Formatter {
    ///
    ///  Write `self` to `f` according to `options`.
    ///
    /// Implementations should respect every relevant field of `options`
    /// (kind, precision, labels) and produce no extraneous whitespace.
    ///
    fn format<W: Write>(&self, f: &mut W, options: &FormatOptions) -> std::fmt::Result;

    /// Convenience helper: render `self` into a new `String`.
    ///
    /// This is implemented in terms of [`Formatter::format`] and a `String`
    /// buffer, so it cannot fail in practice.
    fn to_formatted_string(&self, fmt: &FormatOptions) -> String {
        let mut buffer = String::new();
        self.format(&mut buffer, fmt).unwrap();
        buffer
    }
}

///
/// The default format is [`FormatKind::Decimal`] (plain decimal degrees).
/// When you use the alternate flag (`{:#}`) the default DMS variant is used.
///
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FormatOptions {
    precision: Option<usize>,
    kind: FormatKind,
    labels: Option<(char, char)>,
}

///
/// | Variant      | Example              |
/// |--------------|----------------------|
/// | `Decimal`    | `48.8582`            |
/// | `DmsSigned`  | `48° 51′ 29.6″`      |
/// | `DmsLabeled` | `48° 51′ 29.6″ N`    |
/// | `DmsBare`    | `+048:51:29.600000`  |
///
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FormatKind {
    #[default]
    ///
    /// Format as decimal degrees, e.g. `48.8582`. This format has a *default* precision
    /// of 8 decimal places.
    ///
    Decimal,

    /// Format as degrees with Unicode symbols, e.g. `-48° 51′ 29.600000″`. This format
    /// has a *default* precision of 6 decimal places.
    DmsSigned,

    /// Format as degrees with a cardinal-direction label, e.g. `48° 51′ 29.600000″ N`.
    /// This format has a *default* precision of 6 decimal places.
    DmsLabeled,

    /// Format as degrees with no symbols, e.g. `048:51:29.600000`. This format has a
    /// *minimum* precision of 4 decimal places, and a *default* precision of 6.
    DmsBare,
}

// ---------------------------------------------------------------------------
// Public Constants
// ---------------------------------------------------------------------------

///
/// Default number of fractional digits used when rendering decimal degrees.
///
pub const DEFAULT_DECIMAL_PRECISION: usize = 8;

///
/// Default number of fractional digits used for the seconds component of any
/// DMS rendering.
///
pub const DEFAULT_DMS_PRECISION: usize = 6;

///
/// Minimum number of fractional digits enforced by [`FormatKind::DmsBare`].
///
/// The bare format is designed to be machine-parseable, so its seconds field
/// has a guaranteed-minimum width regardless of the requested precision.
///
pub const MINIMUM_DMS_BARE_PRECISION: usize = 4;

// ---------------------------------------------------------------------------
// Implementations >> Formatter
// ---------------------------------------------------------------------------

impl Formatter for OrderedFloat<f64> {
    fn format<W: Write>(&self, f: &mut W, options: &FormatOptions) -> std::fmt::Result {
        formatter_impl(*self, f, options)
    }
}

// ---------------------------------------------------------------------------
// Implementations >> FormatOptions
// ---------------------------------------------------------------------------

impl From<FormatKind> for FormatOptions {
    fn from(kind: FormatKind) -> Self {
        Self::new(kind)
    }
}

impl FormatOptions {
    const fn new(kind: FormatKind) -> Self {
        Self {
            precision: None,
            kind,
            labels: None,
        }
    }

    ///
    /// Return a [`FormatOptions`] for decimal degrees with the default precision.
    ///
    pub const fn decimal() -> Self {
        Self::new(FormatKind::Decimal).with_default_precision()
    }

    ///
    /// Return a [`FormatOptions`] for degrees, minutes, seconds with the default precision.
    ///
    pub const fn dms() -> Self {
        Self::dms_signed()
    }

    ///
    /// Return a [`FormatOptions`] for signed degrees, minutes, seconds with the default precision.
    ///
    pub const fn dms_signed() -> Self {
        Self::new(FormatKind::DmsSigned).with_default_precision()
    }

    ///
    /// Return a [`FormatOptions`] for labeled degrees, minutes, seconds with the default precision.
    ///
    pub const fn dms_labeled() -> Self {
        Self::new(FormatKind::DmsLabeled).with_default_precision()
    }

    ///
    /// Return a [`FormatOptions`] for bare degrees, minutes, seconds with the default precision.
    ///
    pub const fn dms_bare() -> Self {
        Self::new(FormatKind::DmsBare).with_default_precision()
    }

    ///
    /// Override the number of fractional digits used when rendering.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lat_long::{Angle, Latitude, fmt::{FormatOptions, Formatter}};
    ///
    /// let lat = Latitude::new(48, 51, 29.6).unwrap();
    /// let s = lat.to_formatted_string(&FormatOptions::decimal().with_precision(2));
    /// assert_eq!(s, "48.86");
    /// ```
    ///
    pub const fn with_precision(mut self, precision: usize) -> Self {
        self.precision = Some(precision);
        self
    }

    ///
    /// Set the precision to the default value for the current [`FormatKind`].
    ///
    /// Decimal uses [`DEFAULT_DECIMAL_PRECISION`]; every DMS variant uses
    /// [`DEFAULT_DMS_PRECISION`].
    ///
    pub const fn with_default_precision(mut self) -> Self {
        match self.kind {
            FormatKind::Decimal => self.precision = Some(DEFAULT_DECIMAL_PRECISION),
            _ => self.precision = Some(DEFAULT_DMS_PRECISION),
        }
        self
    }

    ///
    /// Set the `(positive, negative)` label pair used by [`FormatKind::DmsLabeled`].
    ///
    /// Prefer [`with_latitude_labels`](Self::with_latitude_labels) or
    /// [`with_longitude_labels`](Self::with_longitude_labels) for the standard
    /// `N`/`S` and `E`/`W` pairs.
    ///
    pub const fn with_labels(mut self, labels: (char, char)) -> Self {
        self.labels = Some(labels);
        self
    }

    ///
    /// Convenience: set labels to the latitude pair `('N', 'S')`.
    ///
    pub const fn with_latitude_labels(mut self) -> Self {
        self.labels = Some(('N', 'S'));
        self
    }

    ///
    /// Convenience: set labels to the longitude pair `('E', 'W')`.
    ///
    pub const fn with_longitude_labels(mut self) -> Self {
        self.labels = Some(('E', 'W'));
        self
    }

    ///
    /// Returns the configured [`FormatKind`].
    ///
    pub const fn kind(&self) -> FormatKind {
        self.kind
    }

    ///
    /// Returns `true` if this is a [`FormatKind::Decimal`] format.
    ///
    pub const fn is_decimal(&self) -> bool {
        matches!(self.kind(), FormatKind::Decimal)
    }

    ///
    /// Returns `true` if this is any DMS variant (signed, labeled, or bare).
    ///
    pub const fn is_dms(&self) -> bool {
        self.is_dms_signed() || self.is_dms_labeled() || self.is_dms_bare()
    }

    ///
    /// Returns `true` if this is the [`FormatKind::DmsSigned`] variant.
    ///
    pub const fn is_dms_signed(&self) -> bool {
        matches!(self.kind(), FormatKind::DmsSigned)
    }

    ///
    /// Returns `true` if this is the [`FormatKind::DmsLabeled`] variant.
    ///
    pub const fn is_dms_labeled(&self) -> bool {
        matches!(self.kind(), FormatKind::DmsLabeled)
    }

    ///
    /// Returns `true` if this is the [`FormatKind::DmsBare`] variant.
    ///
    pub const fn is_dms_bare(&self) -> bool {
        matches!(self.kind(), FormatKind::DmsBare)
    }

    ///
    /// Returns the configured precision, if any was set.
    ///
    pub const fn precision(&self) -> Option<usize> {
        self.precision
    }

    ///
    /// Returns the configured `(positive, negative)` label pair, if any.
    ///
    pub const fn labels(&self) -> Option<(char, char)> {
        self.labels
    }

    ///
    /// Returns just the label used for positive values, if labels are set.
    ///
    pub fn positive_label(&self) -> Option<char> {
        self.labels.as_ref().map(|l| l.0)
    }

    ///
    /// Returns just the label used for negative values, if labels are set.
    ///
    pub fn negative_label(&self) -> Option<char> {
        self.labels.as_ref().map(|l| l.1)
    }
}

// ---------------------------------------------------------------------------
// Internal Functions
// ---------------------------------------------------------------------------

pub(crate) fn formatter_impl<W: Write>(
    angle: OrderedFloat<f64>,
    f: &mut W,
    options: &FormatOptions,
) -> std::fmt::Result {
    match options.kind() {
        FormatKind::Decimal => {
            if let Some(precision) = options.precision() {
                write!(f, "{:.precision$}", angle.into_inner())
            } else {
                write!(f, "{}", angle.into_inner())
            }
        }
        FormatKind::DmsSigned => {
            let (degrees, minutes, seconds) = inner::to_degrees_minutes_seconds(angle);
            if let Some(precision) = options.precision() {
                write!(f, "{degrees}° {minutes}′ {seconds:.precision$}″")
            } else {
                write!(f, "{degrees}° {minutes}{seconds}")
            }
        }
        FormatKind::DmsLabeled => {
            let (degrees, minutes, seconds) = inner::to_degrees_minutes_seconds(angle);
            let (positive, negative) = options.labels().expect("No labels provided");
            if let Some(precision) = options.precision() {
                write!(
                    f,
                    "{}° {}′ {:.precision$}″ {}",
                    degrees.abs(),
                    minutes,
                    seconds,
                    if angle > inner::ZERO {
                        positive.to_string()
                    } else if angle < inner::ZERO {
                        negative.to_string()
                    } else {
                        "".to_string()
                    }
                )
            } else {
                write!(
                    f,
                    "{}° {}{}{}",
                    degrees.abs(),
                    minutes,
                    seconds,
                    if angle > inner::ZERO {
                        positive.to_string()
                    } else if angle < inner::ZERO {
                        negative.to_string()
                    } else {
                        "".to_string()
                    }
                )
            }
        }
        FormatKind::DmsBare => {
            let (degrees, minutes, seconds) = inner::to_degrees_minutes_seconds(angle);
            let precision = if let Some(precision) = options.precision()
                && precision >= 4
            {
                precision
            } else {
                MINIMUM_DMS_BARE_PRECISION
            };
            let width = precision + 3;
            write!(f, "{degrees:+04}:{minutes:02}:{seconds:0width$.precision$}",)
        }
    }
}

// ---------------------------------------------------------------------------
// Unit Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use crate::fmt::{FormatOptions, Formatter};
    use ordered_float::OrderedFloat;

    #[test]
    fn test_float_to_string_positive() {
        assert_eq!(
            OrderedFloat(45.508333)
                .to_formatted_string(&FormatOptions::decimal().with_precision(6)),
            "45.508333"
        );
    }

    #[test]
    fn test_float_to_string_negative() {
        assert_eq!(
            OrderedFloat(-45.508333)
                .to_formatted_string(&FormatOptions::decimal().with_precision(6)),
            "-45.508333"
        );
    }

    #[test]
    fn test_float_to_string_signed_positive() {
        assert_eq!(
            OrderedFloat(45.508333).to_formatted_string(&FormatOptions::dms_signed()),
            "45° 30′ 29.998800″"
        );
    }

    #[test]
    fn test_float_to_string_signed_negative() {
        assert_eq!(
            OrderedFloat(-45.508333).to_formatted_string(&FormatOptions::dms_signed()),
            "-45° 30′ 29.998800″"
        );
    }

    #[test]
    fn test_float_to_degree_string_labeled_positive() {
        assert_eq!(
            OrderedFloat(45.508333)
                .to_formatted_string(&FormatOptions::dms_labeled().with_latitude_labels()),
            "45° 30′ 29.998800″ N"
        );
    }

    #[test]
    fn test_float_to_string_labeled_negative() {
        assert_eq!(
            OrderedFloat(-45.508333)
                .to_formatted_string(&FormatOptions::dms_labeled().with_latitude_labels()),
            "45° 30′ 29.998800″ S"
        );
    }

    #[test]
    fn test_float_to_string_bare_positive() {
        assert_eq!(
            OrderedFloat(45.508333).to_formatted_string(&FormatOptions::dms_bare()),
            "+045:30:29.998800"
        );
    }

    #[test]
    fn test_float_to_string_bare_negative() {
        assert_eq!(
            OrderedFloat(-45.508333).to_formatted_string(&FormatOptions::dms_bare()),
            "-045:30:29.998800"
        );
    }
}