2022-03-18 07:24:36 +00:00
|
|
|
use crate::cnf::ID_CHARS;
|
2022-08-26 23:23:24 +00:00
|
|
|
use crate::sql::array::{array, Array};
|
2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-07-06 09:08:11 +00:00
|
|
|
use crate::sql::escape::escape_id;
|
2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::ident::ident_raw;
|
2022-06-08 07:39:09 +00:00
|
|
|
use crate::sql::number::integer;
|
2022-08-26 23:23:24 +00:00
|
|
|
use crate::sql::object::{object, Object};
|
2022-09-24 01:57:27 +00:00
|
|
|
use crate::sql::strand::Strand;
|
|
|
|
use crate::sql::uuid::Uuid;
|
2022-03-18 07:24:36 +00:00
|
|
|
use nanoid::nanoid;
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::combinator::map;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub enum Id {
|
2022-06-08 07:39:09 +00:00
|
|
|
Number(i64),
|
2022-03-18 07:24:36 +00:00
|
|
|
String(String),
|
2022-08-26 23:23:24 +00:00
|
|
|
Array(Array),
|
2022-08-31 13:37:22 +00:00
|
|
|
Object(Object),
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 07:39:09 +00:00
|
|
|
impl From<i64> for Id {
|
|
|
|
fn from(v: i64) -> Self {
|
2022-03-18 07:24:36 +00:00
|
|
|
Id::Number(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 07:39:09 +00:00
|
|
|
impl From<i32> for Id {
|
|
|
|
fn from(v: i32) -> Self {
|
|
|
|
Id::Number(v as i64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Id {
|
|
|
|
fn from(v: u64) -> Self {
|
|
|
|
Id::Number(v as i64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 01:57:27 +00:00
|
|
|
impl From<String> for Id {
|
|
|
|
fn from(v: String) -> Self {
|
|
|
|
Id::String(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:40:19 +00:00
|
|
|
impl From<Array> for Id {
|
|
|
|
fn from(v: Array) -> Self {
|
|
|
|
Id::Array(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Object> for Id {
|
|
|
|
fn from(v: Object) -> Self {
|
|
|
|
Id::Object(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 01:57:27 +00:00
|
|
|
impl From<Uuid> for Id {
|
|
|
|
fn from(v: Uuid) -> Self {
|
|
|
|
Id::String(v.to_raw())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Strand> for Id {
|
|
|
|
fn from(v: Strand) -> Self {
|
|
|
|
Id::String(v.as_string())
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Id {
|
|
|
|
fn from(v: &str) -> Self {
|
|
|
|
Id::String(v.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Id {
|
|
|
|
pub fn rand() -> Id {
|
|
|
|
Id::String(nanoid!(20, &ID_CHARS))
|
|
|
|
}
|
2022-07-06 09:17:16 +00:00
|
|
|
pub fn to_raw(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Id::Number(v) => v.to_string(),
|
|
|
|
Id::String(v) => v.to_string(),
|
2022-08-26 23:23:24 +00:00
|
|
|
Id::Object(v) => v.to_string(),
|
|
|
|
Id::Array(v) => v.to_string(),
|
2022-07-06 09:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Id {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Id::Number(v) => write!(f, "{}", v),
|
2022-07-06 09:08:11 +00:00
|
|
|
Id::String(v) => write!(f, "{}", escape_id(v)),
|
2022-08-26 23:23:24 +00:00
|
|
|
Id::Object(v) => write!(f, "{}", v),
|
|
|
|
Id::Array(v) => write!(f, "{}", v),
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(i: &str) -> IResult<&str, Id> {
|
2022-08-26 23:23:24 +00:00
|
|
|
alt((
|
|
|
|
map(integer, Id::Number),
|
|
|
|
map(ident_raw, Id::String),
|
|
|
|
map(object, Id::Object),
|
|
|
|
map(array, Id::Array),
|
|
|
|
))(i)
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2022-09-14 22:40:19 +00:00
|
|
|
#[test]
|
|
|
|
fn id_int() {
|
|
|
|
let sql = "001";
|
|
|
|
let res = id(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(Id::from(1), out);
|
|
|
|
}
|
|
|
|
|
2022-03-18 07:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn id_number() {
|
|
|
|
let sql = "100";
|
|
|
|
let res = id(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(Id::from(100), out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn id_string() {
|
|
|
|
let sql = "test";
|
|
|
|
let res = id(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(Id::from("test"), out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn id_either() {
|
|
|
|
let sql = "100test";
|
|
|
|
let res = id(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(Id::from("100test"), out);
|
|
|
|
}
|
|
|
|
}
|