surrealpatch/lib/src/sql/kind.rs

91 lines
2.2 KiB
Rust
Raw Normal View History

use crate::sql::fmt::Fmt;
use crate::sql::table::Table;
use revision::revisioned;
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
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 Kind {
Any,
Null,
2020-06-29 15:36:01 +00:00
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,
Point,
2020-06-29 15:36:01 +00:00
String,
Uuid,
2020-06-29 15:36:01 +00:00
Record(Vec<Table>),
Geometry(Vec<String>),
Option(Box<Kind>),
Either(Vec<Kind>),
Set(Box<Kind>, Option<u64>),
Array(Box<Kind>, Option<u64>),
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 Kind {
fn is_any(&self) -> bool {
matches!(self, Kind::Any)
}
}
impl From<&Kind> for Box<Kind> {
#[inline]
fn from(v: &Kind) -> Self {
Box::new(v.clone())
}
}
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::Null => f.write_str("null"),
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::Point => f.write_str("point"),
Kind::String => f.write_str("string"),
Kind::Uuid => f.write_str("uuid"),
Kind::Option(k) => write!(f, "option<{}>", k),
Kind::Record(k) => match k {
k if k.is_empty() => write!(f, "record"),
k => write!(f, "record<{}>", Fmt::verbar_separated(k)),
},
Kind::Geometry(k) => match k {
k if k.is_empty() => write!(f, "geometry"),
k => write!(f, "geometry<{}>", Fmt::verbar_separated(k)),
},
Kind::Set(k, l) => match (k, l) {
(k, None) if k.is_any() => write!(f, "set"),
(k, None) => write!(f, "set<{k}>"),
(k, Some(l)) => write!(f, "set<{k}, {l}>"),
},
Kind::Array(k, l) => match (k, l) {
(k, None) if k.is_any() => write!(f, "array"),
(k, None) => write!(f, "array<{k}>"),
(k, Some(l)) => write!(f, "array<{k}, {l}>"),
},
Kind::Either(k) => write!(f, "{}", Fmt::verbar_separated(k)),
2020-06-29 15:36:01 +00:00
}
}
}