surrealpatch/core/src/sql/v1/part.rs

127 lines
2.5 KiB
Rust
Raw Normal View History

use crate::sql::{fmt::Fmt, strand::no_nul_bytes, Graph, Ident, Idiom, Number, Value};
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
2024-01-09 15:34:52 +00:00
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Part {
All,
Flatten,
Last,
First,
Field(Ident),
Index(Number),
Where(Value),
Graph(Graph),
Value(Value),
2023-07-15 07:08:26 +00:00
Start(Value),
Method(#[serde(with = "no_nul_bytes")] String, Vec<Value>),
}
impl From<i32> for Part {
fn from(v: i32) -> Self {
Self::Index(v.into())
}
}
impl From<isize> for Part {
fn from(v: isize) -> Self {
Self::Index(v.into())
}
}
impl From<usize> for Part {
fn from(v: usize) -> Self {
Self::Index(v.into())
}
}
impl From<String> for Part {
fn from(v: String) -> Self {
Self::Field(v.into())
}
}
impl From<Number> for Part {
fn from(v: Number) -> Self {
Self::Index(v)
}
}
impl From<Ident> for Part {
fn from(v: Ident) -> Self {
Self::Field(v)
}
}
impl From<Graph> for Part {
fn from(v: Graph) -> Self {
Self::Graph(v)
}
}
impl From<&str> for Part {
fn from(v: &str) -> Self {
match v.parse::<isize>() {
Ok(v) => Self::from(v),
_ => Self::from(v.to_owned()),
}
}
}
impl Part {
/// Check if we require a writeable transaction
pub(crate) fn writeable(&self) -> bool {
match self {
2023-07-15 07:08:26 +00:00
Part::Start(v) => v.writeable(),
Part::Where(v) => v.writeable(),
Part::Value(v) => v.writeable(),
Part::Method(_, v) => v.iter().any(Value::writeable),
_ => false,
}
}
2022-10-19 09:55:19 +00:00
/// Returns a yield if an alias is specified
pub(crate) fn alias(&self) -> Option<&Idiom> {
match self {
Part::Graph(v) => v.alias.as_ref(),
_ => None,
}
}
}
impl fmt::Display for Part {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Part::All => f.write_str("[*]"),
Part::Last => f.write_str("[$]"),
Part::First => f.write_str("[0]"),
2023-07-15 07:08:26 +00:00
Part::Start(v) => write!(f, "{v}"),
2023-02-03 11:47:07 +00:00
Part::Field(v) => write!(f, ".{v}"),
Part::Flatten => f.write_str(""),
2023-02-03 11:47:07 +00:00
Part::Index(v) => write!(f, "[{v}]"),
Part::Where(v) => write!(f, "[WHERE {v}]"),
Part::Graph(v) => write!(f, "{v}"),
2023-07-15 07:08:26 +00:00
Part::Value(v) => write!(f, "[{v}]"),
Part::Method(v, a) => write!(f, ".{v}({})", Fmt::comma_separated(a)),
}
}
}
// ------------------------------
pub trait Next<'a> {
fn next(&'a self) -> &[Part];
}
impl<'a> Next<'a> for &'a [Part] {
fn next(&'a self) -> &'a [Part] {
match self.len() {
0 => &[],
_ => &self[1..],
}
}
}