sidereon-core 0.8.0

The complete Sidereon engine: numerical astrodynamics propagation core plus the GNSS domain layer (SP3, broadcast ephemeris, multi-GNSS positioning, RTK/PPP, ionosphere/troposphere, DOP) behind a default-on gnss feature
Documentation
//! Small fixed-width text parsing helpers.
//!
//! SP3, RINEX, IONEX, and CRINEX are fixed-column text products. Keeping the
//! common field slicing and Fortran-float handling here keeps product parsers
//! focused on record semantics instead of repeating byte-window plumbing.

use crate::astro::time::model::TimeScale;

/// Map a GNSS product time-system label onto the core [`TimeScale`].
///
/// Returns `None` for an empty or unrecognized label so each product parser can
/// apply its own policy for the unknown case (SP3 rejects, RINEX defaults to
/// GPST). Only the labels common to SP3 and RINEX headers are mapped here;
/// product-specific spellings (e.g. the SP3 `BDS` alias) stay with the caller.
pub(crate) fn time_scale_label(label: &str) -> Option<TimeScale> {
    match label.trim() {
        "GPS" | "QZS" => Some(TimeScale::Gpst),
        "GLO" => Some(TimeScale::Utc),
        "GAL" => Some(TimeScale::Gst),
        "BDT" => Some(TimeScale::Bdt),
        "UTC" => Some(TimeScale::Utc),
        "TAI" => Some(TimeScale::Tai),
        _ => None,
    }
}

/// A fixed-column field, trimmed. Returns `None` if the requested range is out
/// of bounds or the trimmed field is blank.
pub(crate) fn field(line: &str, start: usize, end: usize) -> Option<&str> {
    let s = line.get(start..end.min(line.len()))?.trim();
    if s.is_empty() {
        None
    } else {
        Some(s)
    }
}

/// A fixed-column field, untrimmed. Missing ranges are returned as an empty
/// string so callers that historically treated short lines as blank can preserve
/// that behavior.
pub(crate) fn raw_field(line: &str, start: usize, end: usize) -> &str {
    let s = floor_char_boundary(line, start);
    let e = floor_char_boundary(line, end);
    if e <= s {
        return "";
    }
    &line[s..e]
}

/// The remainder of a fixed-column line, untrimmed. Missing ranges are returned
/// as an empty string.
pub(crate) fn raw_field_from(line: &str, start: usize) -> &str {
    let s = floor_char_boundary(line, start);
    &line[s..]
}

/// Parse a RINEX-style numeric field, accepting Fortran `D`/`d` exponent
/// markers. Returns `None` for missing, blank, or malformed fields.
pub(crate) fn fortran_f64(line: &str, start: usize, end: usize) -> Option<f64> {
    let s = field(line, start, end)?;
    crate::validate::strict_f64(s, "numeric field").ok()
}

/// Largest char boundary `<= index` and `<= line.len()`.
fn floor_char_boundary(line: &str, index: usize) -> usize {
    let mut i = index.min(line.len());
    while i > 0 && !line.is_char_boundary(i) {
        i -= 1;
    }
    i
}

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

    #[test]
    fn fortran_f64_rejects_nonfinite_fields() {
        for value in ["NaN", "inf", "-inf"] {
            let line = format!("{value:>19}");
            assert_eq!(fortran_f64(&line, 0, line.len()), None);
        }
    }

    #[test]
    fn fortran_f64_keeps_valid_fortran_exponents() {
        assert_eq!(fortran_f64(" 1.250000000000D+03", 0, 20), Some(1250.0));
        assert_eq!(fortran_f64("-2.500000000000d-01", 0, 20), Some(-0.25));
    }
}