2022-09-25 21:56:33 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-09-25 21:56:33 +00:00
|
|
|
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::map;
|
2023-08-17 18:03:46 +00:00
|
|
|
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)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub enum Base {
|
2023-07-29 18:47:25 +00:00
|
|
|
Root,
|
2020-06-29 15:36:01 +00:00
|
|
|
Ns,
|
|
|
|
Db,
|
2022-09-25 21:56:33 +00:00
|
|
|
Sc(Ident),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Base {
|
2022-10-04 21:51:18 +00:00
|
|
|
fn default() -> Self {
|
2023-07-29 18:47:25 +00:00
|
|
|
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 {
|
2022-10-04 21:51:18 +00:00
|
|
|
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}"),
|
2023-07-29 18:47:25 +00:00
|
|
|
Self::Root => f.write_str("ROOT"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn base(i: &str) -> IResult<&str, Base> {
|
|
|
|
alt((
|
|
|
|
map(tag_no_case("NAMESPACE"), |_| Base::Ns),
|
|
|
|
map(tag_no_case("DATABASE"), |_| Base::Db),
|
2023-07-29 18:47:25 +00:00
|
|
|
map(tag_no_case("ROOT"), |_| Base::Root),
|
2020-06-29 15:36:01 +00:00
|
|
|
map(tag_no_case("NS"), |_| Base::Ns),
|
|
|
|
map(tag_no_case("DB"), |_| Base::Db),
|
2023-07-29 18:47:25 +00:00
|
|
|
map(tag_no_case("KV"), |_| Base::Root),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
2022-09-25 21:56:33 +00:00
|
|
|
|
|
|
|
pub fn base_or_scope(i: &str) -> IResult<&str, Base> {
|
|
|
|
alt((
|
|
|
|
map(tag_no_case("NAMESPACE"), |_| Base::Ns),
|
|
|
|
map(tag_no_case("DATABASE"), |_| Base::Db),
|
2023-07-29 18:47:25 +00:00
|
|
|
map(tag_no_case("ROOT"), |_| Base::Root),
|
2022-09-25 21:56:33 +00:00
|
|
|
map(tag_no_case("NS"), |_| Base::Ns),
|
|
|
|
map(tag_no_case("DB"), |_| Base::Db),
|
2023-07-29 18:47:25 +00:00
|
|
|
map(tag_no_case("KV"), |_| Base::Root),
|
2022-09-25 21:56:33 +00:00
|
|
|
|i| {
|
|
|
|
let (i, _) = tag_no_case("SCOPE")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, v) = ident(i)?;
|
|
|
|
Ok((i, Base::Sc(v)))
|
|
|
|
},
|
|
|
|
))(i)
|
|
|
|
}
|