surrealdb_core/sql/
version.rs

1use crate::{ctx::Context, dbs::Options, doc::CursorDoc, err::Error, sql::datetime::Datetime};
2use reblessive::tree::Stk;
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7use super::Value;
8
9#[revisioned(revision = 2)]
10#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
11#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
12#[non_exhaustive]
13pub struct Version(
14	#[revision(end = 2, convert_fn = "convert_version_datetime")] pub Datetime,
15	#[revision(start = 2)] pub Value,
16);
17
18impl Version {
19	fn convert_version_datetime(
20		&mut self,
21		_revision: u16,
22		old: Datetime,
23	) -> Result<(), revision::Error> {
24		self.0 = Value::Datetime(old);
25		Ok(())
26	}
27
28	pub(crate) async fn compute(
29		&self,
30		stk: &mut Stk,
31		ctx: &Context,
32		opt: &Options,
33		doc: Option<&CursorDoc>,
34	) -> Result<u64, Error> {
35		match self.0.compute(stk, ctx, opt, doc).await? {
36			Value::Datetime(v) => match v.to_u64() {
37				Some(ts) => Ok(ts),
38				_ => Err(Error::Unreachable("Failed to convert datetime to timestamp".into())),
39			},
40			found => Err(Error::InvalidVersion {
41				found,
42			}),
43		}
44	}
45}
46
47impl fmt::Display for Version {
48	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49		write!(f, "VERSION {}", self.0)
50	}
51}