use crate::{Error, FixedField};
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct BoundaryVia {
pub path: BoundaryPath,
pub return_to_origin: bool,
}
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum BoundaryPath {
Circle,
GreatCircle,
RhumbLine,
CounterClockwiseArc,
ClockwiseArc,
}
impl FixedField<'_> for BoundaryVia {
const LENGTH: usize = 2;
fn from_bytes(bytes: &'_ [u8]) -> Result<Self, Error> {
let path = match bytes[0] {
b'C' => BoundaryPath::Circle,
b'G' => BoundaryPath::GreatCircle,
b'H' => BoundaryPath::RhumbLine,
b'L' => BoundaryPath::CounterClockwiseArc,
b'R' => BoundaryPath::ClockwiseArc,
byte => {
return Err(Error::InvalidCharacter {
field: "Boundary Via",
byte,
expected: "C, G, H, L or R",
})
}
};
let return_to_origin = bytes[1] == b'E';
Ok(BoundaryVia {
path,
return_to_origin,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_clockwise_arc() {
let via = BoundaryVia::from_bytes(b"R ").expect("should parse");
assert_eq!(via.path, BoundaryPath::ClockwiseArc);
assert!(!via.return_to_origin);
}
#[test]
fn parses_with_end_indicator() {
let via = BoundaryVia::from_bytes(b"GE").expect("should parse");
assert_eq!(via.path, BoundaryPath::GreatCircle);
assert!(via.return_to_origin);
}
}