use crate::core::geo::circle::Circle;
use crate::core::geo::component_tree::{ComponentTree, create as create_component_tree};
use crate::core::geo::component2d::Component2DEnum2;
use crate::core::geo::component2d::{Component2D, WithinRelation};
use crate::core::geo::geometry::Geometry;
use crate::core::geo::line::Line;
use crate::core::geo::point::Point;
use crate::core::geo::polygon::Polygon;
use crate::core::geo::rectangle::Rectangle;
use crate::core::index::point_values::Relation;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::impl_from_for_enum;
pub trait LatLonGeometry: Geometry {}
pub fn create<T>(xy_geometries: &[T]) -> Result<LatLonGeometryType<T::Component2D>>
where
T: LatLonGeometry,
{
if xy_geometries.is_empty() {
return Err(LuceneError::illegal_argument(
"geometries must not be empty",
));
}
if let [geometry] = xy_geometries {
return Ok(LatLonGeometryType::A(geometry.to_component2d()?));
}
let mut components = Vec::with_capacity(xy_geometries.len());
for geometry in xy_geometries {
components.push(geometry.to_component2d()?);
}
Ok(LatLonGeometryType::B(create_component_tree(components)?))
}
pub type LatLonGeometryType<T> = Component2DEnum2<T, ComponentTree<T>>;
#[macro_export]
macro_rules! either_lat_lon_geometry_named {
(
$vis:vis $name:ident,
$component_name:ident {
$( $Variant:ident : $T:ident ),+ $(,)?
}
) => {
$crate::either_component2d_named!(
$vis $component_name {
$( $Variant : $T ),+
}
);
#[derive(PartialEq, Eq, Hash)]
$vis enum $name<$( $T ),+> {
$( $Variant($T), )+
}
impl<$( $T ),+> std::fmt::Display for $name<$( $T ),+>
where
$( $T: std::fmt::Display ),+
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
$( Self::$Variant(inner) => std::fmt::Display::fmt(inner, f), )+
}
}
}
impl<$( $T ),+> Geometry for $name<$( $T ),+>
where
$( $T: Geometry ),+
{
type Component2D = $component_name<$( <$T as Geometry>::Component2D ),+>;
fn to_component2d(&self) -> Result<Self::Component2D> {
match self {
$( Self::$Variant(inner) => Ok(Self::Component2D::$Variant(inner.to_component2d()?)), )+
}
}
}
impl<$( $T ),+> LatLonGeometry for $name<$( $T ),+>
where
$( $T: LatLonGeometry ),+
{}
};
}
either_lat_lon_geometry_named!(
pub LatLonGeometryEnum5,
LatLonGeometryComponent2DEnum5 {
Circle: A,
Line: B,
Point: C,
Polygon: D,
Rectangle: E,
}
);
pub type LatLonGeometryEnum = LatLonGeometryEnum5<Circle, Line, Point, Polygon, Rectangle>;
pub type LatLonGeometryEnumComponent2D = <LatLonGeometryEnum as Geometry>::Component2D;
impl_from_for_enum!(
LatLonGeometryEnum,
Circle => Circle,
Line => Line,
Point => Point,
Polygon => Polygon,
Rectangle => Rectangle,
);
impl Clone for LatLonGeometryEnum {
fn clone(&self) -> Self {
match self {
LatLonGeometryEnum5::Circle(v) => LatLonGeometryEnum5::Circle(v.clone()),
LatLonGeometryEnum5::Line(v) => LatLonGeometryEnum5::Line(v.clone()),
LatLonGeometryEnum5::Point(v) => LatLonGeometryEnum5::Point(*v),
LatLonGeometryEnum5::Polygon(v) => LatLonGeometryEnum5::Polygon(v.clone()),
LatLonGeometryEnum5::Rectangle(v) => LatLonGeometryEnum5::Rectangle(v.clone()),
}
}
}