surrealdb_core/sql/
mock.rs

1use crate::sql::{escape::escape_ident, Id, Thing};
2use revision::revisioned;
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Mock";
7
8#[non_exhaustive]
9pub struct IntoIter {
10	model: Mock,
11	index: u64,
12}
13
14impl Iterator for IntoIter {
15	type Item = Thing;
16	fn next(&mut self) -> Option<Thing> {
17		match &self.model {
18			Mock::Count(tb, c) => {
19				if self.index < *c {
20					self.index += 1;
21					Some(Thing {
22						tb: tb.to_string(),
23						id: Id::rand(),
24					})
25				} else {
26					None
27				}
28			}
29			Mock::Range(tb, b, e) => {
30				if self.index == 0 {
31					self.index = *b - 1;
32				}
33				if self.index < *e {
34					self.index += 1;
35					Some(Thing {
36						tb: tb.to_string(),
37						id: Id::from(self.index),
38					})
39				} else {
40					None
41				}
42			}
43		}
44	}
45}
46
47#[revisioned(revision = 1)]
48#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
49#[serde(rename = "$surrealdb::private::sql::Mock")]
50#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
51#[non_exhaustive]
52pub enum Mock {
53	Count(String, u64),
54	Range(String, u64, u64),
55	// Add new variants here
56}
57
58impl IntoIterator for Mock {
59	type Item = Thing;
60	type IntoIter = IntoIter;
61	fn into_iter(self) -> Self::IntoIter {
62		IntoIter {
63			model: self,
64			index: 0,
65		}
66	}
67}
68
69impl fmt::Display for Mock {
70	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71		match self {
72			Mock::Count(tb, c) => {
73				write!(f, "|{}:{}|", escape_ident(tb), c)
74			}
75			Mock::Range(tb, b, e) => {
76				write!(f, "|{}:{}..{}|", escape_ident(tb), b, e)
77			}
78		}
79	}
80}