stkcraft-time 0.2.0

NAIF/SPICE-aligned astronomical time scales in Rust: UTC, TAI, TT, TDB/ET, GPS, UT1 and SCLK, with LSK/SCLK/EOP kernels, no_std support, WebGeocalc parity and an MCP server.
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
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
//! Spacecraft clock (SCLK) kernels, type 1.
//!
//! A type-1 clock is a set of cascading integer counters. The SCLK kernel
//! stores, for one spacecraft:
//!
//! * `SCLK01_N_FIELDS_<id>` — number of fields,
//! * `SCLK01_MODULI_<id>` — modulus of each field,
//! * `SCLK01_OFFSETS_<id>` — offset added to each field value,
//! * `SCLK01_TIME_SYSTEM_<id>` — `1` for TDB/ET, `2` for TT (default 1),
//! * `SCLK01_COEFFICIENTS_<id>` — triplets `(ticks, parallel_seconds,
//!   rate)` describing the piecewise-linear map from encoded ticks to the
//!   parallel time system,
//! * `SCLK01_OUTPUT_DELIM_<id>` — delimiter used in formatted strings.
//!
//! The encoded tick count of a field vector is
//!
//! ```text
//! ticks = (((f0 - o0) * m1 + (f1 - o1)) * m2 + (f2 - o2)) * ...
//! ```
//!
//! The offsets give the value of the first count of each field. For
//! Voyager, the third field starts at 1, so its offset is 1 and the
//! encoded counter runs from 0 to 799.
//!
//! and the number of ticks in one count of the most significant field is
//! `m1 * m2 * ...`. The spacing between successive coefficient samples is
//! therefore `rate / ticks_per_msb` seconds per tick, which is the
//! behaviour verified against CSPICE `sct2e`.
//!
//! # Scope
//!
//! Encoded-tick/time conversions enforce the cumulative duration of the
//! declared partitions, rounding the accumulated raw durations as SCTE01.
//! Native-style encoded string conversion is available separately from the
//! legacy field API. Partitioned SclkClock strings use discrete SCET01 ticks;
//! continuous tick conversion rejects values outside its own domain. A map
//! without partition metadata has no declared upper bound.
//!
//! Only clock type 1 is supported. No SCLK kernel is embedded at runtime; load the
//! mission kernel you need at deployment time.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::convert::{tai_to_et, tai_to_tt};
use crate::epoch::Epoch;
use crate::error::TimeError;
use crate::leap::LeapSecondTable;
use crate::math;

/// The parallel time system that SCLK coefficients map to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SclkTimeSystem {
    /// Barycentric Dynamical Time: the coefficients give ET seconds past
    /// J2000 directly.
    Tdb,
    /// Terrestrial Dynamical Time: the coefficients give TT seconds past
    /// J2000, and the LSK is needed to reach ET.
    Tdt,
}

/// One piecewise-linear segment of the tick-to-time map.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SclkCoefficient {
    /// Encoded tick count at the start of the segment.
    pub ticks: f64,
    /// Parallel-time value at the start of the segment, in seconds.
    pub seconds: f64,
    /// Parallel-time rate per most-significant-field count.
    pub rate: f64,
}

/// Error symbol for partition-aware SCLK string conversion.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SclkStringError(&'static str);

impl SclkStringError {
    /// Return the CSPICE short error symbol, or `STKCRAFT(NONFINITE)` for
    /// the library's explicit non-finite input guard.
    pub fn spice_short(self) -> &'static str {
        self.0
    }
}

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

#[cfg(feature = "std")]
impl std::error::Error for SclkStringError {}

/// State of NAIF's type-1 SCLK component parser across calls.
///
/// Keep one context for each independent native-style kernel-pool history.
/// A failed numeric component can affect a later blank component until a
/// numeric component is successfully parsed.
#[derive(Clone, Debug, Default)]
pub struct SclkStringContext {
    component_error: bool,
}

/// A parsed type-1 SCLK kernel for one spacecraft clock.
#[derive(Clone, Debug)]
pub struct SclkKernel {
    spacecraft_id: i32,
    data_type: i32,
    time_system: SclkTimeSystem,
    n_fields: usize,
    moduli: Vec<i64>,
    offsets: Vec<i64>,
    coefficients: Vec<SclkCoefficient>,
    delimiter: char,
    native_delimiter_code: Option<i64>,
    partition_start: Vec<f64>,
    partition_end: Vec<f64>,
    max_encoded_ticks: Option<f64>,
}

impl SclkKernel {
    /// Decode continuous encoded ticks to a partition-prefixed NAIF clock string.
    pub fn decode_native_sclk(&self, ticks: f64) -> Result<String, SclkStringError> {
        self.validate_native_string_metadata()?;
        let totals = self.partition_totals(false)?;
        if !ticks.is_finite() {
            return Err(SclkStringError("STKCRAFT(NONFINITE)"));
        }
        let ticks = native_nint(ticks);
        let last = *totals
            .last()
            .ok_or(SclkStringError("SPICE(KERNELVARNOTFOUND)"))?;
        let part = if ticks == last {
            totals.len() - 1
        } else {
            totals
                .iter()
                .position(|&end| ticks < end)
                .ok_or(SclkStringError("SPICE(VALUEOUTOFRANGE)"))?
        };
        if ticks < 0.0 {
            return Err(SclkStringError("SPICE(VALUEOUTOFRANGE)"));
        }
        let readout = if part == 0 {
            ticks + self.partition_start[part]
        } else {
            ticks + self.partition_start[part] - totals[part - 1]
        };
        let mut factors = alloc::vec![1.0; self.n_fields];
        for index in (0..self.n_fields - 1).rev() {
            factors[index] = factors[index + 1] * self.moduli[index + 1] as f64;
        }
        let mut remaining = native_nint(readout);
        if remaining < 0.0 || !remaining.is_finite() {
            return Err(SclkStringError("SPICE(VALUEOUTOFRANGE)"));
        }
        let mut parts = Vec::with_capacity(self.n_fields);
        for (index, &factor) in factors.iter().enumerate() {
            let component = if index + 1 == self.n_fields {
                remaining + self.offsets[index] as f64
            } else {
                let value = math::floor(remaining / factor) + self.offsets[index] as f64;
                remaining %= factor;
                value
            };
            let max_width = self.moduli[index] as f64 + self.offsets[index] as f64 - 1.0;
            if max_width + 0.5 <= 0.0 || !component.is_finite() {
                return Err(SclkStringError("SPICE(INVALIDSCLKSTRING)"));
            }
            let width = (native_log10(max_width + 0.5) as i32 + 1).max(0) as usize;
            // SCFM01 removes DPSTRF's leading sign character, including
            // for negative field values induced by a negative offset.
            let component = native_clock_component(component);
            let mut padded = String::new();
            if component.len() > 28 {
                // The 30-byte DPSTRF buffer cannot contain a sign, this
                // integer, and a decimal point. SCFM01 then observes a
                // missing decimal and pads the field width plus two zeros.
                for _ in 0..width + 2 {
                    padded.push('0');
                }
            } else {
                for _ in component.len()..width {
                    padded.push('0');
                }
                padded.push_str(&component);
            }
            parts.push(padded);
        }
        Ok(format!(
            "{}/{}",
            part + 1,
            parts.join(&self.delimiter.to_string())
        ))
    }

    /// Encode a clock string using a fresh NAIF component-parser context.
    ///
    /// Use [`Self::encode_native_sclk_with_context`] to reproduce the native
    /// parser's state across successive calls.
    pub fn encode_native_sclk(&self, text: &str) -> Result<f64, SclkStringError> {
        self.encode_native_sclk_with_context(text, &mut SclkStringContext::default())
    }

    /// Encode a clock string while preserving native parser state across calls.
    pub fn encode_native_sclk_with_context(
        &self,
        text: &str,
        context: &mut SclkStringContext,
    ) -> Result<f64, SclkStringError> {
        let text = text.split('\0').next().unwrap_or("");
        if text.is_empty() {
            return Err(SclkStringError("SPICE(EMPTYSTRING)"));
        }
        let invalid = SclkStringError("SPICE(INVALIDSCLKSTRING)");
        let bad_partition = SclkStringError("SPICE(BADPARTNUMBER)");
        self.validate_native_string_metadata()?;
        let totals = self.partition_totals(true)?;
        let (partition, clock) = match text.find('/') {
            Some(0) => return Err(bad_partition),
            Some(index) if index + 1 == text.len() => return Err(invalid),
            Some(index) => (Some(&text[..index]), &text[index + 1..]),
            None => (None, text),
        };
        if clock.trim_matches(' ').is_empty() {
            return Err(invalid);
        }
        let fields = native_clock_fields(clock);
        if fields.len() > self.n_fields {
            return Err(invalid);
        }
        let mut factors = alloc::vec![1.0; self.n_fields];
        for index in (0..self.n_fields - 1).rev() {
            factors[index] = factors[index + 1] * self.moduli[index + 1] as f64;
        }
        let mut ticks = 0.0;
        for (index, field) in fields.iter().enumerate() {
            let value = if field.is_empty() {
                if context.component_error {
                    return Err(invalid);
                }
                self.offsets[index] as f64
            } else {
                match core::str::from_utf8(field)
                    .ok()
                    .and_then(parse_native_clock_number)
                {
                    Some(value) => {
                        context.component_error = false;
                        value
                    }
                    None => {
                        context.component_error = true;
                        return Err(invalid);
                    }
                }
            } - self.offsets[index] as f64;
            if native_nint(value) < 0.0 {
                return Err(invalid);
            }
            ticks += value * factors[index];
        }
        ticks = native_nint(ticks);
        let starts: Vec<f64> = self
            .partition_start
            .iter()
            .copied()
            .map(native_nint)
            .collect();
        let ends: Vec<f64> = self
            .partition_end
            .iter()
            .copied()
            .map(native_nint)
            .collect();
        let part = if let Some(number) = partition {
            let value = parse_native_clock_number(number).ok_or(bad_partition)?;
            if value < 1.0 || value >= (starts.len() + 1) as f64 {
                return Err(bad_partition);
            }
            let index = value as usize - 1;
            if ticks < starts[index] || ticks > ends[index] {
                return Err(SclkStringError("SPICE(NOTINPART)"));
            }
            index
        } else {
            starts
                .iter()
                .zip(&ends)
                .position(|(&start, &end)| ticks >= start && ticks <= end)
                .ok_or(SclkStringError("SPICE(NOPARTITION)"))?
        };
        Ok(if part == 0 {
            ticks - starts[part]
        } else {
            ticks - starts[part] + totals[part - 1]
        })
    }

    fn validate_native_string_metadata(&self) -> Result<(), SclkStringError> {
        if self.n_fields > 10 {
            return Err(SclkStringError("SPICE(VALUEOUTOFRANGE)"));
        }
        match self.native_delimiter_code {
            None => Err(SclkStringError("SPICE(KERNELVARNOTFOUND)")),
            Some(1..=5) => Ok(()),
            Some(_) => Err(SclkStringError("SPICE(VALUEOUTOFRANGE)")),
        }
    }

    // SCDECD's LSTLED search selects partition 2 for a NaN tick when at
    // least two partitions exist. The exercised N0067 SCFM01 builds render
    // NaN components as zero-filled fields. This path is used only for an
    // indeterminate intermediate generated from finite ET, not for a
    // caller-supplied non-finite tick.
    fn decode_indeterminate_ticks(&self) -> Result<String, SclkStringError> {
        self.validate_native_string_metadata()?;
        let totals = self.partition_totals(false)?;
        if totals.len() < 2 {
            return Err(SclkStringError("SPICE(VALUEOUTOFRANGE)"));
        }
        let mut parts = Vec::with_capacity(self.n_fields);
        for index in 0..self.n_fields {
            let max_width = self.moduli[index] as f64 + self.offsets[index] as f64 - 1.0;
            if max_width + 0.5 <= 0.0 {
                return Err(SclkStringError("SPICE(INVALIDSCLKSTRING)"));
            }
            let width = (native_log10(max_width + 0.5) as i32 + 1).max(0) as usize;
            let mut field = String::new();
            for _ in 0..width {
                field.push('0');
            }
            parts.push(field);
        }
        Ok(format!("2/{}", parts.join(&self.delimiter.to_string())))
    }

    fn partition_totals(&self, round_endpoints: bool) -> Result<Vec<f64>, SclkStringError> {
        if self.partition_start.is_empty() {
            return Err(SclkStringError("SPICE(KERNELVARNOTFOUND)"));
        }
        if self.partition_start.len() > 9999 {
            return Err(SclkStringError("SPICE(TOOMANYPARTITIONS)"));
        }
        let mut totals = Vec::with_capacity(self.partition_start.len());
        let mut total = 0.0;
        for (&start, &end) in self.partition_start.iter().zip(&self.partition_end) {
            let (start, end) = if round_endpoints {
                (native_nint(start), native_nint(end))
            } else {
                (start, end)
            };
            total = native_nint(total + end - start);
            totals.push(total);
        }
        Ok(totals)
    }
    /// Parses a `KPL/SCLK` text kernel for the given clock.
    ///
    /// `spacecraft_id` may be given with either sign; kernel variables are
    /// looked up with the magnitude, matching NAIF kernels such as
    /// `MRO_SCLKSCET` (variables `_74`, clock id `-74`).
    pub fn parse(text: &str, spacecraft_id: i32) -> Result<Self, TimeError> {
        let tokens = tokenize(&extract_data(text)?);

        let n_fields_token = find_var(&tokens, "SCLK01_N_FIELDS", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_N_FIELDS"))?;
        let n_fields_values = parse_list(&tokens, n_fields_token)?;
        let n_fields = parse_i64(first(n_fields_values)?)?;
        if n_fields <= 0 || n_fields > 64 {
            return Err(TimeError::SclkParse);
        }
        let n_fields = n_fields as usize;

        let data_type = match find_var(&tokens, "SCLK_DATA_TYPE", spacecraft_id) {
            Some(index) => i32::try_from(parse_i64(first(parse_list(&tokens, index)?)?)?)
                .map_err(|_| TimeError::SclkParse)?,
            None => 1,
        };
        if data_type != 1 {
            return Err(TimeError::SclkUnsupported);
        }

        let time_system = match find_var(&tokens, "SCLK01_TIME_SYSTEM", spacecraft_id) {
            Some(index) => match parse_i64(first(parse_list(&tokens, index)?)?)? {
                1 => SclkTimeSystem::Tdb,
                2 => SclkTimeSystem::Tdt,
                _ => return Err(TimeError::SclkParse),
            },
            None => SclkTimeSystem::Tdb,
        };

        let moduli_index = find_var(&tokens, "SCLK01_MODULI", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_MODULI"))?;
        let moduli = parse_i64_list(&tokens, moduli_index)?;
        if moduli.len() != n_fields || moduli.iter().any(|&value| value <= 0) {
            return Err(TimeError::SclkParse);
        }

        let offsets = match find_var(&tokens, "SCLK01_OFFSETS", spacecraft_id) {
            Some(index) => parse_i64_list(&tokens, index)?,
            None => alloc::vec![0i64; n_fields],
        };
        if offsets.len() != n_fields {
            return Err(TimeError::SclkParse);
        }

        let coefficients_index = find_var(&tokens, "SCLK01_COEFFICIENTS", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_COEFFICIENTS"))?;
        let coefficient_values = parse_list(&tokens, coefficients_index)?;
        if coefficient_values.len() < 3 || coefficient_values.len() % 3 != 0 {
            return Err(TimeError::SclkParse);
        }
        let mut coefficients = Vec::new();
        let mut previous_ticks = f64::NEG_INFINITY;
        let mut previous_seconds = f64::NEG_INFINITY;
        for chunk in coefficient_values.chunks(3) {
            let ticks = parse_f64(chunk[0])?;
            let seconds = parse_f64(chunk[1])?;
            let rate = parse_f64(chunk[2])?;
            if ticks <= previous_ticks || seconds <= previous_seconds || rate <= 0.0 {
                return Err(TimeError::SclkParse);
            }
            previous_ticks = ticks;
            previous_seconds = seconds;
            coefficients.push(SclkCoefficient {
                ticks,
                seconds,
                rate,
            });
        }

        let native_delimiter_code = match find_var(&tokens, "SCLK01_OUTPUT_DELIM", spacecraft_id) {
            Some(index) => Some(parse_i64(first(parse_list(&tokens, index)?)?)?),
            None => None,
        };
        let delimiter = native_delimiter_code
            .map(delimiter_from_code)
            .unwrap_or('.');

        let partition_start = match find_var(&tokens, "SCLK_PARTITION_START", spacecraft_id) {
            Some(index) => parse_f64_list(&tokens, index)?,
            None => Vec::new(),
        };
        let partition_end = match find_var(&tokens, "SCLK_PARTITION_END", spacecraft_id) {
            Some(index) => parse_f64_list(&tokens, index)?,
            None => Vec::new(),
        };

        if partition_start.len() != partition_end.len() {
            return Err(TimeError::SclkParse);
        }
        // Only per-partition consistency is required. NAIF lets a clock
        // readout jump ahead or regress, which is exactly why partitions
        // exist, so a later partition may start at a lower readout than
        // the previous one ended. The encoded domain uses their durations.
        for (start, end) in partition_start.iter().zip(partition_end.iter()) {
            if !start.is_finite() || !end.is_finite() || start > end {
                return Err(TimeError::SclkParse);
            }
        }
        // SCTE01 and SCET01 round each accumulated raw partition duration.
        // This differs from SCENCD, which rounds each endpoint separately.
        let max_encoded_ticks = if partition_start.is_empty() {
            None
        } else {
            let total = partition_start
                .iter()
                .zip(&partition_end)
                .fold(0.0, |total, (&start, &end)| {
                    native_nint((end - start) + total)
                });
            if !total.is_finite() || total < 0.0 {
                return Err(TimeError::SclkParse);
            }
            Some(total)
        };

        Ok(Self {
            spacecraft_id,
            data_type,
            time_system,
            n_fields,
            moduli,
            offsets,
            coefficients,
            delimiter,
            native_delimiter_code,
            partition_start,
            partition_end,
            max_encoded_ticks,
        })
    }

    /// The clock id this kernel was parsed for.
    pub fn spacecraft_id(&self) -> i32 {
        self.spacecraft_id
    }

    /// The clock type. Only type 1 is supported.
    pub fn data_type(&self) -> i32 {
        self.data_type
    }

    /// The parallel time system of the coefficients.
    pub fn time_system(&self) -> SclkTimeSystem {
        self.time_system
    }

    /// Number of fields in a formatted clock string.
    pub fn n_fields(&self) -> usize {
        self.n_fields
    }

    /// Field moduli.
    pub fn moduli(&self) -> &[i64] {
        &self.moduli
    }

    /// Field offsets.
    pub fn offsets(&self) -> &[i64] {
        &self.offsets
    }

    /// Piecewise-linear coefficients.
    pub fn coefficients(&self) -> &[SclkCoefficient] {
        &self.coefficients
    }

    /// Partition readouts used to bound the cumulative encoded domain.
    pub fn partitions(&self) -> (&[f64], &[f64]) {
        (&self.partition_start, &self.partition_end)
    }

    /// Number of ticks per count of the most significant field.
    pub fn ticks_per_msb(&self) -> f64 {
        let mut product = 1.0;
        for &modulus in self.moduli[1..].iter().rev() {
            product *= modulus as f64;
        }
        product
    }

    /// Encodes a field vector as a total tick count.
    pub fn ticks_from_fields(&self, fields: &[i64]) -> Result<f64, TimeError> {
        self.ensure_partitionless()?;
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }

        let mut ticks: i64 = 0;
        for (index, &field) in fields.iter().enumerate() {
            let value = field
                .checked_sub(self.offsets[index])
                .ok_or(TimeError::SclkParse)?;
            if value < 0 {
                return Err(TimeError::SclkParse);
            }
            ticks = if index == 0 {
                value
            } else {
                ticks
                    .checked_mul(self.moduli[index])
                    .and_then(|partial| partial.checked_add(value))
                    .ok_or(TimeError::SclkParse)?
            };
        }

        Ok(ticks as f64)
    }

    /// Decodes a total tick count into a field vector.
    ///
    /// Partition boundaries are not applied; the fields are those of the
    /// continuous tick encoding.
    pub fn fields_from_ticks(&self, ticks: f64) -> Result<Vec<i64>, TimeError> {
        self.ensure_partitionless()?;
        if !(ticks.is_finite() && ticks >= 0.0) {
            return Err(TimeError::SclkParse);
        }
        let rounded = math::round(ticks);
        if (ticks - rounded).abs() > 1e-6 {
            return Err(TimeError::SclkParse);
        }
        let mut remainder = rounded as i64;

        let mut fields = alloc::vec![0i64; self.n_fields];

        for index in (1..self.n_fields).rev() {
            let modulus = self.moduli[index];
            fields[index] = remainder % modulus + self.offsets[index];
            remainder /= modulus;
        }

        if remainder >= self.moduli[0] {
            return Err(TimeError::SclkParse);
        }
        fields[0] = remainder + self.offsets[0];

        Ok(fields)
    }

    /// Converts encoded ticks to the parallel time system, in seconds.
    pub fn parallel_seconds(&self, ticks: f64) -> Result<f64, TimeError> {
        if !ticks.is_finite() {
            return Err(TimeError::SclkParse);
        }
        let segment = self.segment_for_ticks(ticks)?;
        let rate = segment.rate / self.ticks_per_msb();
        Ok(segment.seconds + rate * (ticks - segment.ticks))
    }

    /// Converts a parallel-time value in seconds to encoded ticks.
    pub fn ticks_from_parallel(&self, seconds: f64) -> Result<f64, TimeError> {
        if !seconds.is_finite() {
            return Err(TimeError::SclkParse);
        }
        let segment = self.segment_for_seconds(seconds)?;
        if segment.rate == 0.0 {
            return Err(TimeError::SclkParse);
        }
        let rate = 1.0 / (segment.rate / self.ticks_per_msb());
        let ticks = segment.ticks + rate * (seconds - segment.seconds);
        self.validate_ticks(ticks)?;
        Ok(ticks)
    }

    /// Converts encoded ticks to `ET` (TDB seconds past J2000).
    pub fn ticks_to_et(&self, ticks: f64, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        let parallel = self.parallel_seconds(ticks)?;
        match self.time_system {
            SclkTimeSystem::Tdb => Ok(parallel),
            SclkTimeSystem::Tdt => Ok(crate::naif::tt_to_et(parallel, lsk)),
        }
    }

    pub(crate) fn classify_native_ticks_error(
        &self,
        ticks: f64,
        error: TimeError,
    ) -> NativeSclkError {
        if error == TimeError::SclkParse
            && ticks.is_finite()
            && (ticks < 0.0
                || self.max_encoded_ticks.is_some_and(|max| ticks > max)
                || self
                    .coefficients
                    .first()
                    .is_some_and(|first| ticks < first.ticks))
        {
            NativeSclkError::Native(SclkStringError("SPICE(VALUEOUTOFRANGE)"))
        } else {
            NativeSclkError::Time(error)
        }
    }

    /// Converts `ET` (TDB seconds past J2000) to encoded ticks.
    pub fn et_to_ticks(&self, et: f64, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        match self.time_system {
            SclkTimeSystem::Tdb => self.ticks_from_parallel(et),
            SclkTimeSystem::Tdt => self.ticks_from_parallel(crate::naif::et_to_tt(et, lsk)),
        }
    }

    /// Converts encoded ticks to a TAI epoch.
    ///
    /// Returns [`TimeError::Overflow`] when the result cannot be represented
    /// as a finite integer epoch.
    pub fn ticks_to_tai(&self, ticks: f64, lsk: &LeapSecondTable) -> Result<Epoch, TimeError> {
        let parallel = self.parallel_seconds(ticks)?;
        let tai = match self.time_system {
            SclkTimeSystem::Tdb => {
                let mean_anomaly = lsk.m0 + lsk.m1 * parallel;
                let eccentric_anomaly = mean_anomaly + lsk.eb * math::sin(mean_anomaly);
                parallel - lsk.delta_t_a + 43_200.0 - lsk.k * math::sin(eccentric_anomaly)
            }
            SclkTimeSystem::Tdt => parallel - lsk.delta_t_a + 43_200.0,
        };
        Epoch::checked_from_tai_seconds(tai).ok_or(TimeError::Overflow)
    }

    // SCET01 rounds the interpolated ticks before checking the clock range.
    pub(crate) fn et_to_discrete_ticks(
        &self,
        et: f64,
        lsk: &LeapSecondTable,
    ) -> Result<f64, TimeError> {
        let parallel = match self.time_system {
            SclkTimeSystem::Tdb => et,
            SclkTimeSystem::Tdt => crate::naif::et_to_tt(et, lsk),
        };
        let segment = self.segment_for_seconds(parallel)?;
        if segment.rate <= 0.0 {
            return Err(TimeError::SclkParse);
        }
        let rate = 1.0 / (segment.rate / self.ticks_per_msb());
        let ticks = native_nint(segment.ticks + rate * (parallel - segment.seconds));
        if !ticks.is_finite()
            || ticks < native_nint(self.coefficients[0].ticks)
            || self.max_encoded_ticks.is_some_and(|max| ticks > max)
        {
            return Err(TimeError::SclkParse);
        }
        Ok(ticks)
    }

    /// Converts a TAI epoch to encoded ticks.
    pub fn tai_to_ticks(&self, epoch: Epoch, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        let parallel = match self.time_system {
            SclkTimeSystem::Tdb => tai_to_et(epoch, lsk),
            SclkTimeSystem::Tdt => tai_to_tt(epoch, lsk),
        };
        self.ticks_from_parallel(parallel)
    }

    /// Parses a formatted clock string into a field vector.
    pub fn parse_string(&self, sclk: &str) -> Result<Vec<i64>, TimeError> {
        self.ensure_partitionless()?;
        let mut fields = Vec::new();
        for part in sclk.split(self.delimiter) {
            fields.push(
                part.trim()
                    .parse::<i64>()
                    .map_err(|_| TimeError::SclkParse)?,
            );
        }
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }
        Ok(fields)
    }

    /// Formats a field vector as a clock string.
    pub fn format_string(&self, fields: &[i64]) -> Result<String, TimeError> {
        self.ensure_partitionless()?;
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }
        let delimiter = self.delimiter.to_string();
        let parts: Vec<String> = fields.iter().map(|value| value.to_string()).collect();
        Ok(parts.join(&delimiter))
    }

    /// Fields and strings are only defined without partition offsets in
    /// this version; keep encoded ticks available for partition-aware
    /// callers that apply the boundaries themselves.
    fn ensure_partitionless(&self) -> Result<(), TimeError> {
        if self.partition_start.is_empty() && self.partition_end.is_empty() {
            return Ok(());
        }
        if self.partition_start.len() == 1 && self.partition_start[0] == 0.0 {
            return Ok(());
        }
        Err(TimeError::SclkPartitionsUnsupported)
    }

    fn segment_for_ticks(&self, ticks: f64) -> Result<SclkCoefficient, TimeError> {
        self.validate_ticks(ticks)?;
        if self.coefficients.is_empty() {
            return Err(TimeError::SclkMissing("SCLK01_COEFFICIENTS"));
        }
        let index = self
            .coefficients
            .partition_point(|segment| segment.ticks <= ticks);
        let index = index.checked_sub(1).ok_or(TimeError::SclkParse)?;
        Ok(self.coefficients[index])
    }

    fn validate_ticks(&self, ticks: f64) -> Result<(), TimeError> {
        if !ticks.is_finite()
            || ticks < 0.0
            || self.max_encoded_ticks.is_some_and(|max| ticks > max)
            || self
                .coefficients
                .first()
                .is_some_and(|first| ticks < first.ticks)
        {
            return Err(TimeError::SclkParse);
        }
        Ok(())
    }

    fn segment_for_seconds(&self, seconds: f64) -> Result<SclkCoefficient, TimeError> {
        if !seconds.is_finite() {
            return Err(TimeError::SclkParse);
        }
        if self.coefficients.is_empty() {
            return Err(TimeError::SclkMissing("SCLK01_COEFFICIENTS"));
        }
        let index = self
            .coefficients
            .partition_point(|segment| segment.seconds <= seconds);
        let index = if index == 0 { 0 } else { index - 1 };
        Ok(self.coefficients[index])
    }
}

// f2c's d_lg10 uses this operation order instead of the platform log10.
fn native_log10(value: f64) -> f64 {
    core::f64::consts::LOG10_E * math::ln(value)
}

// CSPICE's f2c implementation evaluates the addition before floor.
fn native_nint(value: f64) -> f64 {
    if value >= 0.0 {
        math::floor(value + 0.5)
    } else {
        -math::floor(0.5 - value)
    }
}

// SCFM01 uses DPSTRF, whose DPSTR decimal conversion preserves at most
// fourteen significant digits. Integer components below 10^14 are exact.
fn native_clock_component(value: f64) -> String {
    let value = math::abs(value);
    if value < 1.0e14 {
        return format!("{value:.0}");
    }
    const INVERSE_POWERS: [f64; 17] = [
        1.0, 0.1, 0.01, 0.001, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13,
        1e-14, 1e-15, 1e-16,
    ];
    let mut exponent = native_log10(value) as usize;
    let mut scaled = value;
    let mut remaining = exponent;
    while remaining > 16 {
        scaled *= 1e-16;
        remaining -= 16;
    }
    scaled *= INVERSE_POWERS[remaining];
    scaled = (native_nint(scaled * 1e13) + 0.125) * 1e-13;
    if scaled >= 10.0 {
        scaled *= 0.1;
        exponent += 1;
    }
    let mut output = String::new();
    for _ in 0..14 {
        let digit = scaled as u8;
        output.push(char::from(b'0' + digit));
        scaled = (scaled - f64::from(digit)) * 10.0;
    }
    for _ in 14..=exponent {
        output.push('0');
    }
    output
}

// LPARSM treats repeated non-space delimiters as blank components, while
// whitespace adjacent to another delimiter does not add a component.
fn native_clock_fields(clock: &str) -> Vec<&[u8]> {
    let bytes = clock.as_bytes();
    let mut end = bytes.len();
    while end > 0 && bytes[end - 1] == b' ' {
        end -= 1;
    }
    let mut begin = 0;
    let mut fields = Vec::new();
    while begin < end && fields.len() < 10 {
        while begin < end && bytes[begin] == b' ' {
            begin += 1;
        }
        let mut next = begin;
        while next < end && !b".:-, ".contains(&bytes[next]) {
            next += 1;
        }
        if next < end && bytes[next] == b' ' {
            while next < end && bytes[next] == b' ' {
                next += 1;
            }
            if next < end && !b".:-, ".contains(&bytes[next]) {
                next -= 1;
            }
        }
        let stop = next.min(begin.saturating_add(30));
        let mut field = &bytes[begin..stop];
        while field.first() == Some(&b' ') {
            field = &field[1..];
        }
        while field.last() == Some(&b' ') {
            field = &field[..field.len() - 1];
        }
        fields.push(field);
        begin = next + 1;
    }
    if end > 0 && b".:-,".contains(&bytes[end - 1]) && fields.len() < 10 {
        fields.push(&[]);
    }
    fields
}

// NPARSD accepts PI, an implicit exponent mantissa, and exponent strings
// whose digit accumulation stops on underflow. Preserve its operation order
// through the existing NAIF numeric arithmetic after lexical normalization.
fn parse_native_clock_number(token: &str) -> Option<f64> {
    let text: String = token
        .chars()
        .filter(|c| *c != ' ' && *c != ',')
        .map(|c| c.to_ascii_uppercase())
        .collect();
    let (sign, unsigned) = match text.as_bytes().first() {
        Some(b'-') => ("-", &text[1..]),
        Some(b'+') => ("+", &text[1..]),
        _ => ("", text.as_str()),
    };
    if unsigned == "PI" {
        return Some(if sign == "-" {
            -core::f64::consts::PI
        } else {
            core::f64::consts::PI
        });
    }
    let position = unsigned.find(['E', 'D']);
    let Some(position) = position else {
        return crate::naif::parse_f64(&text);
    };
    let mantissa = &unsigned[..position];
    let mut integer = 0.0;
    let mut fraction = 0.0;
    let mut decimal = false;
    let mut zero_integer = true;
    let mut shift = 0_i64;
    let mut digits_found = false;
    for byte in mantissa.bytes() {
        if byte == b'.' && !decimal {
            decimal = true;
            zero_integer = integer == 0.0;
            continue;
        }
        if !byte.is_ascii_digit() {
            return None;
        }
        digits_found = true;
        let digit = f64::from(byte - b'0');
        if !decimal {
            if integer < 1e15 {
                integer = integer * 10.0 + digit;
            } else {
                shift += 1;
            }
        } else if zero_integer && fraction < 1e15 {
            fraction = fraction * 10.0 + digit;
            shift -= 1;
        }
    }
    let raw_exponent = &unsigned[position + 1..];
    let (exp_sign, exponent) = if let Some(value) = raw_exponent.strip_prefix('-') {
        (-1_i64, value)
    } else {
        (
            1_i64,
            raw_exponent.strip_prefix('+').unwrap_or(raw_exponent),
        )
    };
    let mut power = 0_i64;
    for digit in exponent.bytes() {
        if !digit.is_ascii_digit() || power + shift > 308 {
            return None;
        }
        if power + shift >= -309 {
            power = power * 10 + exp_sign * i64::from(digit - b'0');
        }
    }
    let mantissa = if digits_found { mantissa } else { "1" };
    crate::naif::parse_f64(&format!("{sign}{mantissa}E{power}"))
}

/// A spacecraft clock bound to a leap-second kernel.
///
/// The [`SclkMap`] trait needs the LSK for the `TT -> ET` step of clocks
/// whose coefficients are in TT.
pub struct SclkClock<'a> {
    /// Parsed SCLK kernel.
    pub kernel: &'a SclkKernel,
    /// Leap-second kernel used to relate TT and ET.
    pub lsk: &'a LeapSecondTable,
}

/// Error from a native-style SCLK string/ET conversion.
///
/// String parsing errors retain their CSPICE short symbol. Errors from the
/// existing time conversion path remain distinguishable and are not assigned
/// a guessed CSPICE symbol.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NativeSclkError {
    /// Native clock string encoding or decoding failed.
    String(SclkStringError),
    /// A native tick/time range check failed with a CSPICE short symbol.
    Native(SclkStringError),
    /// The encoded ticks/ET conversion failed.
    Time(TimeError),
}

impl core::fmt::Display for NativeSclkError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::String(error) | Self::Native(error) => error.fmt(f),
            Self::Time(error) => error.fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for NativeSclkError {}

impl SclkClock<'_> {
    /// Convert a partition-aware native clock string to ET.
    ///
    /// # Errors
    /// Preserves string errors separately from tick/time conversion errors.
    pub fn native_sclk_to_et(&self, clock: &str) -> Result<f64, NativeSclkError> {
        self.native_sclk_to_et_with_context(clock, &mut SclkStringContext::default())
    }

    /// Convert a native clock string to ET while retaining parser state.
    ///
    /// # Errors
    /// Preserves string errors separately from tick/time conversion errors.
    pub fn native_sclk_to_et_with_context(
        &self,
        clock: &str,
        context: &mut SclkStringContext,
    ) -> Result<f64, NativeSclkError> {
        let ticks = self
            .kernel
            .encode_native_sclk_with_context(clock, context)
            .map_err(NativeSclkError::String)?;
        self.kernel
            .ticks_to_et(ticks, self.lsk)
            .map_err(|error| self.kernel.classify_native_ticks_error(ticks, error))
    }

    /// Convert ET to a partition-aware native clock string.
    ///
    /// # Errors
    /// Preserves string errors separately from tick/time conversion errors.
    pub fn native_et_to_sclk(&self, et: f64) -> Result<String, NativeSclkError> {
        if et.is_finite() {
            let parallel = match self.kernel.time_system {
                SclkTimeSystem::Tdb => et,
                SclkTimeSystem::Tdt => crate::naif::et_to_tt(et, self.lsk),
            };
            if parallel.is_finite() {
                if let Ok(segment) = self.kernel.segment_for_seconds(parallel) {
                    let rate = 1.0 / (segment.rate / self.kernel.ticks_per_msb());
                    let ticks = native_nint(segment.ticks + rate * (parallel - segment.seconds));
                    if ticks.is_nan() {
                        return self
                            .kernel
                            .decode_indeterminate_ticks()
                            .map_err(NativeSclkError::Native);
                    }
                }
            }
        }
        let ticks = self
            .kernel
            .et_to_discrete_ticks(et, self.lsk)
            .map_err(|error| {
                if error == TimeError::SclkParse {
                    let parallel = match self.kernel.time_system {
                        SclkTimeSystem::Tdb => et,
                        SclkTimeSystem::Tdt => crate::naif::et_to_tt(et, self.lsk),
                    };
                    if let Ok(segment) = self.kernel.segment_for_seconds(parallel) {
                        let rate = 1.0 / (segment.rate / self.kernel.ticks_per_msb());
                        let ticks =
                            native_nint(segment.ticks + rate * (parallel - segment.seconds));
                        if et.is_finite()
                            && parallel.is_finite()
                            && !ticks.is_nan()
                            && (ticks < native_nint(self.kernel.coefficients[0].ticks)
                                || self.kernel.max_encoded_ticks.is_some_and(|max| ticks > max))
                        {
                            return NativeSclkError::Native(SclkStringError(
                                "SPICE(VALUEOUTOFRANGE)",
                            ));
                        }
                    }
                }
                NativeSclkError::Time(error)
            })?;
        self.kernel
            .decode_native_sclk(ticks)
            .map_err(NativeSclkError::String)
    }
}

impl SclkMap for SclkClock<'_> {
    fn sclk_to_et(&self, sclk: &str) -> Result<f64, TimeError> {
        if !self.kernel.partition_start.is_empty()
            && (sclk.contains('/') || self.kernel.ensure_partitionless().is_err())
        {
            let ticks = self
                .kernel
                .encode_native_sclk(sclk)
                .map_err(|_| TimeError::SclkParse)?;
            return self.kernel.ticks_to_et(ticks, self.lsk);
        }
        let fields = self.kernel.parse_string(sclk)?;
        let ticks = self.kernel.ticks_from_fields(&fields)?;
        self.kernel.ticks_to_et(ticks, self.lsk)
    }

    fn et_to_sclk(&self, et: f64) -> Result<String, TimeError> {
        if self.kernel.ensure_partitionless().is_err() {
            let ticks = self.kernel.et_to_discrete_ticks(et, self.lsk)?;
            return self
                .kernel
                .decode_native_sclk(ticks)
                .map_err(|_| TimeError::SclkParse);
        }
        let ticks = self.kernel.et_to_ticks(et, self.lsk)?;
        let fields = self.kernel.fields_from_ticks(ticks)?;
        self.kernel.format_string(&fields)
    }
}

/// Converts between a spacecraft clock string and `ET` (TDB seconds past
/// J2000).
pub trait SclkMap {
    /// Converts an SCLK string to `ET`.
    fn sclk_to_et(&self, sclk: &str) -> Result<f64, TimeError>;

    /// Converts `ET` to an SCLK string.
    fn et_to_sclk(&self, et: f64) -> Result<String, TimeError>;
}

/// Extracts every `\begindata` ... `\begintext` block.
fn extract_data(text: &str) -> Result<String, TimeError> {
    const BEGIN: &str = "\\begindata";
    const END: &str = "\\begintext";

    let mut data = String::new();
    let mut rest = text;
    let mut found = false;

    while let Some(start) = rest.find(BEGIN) {
        found = true;
        let after = &rest[start + BEGIN.len()..];
        let end = after.find(END).unwrap_or(after.len());
        data.push_str(&after[..end]);
        data.push('\n');
        if end == after.len() {
            break;
        }
        rest = &after[end + END.len()..];
    }

    if found {
        Ok(data)
    } else {
        Err(TimeError::SclkParse)
    }
}

/// Splits on whitespace and the assignment punctuation.
fn tokenize(input: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current = String::new();

    for ch in input.chars() {
        match ch {
            '=' | '(' | ')' | ',' => {
                if !current.is_empty() {
                    tokens.push(core::mem::take(&mut current));
                }
                tokens.push(ch.to_string());
            }
            c if c.is_whitespace() => {
                if !current.is_empty() {
                    tokens.push(core::mem::take(&mut current));
                }
            }
            c => current.push(c),
        }
    }

    if !current.is_empty() {
        tokens.push(current);
    }

    tokens
}

/// Finds the index of a kernel variable token, trying both id signs.
fn find_var(tokens: &[String], base: &str, spacecraft_id: i32) -> Option<usize> {
    let magnitude = i64::from(spacecraft_id).abs();
    let candidates = [
        format!("{base}_{spacecraft_id}"),
        format!("{base}_{magnitude}"),
        format!("{base}_-{magnitude}"),
    ];
    tokens
        .iter()
        .rposition(|token| candidates.iter().any(|candidate| candidate == token))
}

/// Reads one scalar or the parenthesized list following `name =`.
fn parse_list(tokens: &[String], name_index: usize) -> Result<Vec<&str>, TimeError> {
    let mut index = name_index + 1;
    expect(tokens, index, "=")?;
    index += 1;
    let value = tokens.get(index).ok_or(TimeError::SclkParse)?;
    if value != "(" {
        if matches!(value.as_str(), "=" | ")" | ",")
            || tokens.get(index + 1).is_some_and(|token| token == "=")
        {
            return Err(TimeError::SclkParse);
        }
        return Ok(alloc::vec![value.as_str()]);
    }
    index += 1;

    let mut values = Vec::new();
    while index < tokens.len() && tokens[index] != ")" {
        if matches!(tokens[index].as_str(), "(" | "=") {
            return Err(TimeError::SclkParse);
        }
        if tokens[index] != "," {
            values.push(tokens[index].as_str());
        }
        index += 1;
    }
    if index >= tokens.len() {
        return Err(TimeError::SclkParse);
    }
    Ok(values)
}

fn expect(tokens: &[String], index: usize, expected: &str) -> Result<(), TimeError> {
    if tokens.get(index).map(String::as_str) == Some(expected) {
        Ok(())
    } else {
        Err(TimeError::SclkParse)
    }
}

fn first(values: Vec<&str>) -> Result<&str, TimeError> {
    values.into_iter().next().ok_or(TimeError::SclkParse)
}

fn parse_i64(token: &str) -> Result<i64, TimeError> {
    if let Ok(value) = token.parse::<i64>() {
        return Ok(value);
    }
    // Kernel-pool integer retrieval accepts numeric exponents and rounds
    // to the nearest integer (ties away from zero), as NAIF GIPOOL does.
    let value = math::round(parse_f64(token)?);
    if value < i64::MIN as f64 || value >= -(i64::MIN as f64) {
        return Err(TimeError::SclkParse);
    }
    Ok(value as i64)
}

fn parse_f64(token: &str) -> Result<f64, TimeError> {
    crate::naif::parse_f64(token).ok_or(TimeError::SclkParse)
}

fn parse_i64_list(tokens: &[String], name_index: usize) -> Result<Vec<i64>, TimeError> {
    parse_list(tokens, name_index)?
        .into_iter()
        .map(parse_i64)
        .collect()
}

fn parse_f64_list(tokens: &[String], name_index: usize) -> Result<Vec<f64>, TimeError> {
    parse_list(tokens, name_index)?
        .into_iter()
        .map(parse_f64)
        .collect()
}

fn delimiter_from_code(code: i64) -> char {
    match code {
        2 => ':',
        3 => '-',
        4 => ',',
        5 => ' ',
        _ => '.',
    }
}