sl-glw 0.4.0

Fetch, parse, and render GlobalWind (GLW) wind/current/wave event overlays for Second Life sailing maps
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
//! GlobalWind (GLW) data types.
//!
//! All types correspond directly to fields in the JSON shape documented
//! at the top of the repository `TODO.md`. Numeric values are wrapped in
//! validating newtypes so out-of-range data fails at parse time rather
//! than silently producing wrong wind arrows on the map.
//!
//! Direction conventions (verified against the GLW LSL source):
//! - 0° = North, increasing clockwise, in degrees.
//! - Wind direction is the direction the wind blows **from**
//!   (meteorological), so a north wind blows toward the south.
//! - Current direction follows the same convention.

use crate::error::ParseError;

// ---------------------------------------------------------------------------
// Identifiers
// ---------------------------------------------------------------------------

/// Numeric event identifier assigned by the GLW server.
///
/// Used as the value of the `?id=…` query parameter on the GLW
/// `glwDataReq.php` endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EventId(u32);

impl EventId {
    /// Wrap a raw numeric id.
    #[must_use]
    pub const fn new(id: u32) -> Self {
        Self(id)
    }
    /// The underlying numeric id.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }
}

impl std::fmt::Display for EventId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl serde::Serialize for EventId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for EventId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(u32::deserialize(deserializer)?))
    }
}

/// String event key (the `eventKey` field of the JSON document).
///
/// The GLW server treats this as opaque text and looks it up via the
/// `?key=…` query parameter on the `glwDataReq.php` endpoint. Named with
/// the `Glw` prefix to avoid colliding with `sl_types::key::EventKey`
/// which represents a Second Life UUID for an event listing.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GlwEventKey(String);

impl GlwEventKey {
    /// Wrap a raw event-key string.
    #[must_use]
    pub fn new<S: Into<String>>(key: S) -> Self {
        Self(key.into())
    }
    /// Borrow the underlying string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for GlwEventKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl serde::Serialize for GlwEventKey {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> serde::Deserialize<'de> for GlwEventKey {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(String::deserialize(deserializer)?))
    }
}

// ---------------------------------------------------------------------------
// Validated scalar newtypes
// ---------------------------------------------------------------------------

/// Wind or current direction in degrees from North, increasing clockwise.
///
/// Valid range is `0..=359`. By GLW/meteorological convention, a value
/// of e.g. `180` means the wind is coming **from** the south.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WindDirection(u16);

impl WindDirection {
    /// Construct after range validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `degrees` is not in `0..=359`.
    pub fn try_new(degrees: u16) -> Result<Self, ParseError> {
        if degrees > 359 {
            return Err(ParseError::OutOfRange {
                field: "wind_direction",
                value: degrees.to_string(),
                allowed: "0..=359",
            });
        }
        Ok(Self(degrees))
    }
    /// Raw integer degrees in `0..=359`.
    #[must_use]
    pub const fn degrees(self) -> u16 {
        self.0
    }
}

impl serde::Serialize for WindDirection {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u16(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for WindDirection {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = u16::deserialize(deserializer)?;
        Self::try_new(v).map_err(serde::de::Error::custom)
    }
}

/// Wind / wave / current speed in knots.
///
/// Stored as `f32`; GLW JSON may use either integer or floating-point
/// representations and serde_json promotes integers transparently. We do
/// not range-validate here because reasonable upper bounds on wind speed
/// are domain-dependent.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct KnotSpeed(f32);

impl KnotSpeed {
    /// Wrap a raw float.
    #[must_use]
    pub const fn new(knots: f32) -> Self {
        Self(knots)
    }
    /// The raw value in knots.
    #[must_use]
    pub const fn knots(self) -> f32 {
        self.0
    }
}

impl serde::Serialize for KnotSpeed {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for KnotSpeed {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(f32::deserialize(deserializer)?))
    }
}

/// Gust intensity as a percentage of base wind speed, valid in `0..=100`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GustsPercent(u8);

impl GustsPercent {
    /// Construct after range validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `percent` is greater than 100.
    pub fn try_new(percent: u8) -> Result<Self, ParseError> {
        if percent > 100 {
            return Err(ParseError::OutOfRange {
                field: "gusts_percent",
                value: percent.to_string(),
                allowed: "0..=100",
            });
        }
        Ok(Self(percent))
    }
    /// Raw percentage in `0..=100`.
    #[must_use]
    pub const fn percent(self) -> u8 {
        self.0
    }
}

impl serde::Serialize for GustsPercent {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u8(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for GustsPercent {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = u8::deserialize(deserializer)?;
        Self::try_new(v).map_err(serde::de::Error::custom)
    }
}

/// Maximum wind shift in degrees per period (`0..=180`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShiftsDegrees(u16);

impl ShiftsDegrees {
    /// Construct after range validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `degrees` is greater than 180.
    pub fn try_new(degrees: u16) -> Result<Self, ParseError> {
        if degrees > 180 {
            return Err(ParseError::OutOfRange {
                field: "shifts_degrees",
                value: degrees.to_string(),
                allowed: "0..=180",
            });
        }
        Ok(Self(degrees))
    }
    /// Raw degrees in `0..=180`.
    #[must_use]
    pub const fn degrees(self) -> u16 {
        self.0
    }
}

impl serde::Serialize for ShiftsDegrees {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u16(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for ShiftsDegrees {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = u16::deserialize(deserializer)?;
        Self::try_new(v).map_err(serde::de::Error::custom)
    }
}

/// Wind shift and gust period in seconds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Period(u32);

impl Period {
    /// Wrap a raw count of seconds.
    #[must_use]
    pub const fn new(seconds: u32) -> Self {
        Self(seconds)
    }
    /// The raw period in seconds.
    #[must_use]
    pub const fn seconds(self) -> u32 {
        self.0
    }
}

impl serde::Serialize for Period {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for Period {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(u32::deserialize(deserializer)?))
    }
}

/// Wave height in meters. A value of 0 in JSON is preserved literally
/// (per the GLW spec: "if height=0 there are no waves, but it is set to
/// fill the values of the variations").
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WaveHeight(f32);

impl WaveHeight {
    /// Wrap a raw float.
    #[must_use]
    pub const fn new(meters: f32) -> Self {
        Self(meters)
    }
    /// The raw height in meters.
    #[must_use]
    pub const fn meters(self) -> f32 {
        self.0
    }
}

impl serde::Serialize for WaveHeight {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for WaveHeight {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(f32::deserialize(deserializer)?))
    }
}

/// Wave length in meters.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WaveLength(f32);

impl WaveLength {
    /// Wrap a raw float.
    #[must_use]
    pub const fn new(meters: f32) -> Self {
        Self(meters)
    }
    /// The raw length in meters.
    #[must_use]
    pub const fn meters(self) -> f32 {
        self.0
    }
}

impl serde::Serialize for WaveLength {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for WaveLength {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(f32::deserialize(deserializer)?))
    }
}

/// Percentage variance applied to a wave height/length (`0..=100`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PercentVariance(u8);

impl PercentVariance {
    /// Construct after range validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `percent` is greater than 100.
    pub fn try_new(percent: u8) -> Result<Self, ParseError> {
        if percent > 100 {
            return Err(ParseError::OutOfRange {
                field: "percent_variance",
                value: percent.to_string(),
                allowed: "0..=100",
            });
        }
        Ok(Self(percent))
    }
    /// Raw percent in `0..=100`.
    #[must_use]
    pub const fn percent(self) -> u8 {
        self.0
    }
}

impl serde::Serialize for PercentVariance {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u8(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for PercentVariance {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = u8::deserialize(deserializer)?;
        Self::try_new(v).map_err(serde::de::Error::custom)
    }
}

/// Two boolean toggles describing how waves affect a boat: whether they
/// modulate its speed and whether they perturb its heading.
///
/// In GLW JSON the field appears either as an object
/// `{"speed": 0|1, "steer": 0|1}` or as an integer bitmask
/// (`0 = none`, `1 = steer`, `2 = speed`, `3 = both`). Both forms are
/// accepted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaveEffects {
    /// Whether waves modulate the boat's forward speed.
    pub speed: bool,
    /// Whether waves perturb the boat's heading.
    pub steer: bool,
}

impl serde::Serialize for WaveEffects {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct as _;
        let mut s = serializer.serialize_struct("WaveEffects", 2)?;
        s.serialize_field("speed", &u8::from(self.speed))?;
        s.serialize_field("steer", &u8::from(self.steer))?;
        s.end()
    }
}

impl<'de> serde::Deserialize<'de> for WaveEffects {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        /// Internal helper to deserialize either of the two JSON shapes.
        #[derive(serde::Deserialize)]
        #[serde(untagged)]
        enum Raw {
            /// JSON integer 0..=3, treated as a bitmask
            /// (bit 0 = steer, bit 1 = speed) per the GLW LSL convention.
            Bitmask(u8),
            /// JSON object with `speed` and `steer` ints (0 or 1).
            Object {
                /// `1` if the speed effect is on, `0` otherwise.
                #[serde(default)]
                speed: u8,
                /// `1` if the steer effect is on, `0` otherwise.
                #[serde(default)]
                steer: u8,
            },
        }
        let raw = Raw::deserialize(deserializer)?;
        Ok(match raw {
            Raw::Bitmask(n) => Self {
                speed: (n & 0b10) != 0,
                steer: (n & 0b01) != 0,
            },
            Raw::Object { speed, steer } => Self {
                speed: speed != 0,
                steer: steer != 0,
            },
        })
    }
}

/// A strictly positive water depth in meters.
///
/// The GLW JSON uses `0` to mean "depth-attenuation disabled"; this
/// crate models that as the absence of a value (see
/// [`WaterDepthSetting`]) instead of allowing a zero value here. Negative
/// values are rejected at parse time.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WaterDepth(f32);

impl WaterDepth {
    /// Construct after validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `meters <= 0.0`.
    pub fn try_new(meters: f32) -> Result<Self, ParseError> {
        if meters > 0.0 {
            Ok(Self(meters))
        } else {
            Err(ParseError::OutOfRange {
                field: "water_depth",
                value: meters.to_string(),
                allowed: "> 0",
            })
        }
    }
    /// The raw depth in meters (always strictly positive).
    #[must_use]
    pub const fn meters(self) -> f32 {
        self.0
    }
}

impl serde::Serialize for WaterDepth {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

/// Tri-state setting for water depth on a `BaseCurrents` block.
///
/// JSON value 0 maps to [`Self::Disabled`]. Positive values map to
/// [`Self::Set`]. Negative values are rejected.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WaterDepthSetting {
    /// Depth attenuation is disabled (JSON `0`).
    Disabled,
    /// A positive depth in meters.
    Set(WaterDepth),
}

impl serde::Serialize for WaterDepthSetting {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match *self {
            Self::Disabled => serializer.serialize_f32(0.0),
            Self::Set(d) => serializer.serialize_f32(d.meters()),
        }
    }
}

impl<'de> serde::Deserialize<'de> for WaterDepthSetting {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = f32::deserialize(deserializer)?;
        // exact 0.0 is the GLW disabled sentinel; serde_json maps the
        // integer 0 to exactly 0.0_f32 so the comparison is well-defined.
        let is_zero = v == 0.0;
        if is_zero {
            Ok(Self::Disabled)
        } else {
            WaterDepth::try_new(v)
                .map(Self::Set)
                .map_err(serde::de::Error::custom)
        }
    }
}

/// Per-area or per-circle margin in meters, clamped to `0..=100`.
///
/// The GLW spec documents a maximum of 100 m; values above 100 are
/// clamped to 100 and emit a `tracing::warn!` rather than failing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MarginMeters(u8);

impl MarginMeters {
    /// Construct, clamping values above 100 (with a `tracing::warn!`).
    #[must_use]
    pub fn new_clamped(meters: u16) -> Self {
        if meters > 100 {
            tracing::warn!(
                requested = meters,
                clamped = 100u8,
                "GLW margin exceeds documented maximum (100m); clamping",
            );
            Self(100)
        } else {
            // safe: meters <= 100 fits in u8
            Self(u8::try_from(meters).unwrap_or(100))
        }
    }
    /// Raw margin in meters (`0..=100`).
    #[must_use]
    pub const fn meters(self) -> u8 {
        self.0
    }
}

impl serde::Serialize for MarginMeters {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u8(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for MarginMeters {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self::new_clamped(u16::deserialize(deserializer)?))
    }
}

/// Radius of a GLW circle in meters.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RadiusMeters(f32);

impl RadiusMeters {
    /// Construct after non-negative validation.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::OutOfRange`] if `meters < 0.0`.
    pub fn try_new(meters: f32) -> Result<Self, ParseError> {
        if meters < 0.0 {
            return Err(ParseError::OutOfRange {
                field: "radius_meters",
                value: meters.to_string(),
                allowed: ">= 0",
            });
        }
        Ok(Self(meters))
    }
    /// Raw radius in meters.
    #[must_use]
    pub const fn meters(self) -> f32 {
        self.0
    }
}

impl serde::Serialize for RadiusMeters {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

impl<'de> serde::Deserialize<'de> for RadiusMeters {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let v = f32::deserialize(deserializer)?;
        Self::try_new(v).map_err(serde::de::Error::custom)
    }
}

/// Whether an area or circle is a nested override on top of another
/// area or circle (`overlap = 1`) rather than directly modifying the
/// base wind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Overlap(bool);

impl Overlap {
    /// Wrap a raw bool.
    #[must_use]
    pub const fn new(v: bool) -> Self {
        Self(v)
    }
    /// The raw bool value.
    #[must_use]
    pub const fn get(self) -> bool {
        self.0
    }
}

impl serde::Serialize for Overlap {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u8(u8::from(self.0))
    }
}

impl<'de> serde::Deserialize<'de> for Overlap {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self(u8::deserialize(deserializer)? != 0))
    }
}

// ---------------------------------------------------------------------------
// Base wind / waves / currents (always fully populated)
// ---------------------------------------------------------------------------

/// Base wind parameters that apply to the whole event unless an area or
/// circle override is in effect.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct BaseWind {
    /// Direction the wind blows **from**, in degrees clockwise from North.
    #[serde(rename = "dir")]
    pub direction: WindDirection,
    /// Base wind speed in knots.
    pub speed: KnotSpeed,
    /// Gust intensity as a percentage of base wind speed.
    pub gusts: GustsPercent,
    /// Maximum wind shift in degrees per period.
    pub shifts: ShiftsDegrees,
    /// Period of shift/gust oscillation in seconds.
    pub period: Period,
}

/// Base wave parameters that apply to the whole event unless an area or
/// circle override is in effect.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BaseWaves {
    /// Wave height in meters. A height of zero means "no waves", though
    /// other fields may still carry meaningful variance values.
    pub height: WaveHeight,
    /// Wave speed in knots.
    pub speed: KnotSpeed,
    /// Wave length in meters.
    pub length: WaveLength,
    /// Whether waves affect boat speed and/or steering.
    pub effects: WaveEffects,
    /// Variance applied to wave height, as a percentage.
    pub height_var: PercentVariance,
    /// Variance applied to wave length, as a percentage.
    pub length_var: PercentVariance,
}

/// Base current parameters that apply to the whole event unless an area
/// or circle override is in effect.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BaseCurrents {
    /// Current speed in knots. Zero means no current; the other fields
    /// still carry meaningful variance values.
    pub speed: KnotSpeed,
    /// Direction the current flows **from**, in degrees clockwise from
    /// North.
    #[serde(rename = "dir")]
    pub direction: WindDirection,
    /// Water-depth setting controlling how currents attenuate with
    /// real-world sea depth.
    pub water_depth: WaterDepthSetting,
}

/// The full base block: wind, waves, currents combined.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Base {
    /// Base wind parameters.
    pub wind: BaseWind,
    /// Base wave parameters.
    pub waves: BaseWaves,
    /// Base current parameters.
    pub currents: BaseCurrents,
}

// ---------------------------------------------------------------------------
// Per-area / per-circle overrides (every field optional)
// ---------------------------------------------------------------------------

/// Wind override block. Each field is independently optional: only the
/// fields actually overridden by the area/circle are present.
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct WindOverride {
    /// Direction the wind blows from, in degrees clockwise from North.
    #[serde(rename = "dir", skip_serializing_if = "Option::is_none")]
    pub direction: Option<WindDirection>,
    /// Wind speed in knots.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed: Option<KnotSpeed>,
    /// Gust intensity (`0..=100`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gusts: Option<GustsPercent>,
    /// Maximum wind shift in degrees per period.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shifts: Option<ShiftsDegrees>,
    /// Period of shift/gust oscillation in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub period: Option<Period>,
}

impl WindOverride {
    /// `true` if every field is `None`.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.direction.is_none()
            && self.speed.is_none()
            && self.gusts.is_none()
            && self.shifts.is_none()
            && self.period.is_none()
    }
}

/// Wave override block. Each field is independently optional.
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct WavesOverride {
    /// Wave height in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<WaveHeight>,
    /// Wave speed in knots.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed: Option<KnotSpeed>,
    /// Wave length in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length: Option<WaveLength>,
    /// Whether waves affect boat speed and/or steering.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effects: Option<WaveEffects>,
    /// Variance applied to wave height, as a percentage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height_var: Option<PercentVariance>,
    /// Variance applied to wave length, as a percentage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length_var: Option<PercentVariance>,
}

impl WavesOverride {
    /// `true` if every field is `None`.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.height.is_none()
            && self.speed.is_none()
            && self.length.is_none()
            && self.effects.is_none()
            && self.height_var.is_none()
            && self.length_var.is_none()
    }
}

/// Currents override block. Each field is independently optional.
///
/// Note `water_depth: Option<WaterDepthSetting>`: outer `None` means the
/// override does not touch water depth at all; inner
/// [`WaterDepthSetting::Disabled`] means the override explicitly
/// disables depth attenuation.
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct CurrentsOverride {
    /// Current speed in knots.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed: Option<KnotSpeed>,
    /// Direction the current flows from, in degrees clockwise from North.
    #[serde(rename = "dir", skip_serializing_if = "Option::is_none")]
    pub direction: Option<WindDirection>,
    /// Water-depth setting controlling how currents attenuate with
    /// real-world sea depth.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub water_depth: Option<WaterDepthSetting>,
}

impl CurrentsOverride {
    /// `true` if every field is `None`.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.speed.is_none() && self.direction.is_none() && self.water_depth.is_none()
    }
}

// ---------------------------------------------------------------------------
// Area / Circle / Event
// ---------------------------------------------------------------------------

/// A rectangular GLW area covering one or more whole regions.
///
/// `name` is the original JSON key (e.g. `"area1"`). Document order is
/// preserved by [`AreaList`] so that `overlap = true` layering matches
/// the GLW LSL/PHP reference implementation.
#[derive(Debug, Clone, PartialEq)]
pub struct Area {
    /// JSON key for this area (e.g. `"area1"`).
    pub name: String,
    /// Region rectangle covered by this area.
    pub grid_rectangle: sl_types::map::GridRectangle,
    /// Margin width in meters where conditions blend with the base.
    pub margin: MarginMeters,
    /// `true` when this area overlays another area/circle rather than the
    /// base wind directly.
    pub overlap: Overlap,
    /// Optional wind override.
    pub wind: Option<WindOverride>,
    /// Optional wave override.
    pub waves: Option<WavesOverride>,
    /// Optional currents override.
    pub currents: Option<CurrentsOverride>,
}

/// A circular GLW area.
///
/// `name` is the original JSON key (e.g. `"circle1"`).
#[derive(Debug, Clone, PartialEq)]
pub struct Circle {
    /// JSON key for this circle (e.g. `"circle1"`).
    pub name: String,
    /// Region containing the centre point.
    pub center_sim: sl_types::map::GridCoordinates,
    /// Coordinates of the centre within `center_sim`, in meters from the
    /// SW corner of the region.
    pub center_point: sl_types::map::RegionCoordinates,
    /// Radius of the circle in meters.
    pub radius: RadiusMeters,
    /// Margin width in meters where conditions blend with the base.
    pub margin: MarginMeters,
    /// `true` when this circle overlays another area/circle rather than
    /// the base wind directly.
    pub overlap: Overlap,
    /// Optional wind override.
    pub wind: Option<WindOverride>,
    /// Optional wave override.
    pub waves: Option<WavesOverride>,
    /// Optional currents override.
    pub currents: Option<CurrentsOverride>,
}

/// Document-ordered list of [`Area`]s.
///
/// Order matters: overlap layering applies in document order. Serializes
/// back as a JSON object keyed by `Area::name` so it round-trips against
/// the original GLW JSON shape.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AreaList(pub Vec<Area>);

impl AreaList {
    /// Borrow the underlying `Vec`.
    #[must_use]
    pub fn as_slice(&self) -> &[Area] {
        &self.0
    }
    /// Number of areas in the list.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.0.len()
    }
    /// `true` when the list contains no areas.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl serde::Serialize for AreaList {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap as _;
        let mut map = serializer.serialize_map(Some(self.0.len()))?;
        for area in &self.0 {
            map.serialize_entry(&area.name, &AreaSerializable::from(area))?;
        }
        map.end()
    }
}

/// Private view of an [`Area`] without its `name` field, used only for
/// serialization so the area's body matches the GLW JSON shape (the
/// name is the JSON key, not a body field).
#[derive(serde::Serialize)]
struct AreaSerializable<'a> {
    /// SW corner (sim coordinates).
    #[serde(rename = "coordSW")]
    coord_sw: SimCoord,
    /// NE corner (sim coordinates).
    #[serde(rename = "coordNE")]
    coord_ne: SimCoord,
    /// Margin width in meters.
    margin: &'a MarginMeters,
    /// Whether this area overlays another area/circle rather than the
    /// base wind directly.
    overlap: &'a Overlap,
    /// Wind override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    wind: Option<&'a WindOverride>,
    /// Wave override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    waves: Option<&'a WavesOverride>,
    /// Currents override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    currents: Option<&'a CurrentsOverride>,
}

impl<'a> From<&'a Area> for AreaSerializable<'a> {
    fn from(a: &'a Area) -> Self {
        Self {
            coord_sw: SimCoord::from_grid(&a.grid_rectangle, GridCorner::Sw),
            coord_ne: SimCoord::from_grid(&a.grid_rectangle, GridCorner::Ne),
            margin: &a.margin,
            overlap: &a.overlap,
            wind: a.wind.as_ref(),
            waves: a.waves.as_ref(),
            currents: a.currents.as_ref(),
        }
    }
}

/// Document-ordered list of [`Circle`]s.
///
/// Order matters: overlap layering applies in document order. Serializes
/// back as a JSON object keyed by `Circle::name` so it round-trips
/// against the original GLW JSON shape.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CircleList(pub Vec<Circle>);

impl CircleList {
    /// Borrow the underlying `Vec`.
    #[must_use]
    pub fn as_slice(&self) -> &[Circle] {
        &self.0
    }
    /// Number of circles in the list.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.0.len()
    }
    /// `true` when the list contains no circles.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl serde::Serialize for CircleList {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap as _;
        let mut map = serializer.serialize_map(Some(self.0.len()))?;
        for circle in &self.0 {
            map.serialize_entry(&circle.name, &CircleSerializable::from(circle))?;
        }
        map.end()
    }
}

/// Private view of a [`Circle`] without its `name` field, used only for
/// serialization.
#[derive(serde::Serialize)]
struct CircleSerializable<'a> {
    /// Sim containing the circle centre.
    #[serde(rename = "centerSim")]
    center_sim: SimCoord,
    /// Coordinates of the centre within `centerSim`.
    #[serde(rename = "centerPoint")]
    center_point: RegionXY,
    /// Circle radius in meters.
    radius: &'a RadiusMeters,
    /// Margin width in meters.
    margin: &'a MarginMeters,
    /// Whether this circle overlays another area/circle.
    overlap: &'a Overlap,
    /// Wind override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    wind: Option<&'a WindOverride>,
    /// Wave override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    waves: Option<&'a WavesOverride>,
    /// Currents override block.
    #[serde(skip_serializing_if = "Option::is_none")]
    currents: Option<&'a CurrentsOverride>,
}

impl<'a> From<&'a Circle> for CircleSerializable<'a> {
    fn from(c: &'a Circle) -> Self {
        Self {
            center_sim: SimCoord {
                x: c.center_sim.x(),
                y: c.center_sim.y(),
            },
            center_point: RegionXY {
                x: c.center_point.x(),
                y: c.center_point.y(),
            },
            radius: &c.radius,
            margin: &c.margin,
            overlap: &c.overlap,
            wind: c.wind.as_ref(),
            waves: c.waves.as_ref(),
            currents: c.currents.as_ref(),
        }
    }
}

/// Helper enum used by [`SimCoord::from_grid`] to pick a corner.
#[derive(Debug, Clone, Copy)]
enum GridCorner {
    /// South-west (lower-left) corner.
    Sw,
    /// North-east (upper-right) corner.
    Ne,
}

/// Serialization-side `{ "x": u16, "y": u16 }` shape (mirrors the
/// `GridCoordRaw` in `parse.rs`).
#[derive(Debug, Clone, Copy, serde::Serialize)]
struct SimCoord {
    /// Sim x grid coordinate.
    x: u32,
    /// Sim y grid coordinate.
    y: u32,
}

impl SimCoord {
    /// Project one corner of a [`sl_types::map::GridRectangle`] into the
    /// flat `{x,y}` shape used in JSON.
    fn from_grid(rect: &sl_types::map::GridRectangle, which: GridCorner) -> Self {
        use sl_types::map::GridRectangleLike as _;
        let gc = match which {
            GridCorner::Sw => rect.lower_left_corner(),
            GridCorner::Ne => rect.upper_right_corner(),
        };
        Self {
            x: gc.x(),
            y: gc.y(),
        }
    }
}

/// Serialization-side `{ "x": f32, "y": f32 }` shape for circle
/// centerPoints (z is intentionally dropped to match GLW JSON, which
/// only carries x and y).
#[derive(Debug, Clone, Copy, serde::Serialize)]
struct RegionXY {
    /// X meters from the western edge of the sim.
    x: f32,
    /// Y meters from the southern edge of the sim.
    y: f32,
}

/// A full GLW event as returned by `glwDataReq.php`.
///
/// Unknown top-level fields are silently ignored — the GLW author may
/// extend the schema, and unknown fields do not break parsing.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GlwEvent {
    /// Numeric event id assigned by the server.
    pub event_id: EventId,
    /// Free-text event name (e.g. `"TYC Cruise"`).
    pub event_name: String,
    /// Opaque event key string.
    pub event_key: GlwEventKey,
    /// Free-form event number (informational; not used by GLW).
    #[serde(default)]
    pub event_num: Option<u32>,
    /// Display name of the event director.
    pub director_name: String,
    /// Second Life UUID of the event director (stored as a string; this
    /// crate does not require the `uuid` dependency).
    pub director_key: String,
    /// Sail-mode flag. Documented as "not use for now"; kept optional
    /// for forward compatibility.
    #[serde(default)]
    pub sail_mode: Option<u8>,
    /// Opaque string field forwarded to boats (max 15 chars in the
    /// reference schema; not validated here).
    #[serde(default)]
    pub extra1: String,
    /// Opaque string field forwarded to boats (max 15 chars in the
    /// reference schema; not validated here).
    #[serde(default)]
    pub extra2: String,
    /// Base wind/waves/currents that apply unless overridden.
    pub base: Base,
    /// Per-area overrides, in document order. JSON key names are
    /// preserved on each [`Area`].
    #[serde(default, deserialize_with = "crate::parse::deserialize_area_list")]
    pub areas: AreaList,
    /// Per-circle overrides, in document order.
    #[serde(default, deserialize_with = "crate::parse::deserialize_circle_list")]
    pub circles: CircleList,
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn wind_direction_rejects_out_of_range() {
        assert!(WindDirection::try_new(0).is_ok(), "0 is in range");
        assert!(WindDirection::try_new(359).is_ok(), "359 is in range");
        assert!(WindDirection::try_new(360).is_err(), "360 is out of range");
        assert!(WindDirection::try_new(999).is_err(), "999 is out of range");
    }

    #[test]
    fn gusts_rejects_out_of_range() {
        assert!(GustsPercent::try_new(0).is_ok(), "0 is in range");
        assert!(GustsPercent::try_new(100).is_ok(), "100 is in range");
        assert!(GustsPercent::try_new(101).is_err(), "101 is out of range");
    }

    #[test]
    fn shifts_rejects_out_of_range() {
        assert!(ShiftsDegrees::try_new(180).is_ok(), "180 is in range");
        assert!(ShiftsDegrees::try_new(181).is_err(), "181 is out of range");
    }

    #[test]
    fn margin_clamps_at_100() {
        assert_eq!(MarginMeters::new_clamped(0).meters(), 0);
        assert_eq!(MarginMeters::new_clamped(100).meters(), 100);
        assert_eq!(MarginMeters::new_clamped(150).meters(), 100);
    }

    #[test]
    fn water_depth_rejects_zero_and_negative() {
        assert!(WaterDepth::try_new(1.0).is_ok(), "1.0 is positive");
        assert!(WaterDepth::try_new(0.0).is_err(), "0.0 is rejected");
        assert!(WaterDepth::try_new(-0.5).is_err(), "negative rejected");
    }

    #[test]
    fn water_depth_setting_maps_zero_to_disabled() -> Result<(), Box<dyn std::error::Error>> {
        let disabled: WaterDepthSetting = serde_json::from_str("0")?;
        assert_eq!(disabled, WaterDepthSetting::Disabled);
        let positive: WaterDepthSetting = serde_json::from_str("6.5")?;
        match positive {
            WaterDepthSetting::Set(d) => {
                assert!((d.meters() - 6.5).abs() < 1e-6, "6.5 round-trips");
            }
            WaterDepthSetting::Disabled => {
                return Err("6.5 must not become Disabled".into());
            }
        }
        let negative: Result<WaterDepthSetting, _> = serde_json::from_str("-1");
        assert!(negative.is_err(), "negative rejected");
        Ok(())
    }

    #[test]
    fn overlap_from_0_and_1() -> Result<(), Box<dyn std::error::Error>> {
        let off: Overlap = serde_json::from_str("0")?;
        assert_eq!(off, Overlap::new(false));
        let on: Overlap = serde_json::from_str("1")?;
        assert_eq!(on, Overlap::new(true));
        Ok(())
    }

    #[test]
    fn wave_effects_from_object() -> Result<(), Box<dyn std::error::Error>> {
        let we: WaveEffects = serde_json::from_str(r#"{"speed":1,"steer":0}"#)?;
        assert!(we.speed, "speed=1 turns on");
        assert!(!we.steer, "steer=0 stays off");
        Ok(())
    }

    #[test]
    fn wave_effects_from_bitmask() -> Result<(), Box<dyn std::error::Error>> {
        let we: WaveEffects = serde_json::from_str("3")?;
        assert!(we.speed && we.steer, "bitmask 3 = both on");
        let we: WaveEffects = serde_json::from_str("2")?;
        assert!(we.speed && !we.steer, "bitmask 2 = speed only");
        let we: WaveEffects = serde_json::from_str("1")?;
        assert!(!we.speed && we.steer, "bitmask 1 = steer only");
        let we: WaveEffects = serde_json::from_str("0")?;
        assert!(!we.speed && !we.steer, "bitmask 0 = neither");
        Ok(())
    }
}