surrealdb_core/sql/
limit.rs

1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::sql::number::Number;
6use crate::sql::value::Value;
7use reblessive::tree::Stk;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[revisioned(revision = 1)]
13#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15#[non_exhaustive]
16pub struct Limit(pub Value);
17
18impl Limit {
19	pub(crate) async fn process(
20		&self,
21		stk: &mut Stk,
22		ctx: &Context,
23		opt: &Options,
24		doc: Option<&CursorDoc>,
25	) -> Result<u32, Error> {
26		match self.0.compute(stk, ctx, opt, doc).await {
27			// This is a valid limiting number
28			Ok(Value::Number(Number::Int(v))) if v >= 0 => {
29				if v > u32::MAX as i64 {
30					Err(Error::InvalidLimit {
31						value: v.to_string(),
32					})
33				} else {
34					Ok(v as u32)
35				}
36			}
37			// An invalid value was specified
38			Ok(v) => Err(Error::InvalidLimit {
39				value: v.as_string(),
40			}),
41			// A different error occurred
42			Err(e) => Err(e),
43		}
44	}
45}
46
47impl fmt::Display for Limit {
48	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49		write!(f, "LIMIT {}", self.0)
50	}
51}