Skip to main content

parse_angle

Function parse_angle 

Source
pub fn parse_angle(s: &str) -> Result<ParsedAngle, AstroError>
Expand description

Parses an angle string, trying HMS format first, then DMS.

This is a lightweight parser for angle formats. For more flexible parsing including verbose formats (“12 hours 30 minutes”), see super::parse::parse_hms and super::parse::parse_dms.

§Supported Formats

HMS (hours-minutes-seconds):

  • 12h30m15s or 12h30m15.5s
  • 12ʰ30ᵐ15ˢ (Unicode superscripts)
  • 12:30:15 (colon-separated)
  • 12h (hours only)
  • -12h30m15s (negative)

DMS (degrees-minutes-seconds):

  • 45°30'15" or 45°30'15.5"
  • 45d30m15s
  • 45:30:15 (colon-separated, tried if HMS fails)
  • 45° (degrees only)
  • -45°30'15" (negative)

§Ambiguity

Colon-separated values like 12:30:15 are tried as HMS first. If you need to parse this as DMS explicitly, use parse_dms.

§Errors

Returns AstroError if:

  • The string is empty or contains no valid components
  • Minutes or seconds are outside [0, 60)
  • Fractional hours/degrees are mixed with minutes/seconds (e.g., “12.5h30m”)
  • The string cannot be parsed as either HMS or DMS

§Example

use celestial_core::angle::parse_angle;

// Right ascension
let ra = parse_angle("05h14m32.27s").unwrap();
assert!((ra.angle.hours() - 5.242297).abs() < 1e-5);

// Declination
let dec = parse_angle("-08°12'05.9\"").unwrap();
assert!((dec.angle.degrees() - (-8.201639)).abs() < 1e-5);