surrealdb-sql 1.1.0

Full type definitions for the SurrealQL query language
Documentation
use crate::fmt::Fmt;
use crate::idiom::Idiom;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::Deref;

#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
pub struct Orders(pub Vec<Order>);

impl Deref for Orders {
	type Target = Vec<Order>;
	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl IntoIterator for Orders {
	type Item = Order;
	type IntoIter = std::vec::IntoIter<Self::Item>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}

impl fmt::Display for Orders {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "ORDER BY {}", Fmt::comma_separated(&self.0))
	}
}

#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
pub struct Order {
	pub order: Idiom,
	pub random: bool,
	pub collate: bool,
	pub numeric: bool,
	pub direction: bool,
}

impl Deref for Order {
	type Target = Idiom;
	fn deref(&self) -> &Self::Target {
		&self.order
	}
}

impl fmt::Display for Order {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}", self.order)?;
		if self.random {
			write!(f, "RAND()")?;
		}
		if self.collate {
			write!(f, " COLLATE")?;
		}
		if self.numeric {
			write!(f, " NUMERIC")?;
		}
		match self.direction {
			false => write!(f, " DESC")?,
			true => (),
		};
		Ok(())
	}
}