Skip to main content

surrealdb_sql/statements/define/config/
api.rs

1use common::fmt::{EscapeKwFreeIdent, Fmt};
2use surrealdb_strand::Strand;
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4
5use crate::{CoverStmts, Expr, Permission};
6
7#[derive(Clone, Debug, Default, Eq, PartialEq)]
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9pub struct ApiConfig {
10	pub middleware: Vec<Middleware>,
11	pub permissions: Permission,
12}
13
14impl ToSql for ApiConfig {
15	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
16		if !self.middleware.is_empty() {
17			write_sql!(f, fmt, " MIDDLEWARE ");
18
19			for (idx, m) in self.middleware.iter().enumerate() {
20				if idx != 0 {
21					f.push_str(", ");
22				}
23				for (idx, s) in m.name.as_str().split("::").enumerate() {
24					if idx != 0 {
25						f.push_str("::");
26					}
27					EscapeKwFreeIdent(s).fmt_sql(f, fmt);
28				}
29				write_sql!(
30					f,
31					fmt,
32					"({})",
33					Fmt::pretty_comma_separated(m.args.iter().map(CoverStmts))
34				);
35			}
36		}
37
38		write_sql!(f, fmt, " PERMISSIONS {}", self.permissions);
39	}
40}
41
42#[derive(Clone, Debug, Default, Eq, PartialEq)]
43pub struct Middleware {
44	pub name: Strand,
45	pub args: Vec<Expr>,
46}