surrealdb_core/sql/
edges.rs1use crate::dbs::Options;
2use crate::doc::CursorDoc;
3use crate::err::Error;
4use crate::sql::table::Tables;
5use crate::sql::thing::Thing;
6use crate::{ctx::Context, sql::dir::Dir};
7use reblessive::tree::Stk;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12use super::graph::GraphSubjects;
13use super::Value;
14
15pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Edges";
16
17#[revisioned(revision = 2)]
18#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
19#[serde(rename = "$surrealdb::private::sql::Edges")]
20#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
21#[non_exhaustive]
22pub struct Edges {
23 pub dir: Dir,
24 pub from: Thing,
25 #[revision(end = 2, convert_fn = "convert_old_what")]
26 pub _what: Tables,
27 #[revision(start = 2)]
28 pub what: GraphSubjects,
29}
30
31impl Edges {
32 pub fn new(dir: Dir, from: Thing, what: GraphSubjects) -> Self {
33 Edges {
34 dir,
35 from,
36 what,
37 }
38 }
39
40 fn convert_old_what(&mut self, _rev: u16, old: Tables) -> Result<(), revision::Error> {
41 self.what = old.into();
42 Ok(())
43 }
44
45 pub(crate) async fn compute(
46 &self,
47 stk: &mut Stk,
48 ctx: &Context,
49 opt: &Options,
50 doc: Option<&CursorDoc>,
51 ) -> Result<Value, Error> {
52 Ok(Value::Edges(Box::new(Self {
53 dir: self.dir.clone(),
54 from: self.from.clone(),
55 what: self.what.clone().compute(stk, ctx, opt, doc).await?,
56 })))
57 }
58}
59
60impl fmt::Display for Edges {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 match self.what.len() {
63 0 => write!(f, "{}{}?", self.from, self.dir,),
64 1 => write!(f, "{}{}{}", self.from, self.dir, self.what),
65 _ => write!(f, "{}{}({})", self.from, self.dir, self.what),
66 }
67 }
68}