surrealpatch/lib/src/sql/kind.rs

110 lines
2.7 KiB
Rust
Raw Normal View History

use crate::sql::comment::mightbespace;
use crate::sql::common::commas;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
use crate::sql::fmt::Fmt;
2020-06-29 15:36:01 +00:00
use crate::sql::table::{table, Table};
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::char;
2020-06-29 15:36:01 +00:00
use nom::combinator::map;
use nom::multi::{separated_list0, separated_list1};
2020-06-29 15:36:01 +00:00
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
2020-06-29 15:36:01 +00:00
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
2020-06-29 15:36:01 +00:00
pub enum Kind {
Any,
Array,
Bool,
2023-03-31 22:38:29 +00:00
Bytes,
2020-06-29 15:36:01 +00:00
Datetime,
Decimal,
Duration,
Float,
Int,
2020-06-29 15:36:01 +00:00
Number,
Object,
String,
Record(Vec<Table>),
Geometry(Vec<String>),
2020-06-29 15:36:01 +00:00
}
impl Default for Kind {
fn default() -> Self {
Self::Any
2020-06-29 15:36:01 +00:00
}
}
impl Display for Kind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2020-06-29 15:36:01 +00:00
match self {
Kind::Any => f.write_str("any"),
Kind::Array => f.write_str("array"),
Kind::Bool => f.write_str("bool"),
2023-03-31 22:38:29 +00:00
Kind::Bytes => f.write_str("bytes"),
Kind::Datetime => f.write_str("datetime"),
Kind::Decimal => f.write_str("decimal"),
Kind::Duration => f.write_str("duration"),
Kind::Float => f.write_str("float"),
Kind::Int => f.write_str("int"),
Kind::Number => f.write_str("number"),
Kind::Object => f.write_str("object"),
Kind::String => f.write_str("string"),
Kind::Record(v) => write!(f, "record({})", Fmt::comma_separated(v)),
Kind::Geometry(v) => write!(f, "geometry({})", Fmt::comma_separated(v)),
2020-06-29 15:36:01 +00:00
}
}
}
pub fn kind(i: &str) -> IResult<&str, Kind> {
alt((
map(tag("any"), |_| Kind::Any),
2020-06-29 15:36:01 +00:00
map(tag("array"), |_| Kind::Array),
map(tag("bool"), |_| Kind::Bool),
2023-03-31 22:38:29 +00:00
map(tag("bytes"), |_| Kind::Bytes),
2020-06-29 15:36:01 +00:00
map(tag("datetime"), |_| Kind::Datetime),
map(tag("decimal"), |_| Kind::Decimal),
map(tag("duration"), |_| Kind::Duration),
map(tag("float"), |_| Kind::Float),
map(tag("int"), |_| Kind::Int),
2020-06-29 15:36:01 +00:00
map(tag("number"), |_| Kind::Number),
map(tag("object"), |_| Kind::Object),
map(tag("string"), |_| Kind::String),
map(geometry, Kind::Geometry),
map(record, Kind::Record),
2020-06-29 15:36:01 +00:00
))(i)
}
fn record(i: &str) -> IResult<&str, Vec<Table>> {
let (i, _) = tag("record")(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('(')(i)?;
let (i, v) = separated_list0(commas, table)(i)?;
let (i, _) = char(')')(i)?;
Ok((i, v))
}
fn geometry(i: &str) -> IResult<&str, Vec<String>> {
let (i, _) = tag("geometry")(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('(')(i)?;
let (i, v) = separated_list1(
commas,
map(
alt((
tag("feature"),
tag("point"),
tag("line"),
tag("polygon"),
tag("multipoint"),
tag("multiline"),
tag("multipolygon"),
tag("collection"),
)),
String::from,
),
)(i)?;
let (i, _) = char(')')(i)?;
2020-06-29 15:36:01 +00:00
Ok((i, v))
}