use std::fmt;
use crate::Coord;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Direction {
Cw,
Ccw,
}
impl Default for Direction {
fn default() -> Self {
Self::Cw
}
}
impl Direction {
pub fn parse(data: &str) -> Result<Self, String> {
match data {
"+" => Ok(Self::Cw),
"-" => Ok(Self::Ccw),
_ => Err(format!("Invalid direction: {}", data)),
}
}
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct ArcSegment {
pub centerpoint: Coord,
pub radius: f32,
pub angle_start: f32,
pub angle_end: f32,
pub direction: Direction,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Arc {
pub centerpoint: Coord,
pub start: Coord,
pub end: Coord,
pub direction: Direction,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum PolygonSegment {
Point(Coord),
Arc(Arc),
ArcSegment(ArcSegment),
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum Geometry {
Polygon {
segments: Vec<PolygonSegment>,
},
Circle {
centerpoint: Coord,
radius: f32,
},
}
impl fmt::Display for Geometry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Polygon { segments } => write!(f, "Polygon[{}]", segments.len()),
Self::Circle { radius, .. } => write!(f, "Circle[r={radius}NM]"),
}
}
}