surrealdb_core/dbs/
notification.rs

1#[cfg(test)]
2use crate::dbs::fuzzy_eq::FuzzyEq;
3use crate::sql::{Object, Uuid, Value};
4use revision::revisioned;
5use serde::{Deserialize, Serialize};
6use std::fmt::{self, Debug, Display};
7
8#[revisioned(revision = 1)]
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10#[serde(rename_all = "UPPERCASE")]
11#[non_exhaustive]
12pub enum Action {
13	Create,
14	Update,
15	Delete,
16}
17
18impl Display for Action {
19	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20		match *self {
21			Action::Create => write!(f, "CREATE"),
22			Action::Update => write!(f, "UPDATE"),
23			Action::Delete => write!(f, "DELETE"),
24		}
25	}
26}
27
28#[revisioned(revision = 1)]
29#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
30#[non_exhaustive]
31pub struct Notification {
32	/// The id of the LIVE query to which this notification belongs
33	pub id: Uuid,
34	/// The CREATE / UPDATE / DELETE action which caused this notification
35	pub action: Action,
36	/// The id of the document to which this notification has been made
37	pub record: Value,
38	/// The resulting notification content, usually the altered record content
39	pub result: Value,
40}
41
42impl Display for Notification {
43	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44		let obj: Object = map! {
45			"id".to_string() => self.id.to_string().into(),
46			"action".to_string() => self.action.to_string().into(),
47			"record".to_string() => self.record.clone(),
48			"result".to_string() => self.result.clone(),
49		}
50		.into();
51		write!(f, "{}", obj)
52	}
53}
54
55impl Notification {
56	/// Construct a new notification
57	pub const fn new(id: Uuid, action: Action, record: Value, result: Value) -> Self {
58		Self {
59			id,
60			action,
61			record,
62			result,
63		}
64	}
65}
66
67#[cfg(test)]
68impl FuzzyEq for Notification {
69	fn fuzzy_eq(&self, other: &Self) -> bool {
70		self.action == other.action && self.record == other.record && self.result == other.result
71	}
72}