spicekit 0.2.1

Pure-Rust reader for NASA/NAIF SPICE kernel formats (DAF, SPK, PCK, text kernels). Independent of the CSpice toolkit.
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
//! Minimal text-kernel parser for NAIF body name/code bindings.
//!
//! Text kernels (`.tk`, `.tf`, `.tpc`, `.ti`) alternate `\begintext` /
//! `\begindata` blocks; only data blocks carry assignments. This parser
//! is deliberately scoped: inside data blocks it extracts
//!
//! ```text
//!   NAIF_BODY_NAME += ( 'JWST', 'JAMES WEBB SPACE TELESCOPE' )
//!   NAIF_BODY_CODE += ( -170, -170 )
//! ```
//!
//! and ignores every other assignment. Both `=` and `+=` are treated
//! as providing additional bindings — CSpice semantics are
//! last-loaded-wins, so the caller layers these on top of earlier
//! entries. Scalars and parenthesised lists are both accepted. String
//! values may be single- or double-quoted; embedded `''` / `""` is a
//! literal quote.
//!
//! By only recognising two specific keys we stay robust to the full
//! diversity of text-kernel content (LSK deltas with Fortran `D-`
//! notation, SCLK partitions, date literals like `@1972-JAN-1`, frame
//! definitions, etc.). Anything we don't need is skipped character-by-
//! character until we resync on a `\begintext` marker or a new
//! `NAIF_BODY_*` assignment.

use std::fs;
use std::io;
use std::path::Path;

#[derive(Debug, thiserror::Error)]
pub enum TextKernelError {
    #[error("io error reading text kernel {path}: {source}")]
    Io {
        path: String,
        #[source]
        source: io::Error,
    },
    #[error("parse error at line {line}: {message}")]
    Parse { line: usize, message: String },
    #[error("NAIF_BODY_NAME has {names} entries but NAIF_BODY_CODE has {codes} — they must match")]
    Mismatched { names: usize, codes: usize },
}

/// A single (name, code) binding parsed from a text kernel, in file order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BodyBinding {
    pub name: String,
    pub code: i32,
}

/// Parse a text kernel file and return the ordered list of
/// `NAIF_BODY_NAME` ↔ `NAIF_BODY_CODE` bindings.
///
/// File-order is preserved so downstream callers can apply
/// last-loaded-wins semantics to match CSpice.
pub fn parse_body_bindings(path: &Path) -> Result<Vec<BodyBinding>, TextKernelError> {
    let text = fs::read_to_string(path).map_err(|e| TextKernelError::Io {
        path: path.display().to_string(),
        source: e,
    })?;
    parse_body_bindings_from_str(&text)
}

/// Parse an in-memory text-kernel string.
pub fn parse_body_bindings_from_str(text: &str) -> Result<Vec<BodyBinding>, TextKernelError> {
    let mut names: Vec<String> = Vec::new();
    let mut codes: Vec<i32> = Vec::new();

    let mut in_data = false;
    let mut line_no = 1usize;
    // We sweep the text once, tracking line numbers. Within data
    // blocks we look for NAIF_BODY_NAME / NAIF_BODY_CODE and harvest
    // their RHS values; everything else inside a data block is
    // skipped.
    let bytes = text.as_bytes();
    let mut i = 0usize;
    while i < bytes.len() {
        // Check for line-anchored directives.
        if bytes[i] == b'\\' {
            if let Some(word_end) = directive_end(bytes, i) {
                let word = &text[i..word_end];
                if word.eq_ignore_ascii_case("\\begindata") {
                    in_data = true;
                    i = word_end;
                    continue;
                } else if word.eq_ignore_ascii_case("\\begintext") {
                    in_data = false;
                    i = word_end;
                    continue;
                }
            }
        }

        if !in_data {
            if bytes[i] == b'\n' {
                line_no += 1;
            }
            i += 1;
            continue;
        }

        // Inside a data block. Look for a NAIF_BODY_NAME or
        // NAIF_BODY_CODE assignment. A real assignment starts with
        // either of those identifiers at a token boundary.
        if is_ident_start(bytes[i]) && is_word_boundary_before(bytes, i) {
            let (end, ident) = read_identifier(text, i);
            if ident == "NAIF_BODY_NAME" || ident == "NAIF_BODY_CODE" {
                let (after_op, _op_line_delta) = skip_equals(bytes, end)?;
                line_no += count_newlines(&bytes[end..after_op]);
                let (after_values, values, delta) =
                    read_values(text, after_op).map_err(|msg| TextKernelError::Parse {
                        line: line_no,
                        message: msg,
                    })?;
                line_no += delta;
                if ident == "NAIF_BODY_NAME" {
                    for v in values {
                        match v {
                            Value::String(s) => names.push(s),
                            _ => {
                                return Err(TextKernelError::Parse {
                                    line: line_no,
                                    message: "NAIF_BODY_NAME expects strings".into(),
                                })
                            }
                        }
                    }
                } else {
                    for v in values {
                        match v {
                            Value::Int(n) => codes.push(n),
                            _ => {
                                return Err(TextKernelError::Parse {
                                    line: line_no,
                                    message: "NAIF_BODY_CODE expects integers".into(),
                                })
                            }
                        }
                    }
                }
                i = after_values;
                continue;
            } else {
                // Not one we care about — advance past the identifier
                // and let the loop continue scanning.
                i = end;
                continue;
            }
        }

        if bytes[i] == b'\n' {
            line_no += 1;
        }
        i += 1;
    }

    if names.len() != codes.len() {
        return Err(TextKernelError::Mismatched {
            names: names.len(),
            codes: codes.len(),
        });
    }

    Ok(names
        .into_iter()
        .zip(codes)
        .map(|(name, code)| BodyBinding { name, code })
        .collect())
}

#[derive(Debug)]
enum Value {
    String(String),
    Int(i32),
    Other,
}

fn is_ident_start(b: u8) -> bool {
    b.is_ascii_alphabetic() || b == b'_'
}

fn is_ident_continue(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'/'
}

fn is_word_boundary_before(bytes: &[u8], i: usize) -> bool {
    if i == 0 {
        return true;
    }
    let prev = bytes[i - 1];
    !(prev.is_ascii_alphanumeric() || prev == b'_' || prev == b'/')
}

fn read_identifier(text: &str, start: usize) -> (usize, &str) {
    let bytes = text.as_bytes();
    let mut end = start;
    while end < bytes.len() && is_ident_continue(bytes[end]) {
        end += 1;
    }
    (end, &text[start..end])
}

fn directive_end(bytes: &[u8], start: usize) -> Option<usize> {
    // Read \ + word of alphabetic chars.
    let mut end = start + 1;
    while end < bytes.len() && bytes[end].is_ascii_alphabetic() {
        end += 1;
    }
    if end == start + 1 {
        None
    } else {
        Some(end)
    }
}

fn count_newlines(bytes: &[u8]) -> usize {
    bytes.iter().filter(|&&b| b == b'\n').count()
}

/// Skip whitespace then require `=` or `+=`; return the index after
/// the operator and the number of newlines consumed.
fn skip_equals(bytes: &[u8], mut i: usize) -> Result<(usize, usize), TextKernelError> {
    let mut delta = 0usize;
    while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    if i < bytes.len() && bytes[i] == b'+' {
        i += 1;
    }
    if i < bytes.len() && bytes[i] == b'=' {
        i += 1;
        Ok((i, delta))
    } else {
        // Allow newline between key and operator (rare but legal).
        while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t' || bytes[i] == b'\n') {
            if bytes[i] == b'\n' {
                delta += 1;
            }
            i += 1;
        }
        if i < bytes.len() && bytes[i] == b'+' {
            i += 1;
        }
        if i < bytes.len() && bytes[i] == b'=' {
            i += 1;
            Ok((i, delta))
        } else {
            Err(TextKernelError::Parse {
                line: 0,
                message: "expected '=' or '+=' after NAIF_BODY_NAME/CODE".into(),
            })
        }
    }
}

/// Read a scalar or parenthesised list of values starting at `i`.
/// Returns (next_index, values, newlines_consumed).
fn read_values(text: &str, mut i: usize) -> Result<(usize, Vec<Value>, usize), String> {
    let bytes = text.as_bytes();
    let mut values = Vec::new();
    let mut delta = 0usize;

    // Skip leading whitespace (including newlines before the value).
    while i < bytes.len() && (bytes[i] as char).is_whitespace() {
        if bytes[i] == b'\n' {
            delta += 1;
        }
        i += 1;
    }

    let parenthesised = i < bytes.len() && bytes[i] == b'(';
    if parenthesised {
        i += 1;
    }

    loop {
        // Skip whitespace and commas between elements.
        while i < bytes.len() {
            let c = bytes[i] as char;
            if c == '\n' {
                delta += 1;
                i += 1;
            } else if c.is_whitespace() || c == ',' {
                i += 1;
            } else {
                break;
            }
        }
        if i >= bytes.len() {
            break;
        }
        match bytes[i] {
            b')' if parenthesised => {
                i += 1;
                break;
            }
            b'\'' | b'"' => {
                let quote = bytes[i];
                i += 1;
                let mut s = String::new();
                while i < bytes.len() {
                    if bytes[i] == quote {
                        // Doubled quote = escaped quote.
                        if i + 1 < bytes.len() && bytes[i + 1] == quote {
                            s.push(quote as char);
                            i += 2;
                            continue;
                        }
                        i += 1;
                        break;
                    }
                    if bytes[i] == b'\n' {
                        delta += 1;
                    }
                    s.push(bytes[i] as char);
                    i += 1;
                }
                values.push(Value::String(s));
            }
            b if b == b'-' || b == b'+' || b.is_ascii_digit() || b == b'.' => {
                let start = i;
                i += 1;
                while i < bytes.len() {
                    let c = bytes[i];
                    if c.is_ascii_digit()
                        || c == b'.'
                        || c == b'e'
                        || c == b'E'
                        || c == b'd'
                        || c == b'D'
                        || c == b'+'
                        || c == b'-'
                    {
                        i += 1;
                    } else {
                        break;
                    }
                }
                let tok = &text[start..i];
                if let Ok(n) = tok.parse::<i32>() {
                    values.push(Value::Int(n));
                } else {
                    // Non-integer numeric (e.g. 32.184, 1.657D-3). Not
                    // a body code; record as Other so caller errors
                    // out cleanly if mis-typed.
                    values.push(Value::Other);
                }
            }
            b'@' => {
                // Date literal like @1972-JAN-1. Skip it.
                i += 1;
                while i < bytes.len() {
                    let c = bytes[i];
                    if c.is_ascii_alphanumeric() || c == b'-' || c == b':' || c == b'.' || c == b'/'
                    {
                        i += 1;
                    } else {
                        break;
                    }
                }
                values.push(Value::Other);
            }
            b if b.is_ascii_alphabetic() || b == b'_' => {
                // Bare identifier / symbol (e.g. logical TRUE). Skip.
                while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
                    i += 1;
                }
                values.push(Value::Other);
            }
            _ => break,
        }
        if !parenthesised {
            break;
        }
    }

    Ok((i, values, delta))
}

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

    #[test]
    fn parses_simple_jwst_block() {
        let kernel = "KPL/FK\n\n\\begindata\n  NAIF_BODY_NAME += ( 'JWST', 'JAMES WEBB SPACE TELESCOPE' )\n  NAIF_BODY_CODE += ( -170, -170 )\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(bindings.len(), 2);
        assert_eq!(
            bindings[0],
            BodyBinding {
                name: "JWST".into(),
                code: -170
            }
        );
        assert_eq!(
            bindings[1],
            BodyBinding {
                name: "JAMES WEBB SPACE TELESCOPE".into(),
                code: -170,
            }
        );
    }

    #[test]
    fn ignores_comment_sections() {
        let kernel = "Free text preamble.\n   NAIF_BODY_NAME += ( 'IGNORED' )\n   NAIF_BODY_CODE += ( 999 )\n\n\\begindata\n  NAIF_BODY_NAME += ( 'CUSTOM' )\n  NAIF_BODY_CODE += ( -999 )\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(
            bindings,
            vec![BodyBinding {
                name: "CUSTOM".into(),
                code: -999
            }]
        );
    }

    #[test]
    fn supports_scalar_and_equals() {
        let kernel =
            "\\begindata\n  NAIF_BODY_NAME = 'LONE_BODY'\n  NAIF_BODY_CODE = -42\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(
            bindings,
            vec![BodyBinding {
                name: "LONE_BODY".into(),
                code: -42
            }]
        );
    }

    #[test]
    fn concatenates_multiple_data_blocks() {
        let kernel = "\\begindata\n  NAIF_BODY_NAME += ( 'A' )\n  NAIF_BODY_CODE += ( 1 )\n\\begintext\nfiller\n\\begindata\n  NAIF_BODY_NAME += ( 'B' )\n  NAIF_BODY_CODE += ( 2 )\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(
            bindings,
            vec![
                BodyBinding {
                    name: "A".into(),
                    code: 1
                },
                BodyBinding {
                    name: "B".into(),
                    code: 2
                },
            ]
        );
    }

    #[test]
    fn rejects_mismatched_arrays() {
        let kernel = "\\begindata\n  NAIF_BODY_NAME += ( 'A', 'B' )\n  NAIF_BODY_CODE += ( 1 )\n\\begintext\n";
        let err = parse_body_bindings_from_str(kernel).unwrap_err();
        assert!(matches!(
            err,
            TextKernelError::Mismatched { names: 2, codes: 1 }
        ));
    }

    #[test]
    fn handles_double_quotes_and_line_continuations() {
        let kernel = "\\begindata\n   NAIF_BODY_NAME += (\n     \"ALPHA\",\n     \"BETA\"\n   )\n   NAIF_BODY_CODE += (\n     100,\n     200\n   )\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(bindings.len(), 2);
        assert_eq!(bindings[0].name, "ALPHA");
        assert_eq!(bindings[1].code, 200);
    }

    #[test]
    fn ignores_unrelated_assignments() {
        let kernel = "\\begindata\n  OBJECT_EARTH_FRAME = 'ITRF93'\n  NAIF_BODY_NAME += ( 'ONE' )\n  NAIF_BODY_CODE += ( 1 )\n  DELTET/DELTA_T_A = 32.184\n\\begintext\n";
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(
            bindings,
            vec![BodyBinding {
                name: "ONE".into(),
                code: 1
            }]
        );
    }

    #[test]
    fn no_data_block_returns_empty() {
        let bindings = parse_body_bindings_from_str("just a comment file").unwrap();
        assert!(bindings.is_empty());
    }

    #[test]
    fn tolerates_leapseconds_kernel_content() {
        // Distilled from latest_leapseconds.tls — the actual file
        // trips a naive parser because of `1.657D-3` (Fortran double
        // notation), `@1972-JAN-1` date literals, and slash-containing
        // keys like `DELTET/K`.
        let kernel = r#"
\begindata

DELTET/DELTA_T_A       =   32.184
DELTET/K               =    1.657D-3
DELTET/EB              =    1.671D-2
DELTET/M               = (  6.239996D0   1.99096871D-7 )

DELTET/DELTA_AT        = ( 10,   @1972-JAN-1
                           11,   @1972-JUL-1
                           12,   @1973-JAN-1 )

NAIF_BODY_NAME += ( 'EXTRA_BODY' )
NAIF_BODY_CODE += ( -12345 )

\begintext
"#;
        let bindings = parse_body_bindings_from_str(kernel).unwrap();
        assert_eq!(
            bindings,
            vec![BodyBinding {
                name: "EXTRA_BODY".into(),
                code: -12345
            }]
        );
    }
}