surrealpatch/lib/src/sql/field.rs

149 lines
3.2 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::comment::shouldbespace;
use crate::sql::common::commas;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
2020-06-29 15:36:01 +00:00
use crate::sql::idiom::{idiom, Idiom};
use crate::sql::value::{value, Value};
2021-03-29 15:43:37 +00:00
use nom::branch::alt;
2020-06-29 15:36:01 +00:00
use nom::bytes::complete::tag_no_case;
2021-03-29 15:43:37 +00:00
use nom::multi::separated_list1;
2020-06-29 15:36:01 +00:00
use serde::{Deserialize, Serialize};
use std::fmt;
2022-03-25 18:43:22 +00:00
use std::ops::Deref;
2020-06-29 15:36:01 +00:00
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Fields(pub Vec<Field>);
impl Fields {
pub fn all(&self) -> bool {
self.0.iter().any(|v| matches!(v, Field::All))
}
pub fn other(&self) -> impl Iterator<Item = &Field> {
self.0.iter().filter(|v| !matches!(v, Field::All))
}
pub fn single(&self) -> Option<Idiom> {
match self.0.len() {
1 => match self.0.first() {
Some(Field::All) => None,
Some(Field::Alone(e)) => Some(e.to_idiom()),
Some(Field::Alias(_, i)) => Some(i.to_owned()),
_ => None,
},
_ => None,
}
}
}
2020-06-29 15:36:01 +00:00
2022-03-25 18:43:22 +00:00
impl Deref for Fields {
type Target = Vec<Field>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl IntoIterator for Fields {
type Item = Field;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
2020-06-29 15:36:01 +00:00
impl fmt::Display for Fields {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", "))
}
}
pub fn fields(i: &str) -> IResult<&str, Fields> {
2021-03-29 15:43:37 +00:00
let (i, v) = separated_list1(commas, field)(i)?;
2020-06-29 15:36:01 +00:00
Ok((i, Fields(v)))
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
2021-03-29 15:43:37 +00:00
pub enum Field {
All,
Alone(Value),
Alias(Value, Idiom),
2021-03-29 15:43:37 +00:00
}
impl Default for Field {
fn default() -> Field {
Field::All
}
2020-06-29 15:36:01 +00:00
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2021-03-29 15:43:37 +00:00
match self {
Field::All => write!(f, "*"),
Field::Alone(e) => write!(f, "{}", e),
Field::Alias(e, a) => write!(f, "{} AS {}", e, a),
2020-06-29 15:36:01 +00:00
}
}
}
pub fn field(i: &str) -> IResult<&str, Field> {
2021-03-29 15:43:37 +00:00
alt((all, alias, alone))(i)
}
pub fn all(i: &str) -> IResult<&str, Field> {
2021-03-29 15:43:37 +00:00
let (i, _) = tag_no_case("*")(i)?;
Ok((i, Field::All))
}
pub fn alone(i: &str) -> IResult<&str, Field> {
let (i, f) = value(i)?;
2021-03-29 15:43:37 +00:00
Ok((i, Field::Alone(f)))
2020-06-29 15:36:01 +00:00
}
pub fn alias(i: &str) -> IResult<&str, Field> {
let (i, f) = value(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = shouldbespace(i)?;
let (i, _) = tag_no_case("AS")(i)?;
let (i, _) = shouldbespace(i)?;
2021-03-29 15:43:37 +00:00
let (i, a) = idiom(i)?;
Ok((i, Field::Alias(f, a)))
2020-06-29 15:36:01 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
2021-03-29 15:43:37 +00:00
#[test]
fn field_all() {
let sql = "*";
let res = fields(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("*", format!("{}", out));
}
2020-06-29 15:36:01 +00:00
#[test]
fn field_single() {
let sql = "field";
let res = fields(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("field", format!("{}", out));
}
#[test]
fn field_multiple() {
let sql = "field, other.field";
let res = fields(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("field, other.field", format!("{}", out));
}
#[test]
fn field_aliases() {
let sql = "field AS one, other.field AS two";
let res = fields(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("field AS one, other.field AS two", format!("{}", out));
}
}