2022-08-04 14:11:50 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
2022-08-04 14:12:06 +00:00
|
|
|
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::table::{table, Table};
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::combinator::map;
|
2022-08-04 14:12:06 +00:00
|
|
|
use nom::multi::separated_list1;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub enum Kind {
|
|
|
|
Any,
|
|
|
|
Array,
|
|
|
|
Bool,
|
|
|
|
Datetime,
|
2022-04-04 21:43:10 +00:00
|
|
|
Decimal,
|
|
|
|
Duration,
|
|
|
|
Float,
|
|
|
|
Int,
|
2020-06-29 15:36:01 +00:00
|
|
|
Number,
|
|
|
|
Object,
|
|
|
|
String,
|
|
|
|
Record(Vec<Table>),
|
2022-04-04 21:43:10 +00:00
|
|
|
Geometry(Vec<String>),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Kind {
|
|
|
|
fn default() -> Kind {
|
|
|
|
Kind::Any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Kind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2022-09-27 21:35:30 +00:00
|
|
|
Kind::Any => f.write_str("any"),
|
|
|
|
Kind::Array => f.write_str("array"),
|
|
|
|
Kind::Bool => f.write_str("bool"),
|
|
|
|
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"),
|
2022-01-13 17:36:41 +00:00
|
|
|
Kind::Record(v) => write!(
|
2020-06-29 15:36:01 +00:00
|
|
|
f,
|
|
|
|
"record({})",
|
2022-04-04 21:43:10 +00:00
|
|
|
v.iter().map(|ref v| v.to_string()).collect::<Vec<_>>().join(", ")
|
|
|
|
),
|
|
|
|
Kind::Geometry(v) => write!(
|
|
|
|
f,
|
|
|
|
"geometry({})",
|
|
|
|
v.iter().map(|ref v| v.to_string()).collect::<Vec<_>>().join(", ")
|
2020-06-29 15:36:01 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn kind(i: &str) -> IResult<&str, Kind> {
|
|
|
|
alt((
|
2022-01-13 17:36:41 +00:00
|
|
|
map(tag("any"), |_| Kind::Any),
|
2020-06-29 15:36:01 +00:00
|
|
|
map(tag("array"), |_| Kind::Array),
|
|
|
|
map(tag("bool"), |_| Kind::Bool),
|
|
|
|
map(tag("datetime"), |_| Kind::Datetime),
|
2022-04-04 21:43:10 +00:00
|
|
|
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),
|
2022-03-04 16:01:32 +00:00
|
|
|
map(geometry, Kind::Geometry),
|
|
|
|
map(record, Kind::Record),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:43:10 +00:00
|
|
|
fn record(i: &str) -> IResult<&str, Vec<Table>> {
|
|
|
|
let (i, _) = tag("record")(i)?;
|
2022-08-04 14:11:50 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('(')(i)?;
|
2022-08-04 14:12:06 +00:00
|
|
|
let (i, v) = separated_list1(commas, table)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(')')(i)?;
|
2022-04-04 21:43:10 +00:00
|
|
|
Ok((i, v))
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:43:10 +00:00
|
|
|
fn geometry(i: &str) -> IResult<&str, Vec<String>> {
|
|
|
|
let (i, _) = tag("geometry")(i)?;
|
2022-08-04 14:11:50 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('(')(i)?;
|
2022-08-04 14:12:06 +00:00
|
|
|
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)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(')')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, v))
|
|
|
|
}
|