surrealdb_sql/
mock.rs

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