Skip to main content

surrealdb_sql/
mock.rs

1use std::ops::Bound;
2
3use common::fmt::EscapeKwFreeIdent;
4use common::range::TypedRange;
5use surrealdb_types::{SqlFormat, ToSql, write_sql};
6
7use crate::TableName;
8
9#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Hash)]
10#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
11pub enum Mock {
12	Count(TableName, i64),
13	Range(TableName, TypedRange<i64>),
14	// Add new variants here
15}
16
17impl ToSql for Mock {
18	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
19		match self {
20			Mock::Count(tb, c) => {
21				write_sql!(f, fmt, "|{}:{}|", EscapeKwFreeIdent(tb.as_str()), c);
22			}
23			Mock::Range(tb, r) => {
24				write_sql!(f, fmt, "|{}:", EscapeKwFreeIdent(tb.as_str()));
25				match r.start {
26					Bound::Included(x) => write_sql!(f, fmt, "{x}.."),
27					Bound::Excluded(x) => write_sql!(f, fmt, "{x}>.."),
28					Bound::Unbounded => f.push_str(".."),
29				}
30				match r.end {
31					Bound::Included(x) => write_sql!(f, fmt, "={x}|"),
32					Bound::Excluded(x) => write_sql!(f, fmt, "{x}|"),
33					Bound::Unbounded => f.push('|'),
34				}
35			}
36		}
37	}
38}