surrealpatch/src/sql/field.rs

113 lines
2.5 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::comment::shouldbespace;
use crate::sql::common::commas;
use crate::sql::expression::{expression, Expression};
use crate::sql::idiom::{idiom, Idiom};
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 nom::IResult;
use serde::{Deserialize, Serialize};
use std::fmt;
2021-03-29 15:43:37 +00:00
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
2020-06-29 15:36:01 +00:00
pub struct Fields(Vec<Field>);
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)))
}
2021-03-29 15:43:37 +00:00
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Field {
All,
Alone(Expression),
Alias(Expression, Idiom),
}
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(ref e) => write!(f, "{}", e),
Field::Alias(ref e, ref 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)
}
fn all(i: &str) -> IResult<&str, Field> {
let (i, _) = tag_no_case("*")(i)?;
Ok((i, Field::All))
}
fn alone(i: &str) -> IResult<&str, Field> {
2020-06-29 15:36:01 +00:00
let (i, f) = expression(i)?;
2021-03-29 15:43:37 +00:00
Ok((i, Field::Alone(f)))
2020-06-29 15:36:01 +00:00
}
2021-03-29 15:43:37 +00:00
fn alias(i: &str) -> IResult<&str, Field> {
let (i, f) = expression(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));
}
}