surrealpatch/lib/src/sql/base.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

use crate::sql::comment::shouldbespace;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
use crate::sql::ident::{ident, Ident};
2020-06-29 15:36:01 +00:00
use nom::branch::alt;
use nom::bytes::complete::tag_no_case;
use nom::combinator::{cut, value};
use revision::revisioned;
2020-06-29 15:36:01 +00:00
use serde::{Deserialize, Serialize};
use std::fmt;
2023-08-18 22:51:56 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
2020-06-29 15:36:01 +00:00
pub enum Base {
Root,
2020-06-29 15:36:01 +00:00
Ns,
Db,
Sc(Ident),
2020-06-29 15:36:01 +00:00
}
impl Default for Base {
fn default() -> Self {
Self::Root
2020-06-29 15:36:01 +00:00
}
}
impl fmt::Display for Base {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Ns => f.write_str("NAMESPACE"),
Self::Db => f.write_str("DATABASE"),
2023-02-03 11:47:07 +00:00
Self::Sc(sc) => write!(f, "SCOPE {sc}"),
Self::Root => f.write_str("ROOT"),
2020-06-29 15:36:01 +00:00
}
}
}
pub fn base(i: &str) -> IResult<&str, Base> {
alt((
value(Base::Ns, tag_no_case("NAMESPACE")),
value(Base::Db, tag_no_case("DATABASE")),
value(Base::Root, tag_no_case("ROOT")),
value(Base::Ns, tag_no_case("NS")),
value(Base::Db, tag_no_case("DB")),
value(Base::Root, tag_no_case("KV")),
2020-06-29 15:36:01 +00:00
))(i)
}
pub fn base_or_scope(i: &str) -> IResult<&str, Base> {
alt((base, |i| {
let (i, _) = tag_no_case("SCOPE")(i)?;
let (i, _) = shouldbespace(i)?;
let (i, v) = cut(ident)(i)?;
Ok((i, Base::Sc(v)))
}))(i)
}