use crate::{
ColumnEq, ColumnKind, ColumnName, ColumnShape, CompositeFilterMarker, CompositeMarker,
CursorValue, DefaultMarker, DefaultVecMarker, FieldName, PayloadFilter, QuantifiedMarker, shape,
};
use super::NormCoord;
impl<T> crate::DeclaredLeaves for Point2<T> {}
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Point2<T> {
pub(super) x: T,
pub(super) y: T,
}
impl<T> Point2<T> {
#[inline]
#[must_use]
pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
#[inline]
pub const fn x(&self) -> T
where
T: Copy,
{
self.x
}
#[inline]
pub const fn y(&self) -> T
where
T: Copy,
{
self.y
}
#[inline]
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> Point2<U> {
Point2 {
x: f(self.x),
y: f(self.y),
}
}
}
impl Point2<NormCoord> {
pub fn try_unit(x: f32, y: f32) -> Result<Self, super::OutsideTheUnitInterval> {
let coord = |value| NormCoord::new(value).ok_or(super::OutsideTheUnitInterval);
Ok(Self::new(coord(x)?, coord(y)?))
}
}
pub(super) const X: &FieldName = FieldName::new("x");
pub(super) const Y: &FieldName = FieldName::new("y");
impl<T: ColumnKind> ColumnKind for Point2<T> {
const SEGMENTS: &'static [ColumnShape] = {
assert!(
T::SEGMENTS.is_empty(),
"a point's component spans ONE column: a composite component would make the point's own width \
depend on it, and a fixed-arity row cannot state that"
);
&[shape::<T>(ColumnName::of(X)), shape::<T>(ColumnName::of(Y))]
};
}
impl<T: ColumnEq> ColumnEq for Point2<T> {
#[inline]
fn column_eq(&self, other: &Self) -> bool {
self.x.column_eq(&other.x) && self.y.column_eq(&other.y)
}
}
impl<Db, T> crate::StoredAs<Db> for Point2<T>
where
T: crate::StoredAs<Db>,
{
const WORDS: &'static [&'static str] = &[
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
];
}
impl<T: crate::CollectionElement> crate::CollectionElement for Point2<T> {}
#[cfg(feature = "sqlx")]
impl<T, Db> crate::Carrier<Db> for Point2<T>
where
Db: sqlx::Database,
T: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
<T as crate::Carrier<Db>>::push_bind(&self.x, qb);
qb.push(", ");
<T as crate::Carrier<Db>>::push_bind(&self.y, qb);
}
}
#[cfg(feature = "sqlx")]
impl<'a, R, T> crate::Assemble<'a, R> for Point2<T>
where
R: sqlx::Row,
T: crate::Assemble<'a, R>,
{
fn assemble(row: &'a R, under: Option<&crate::Path<'_>>) -> sqlx::Result<Self> {
let x: T = crate::assembled(row, X, under)?;
let y: T = crate::assembled(row, Y, under)?;
Ok(Self::new(x, y))
}
}
impl<T: CursorValue> CursorValue for Point2<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
CursorValue::write_cursor(&self.x, out);
CursorValue::write_cursor(&self.y, out);
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let width = bytes.len() / 2;
if width == 0 || width * 2 != bytes.len() {
return None;
}
let x = <T as CursorValue>::read_cursor(&bytes[..width])?;
let y = <T as CursorValue>::read_cursor(&bytes[width..])?;
Some(Self::new(x, y))
}
}
impl<T: DefaultMarker> DefaultMarker for Point2<T> {
type Marker = CompositeMarker<Point2<T>>;
}
impl<T: DefaultMarker> DefaultVecMarker for Point2<T> {
type Marker = QuantifiedMarker<Vec<Point2<T>>>;
}
impl<T> CompositeFilterMarker for Point2<T>
where
T: DefaultMarker,
{
type Filter = Point2Filter<PayloadFilter<T>>;
type ColumnFilter = Point2Filter<PayloadFilter<T>>;
}
pub struct Point2Filter<CF> {
pub(super) x: Option<CF>,
pub(super) y: Option<CF>,
}
impl<CF> Point2Filter<CF> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self { x: None, y: None }
}
crate::graphql::filter::opt_accessor!(x: CF => with_x, set_x, maybe_x, update_x);
crate::graphql::filter::opt_accessor!(y: CF => with_y, set_y, maybe_y, update_y);
}
impl<CF: crate::GraphqlFilter> Point2Filter<CF> {
pub fn is_empty(&self) -> bool {
self.x.as_ref().is_none_or(crate::GraphqlFilter::is_empty)
&& self.y.as_ref().is_none_or(crate::GraphqlFilter::is_empty)
}
}
impl<CF: crate::GraphqlFilter> crate::GraphqlFilter for Point2Filter<CF> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
impl<CF> Default for Point2Filter<CF> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<CF: core::fmt::Debug> core::fmt::Debug for Point2Filter<CF> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Point2Filter")
.field("x", &self.x)
.field("y", &self.y)
.finish()
}
}
impl<CF: Clone> Clone for Point2Filter<CF> {
fn clone(&self) -> Self {
Self {
x: self.x.clone(),
y: self.y.clone(),
}
}
}
impl<CF: PartialEq> PartialEq for Point2Filter<CF> {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}