Skip to main content

surrealdb_expr/expr/statements/define/
field.rs

1use super::DefineKind;
2use crate::expr::permission::Permissions;
3use crate::expr::reference::Reference;
4use crate::expr::{Expr, Kind, KindLiteral, Literal};
5
6/// Returns true if this type contains an `object` anywhere (including literal
7/// object types, `array<object>` and `option<object>`).
8pub fn kind_contains_object(kind: &Kind) -> bool {
9	match kind {
10		Kind::Object => true,
11		Kind::Either(kinds) => kinds.iter().any(kind_contains_object),
12		Kind::Array(inner, _) | Kind::Set(inner, _) => kind_contains_object(inner),
13		Kind::Literal(KindLiteral::Object(_)) => true,
14		Kind::Literal(KindLiteral::Array(kinds)) => kinds.iter().any(kind_contains_object),
15		_ => false,
16	}
17}
18
19#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
20pub enum DefineDefault {
21	#[default]
22	None,
23	Always(Expr),
24	Set(Expr),
25}
26
27#[derive(Clone, Debug, Eq, PartialEq, Hash)]
28pub struct DefineFieldStatement {
29	pub kind: DefineKind,
30	pub name: Expr,
31	pub what: Expr,
32	pub field_kind: Option<Kind>,
33	pub flexible: bool,
34	pub readonly: bool,
35	/// Inline the field's value into the edge's adjacency (the `INLINE` clause).
36	pub inline: bool,
37	pub value: Option<Expr>,
38	pub assert: Option<Expr>,
39	pub computed: Option<Expr>,
40	pub default: DefineDefault,
41	pub permissions: Permissions,
42	pub comment: Expr,
43	pub reference: Option<Reference>,
44	pub graphql_alias: Option<String>,
45	pub graphql_deprecated: Option<String>,
46}
47
48impl Default for DefineFieldStatement {
49	fn default() -> Self {
50		Self {
51			kind: DefineKind::Default,
52			name: Expr::Literal(Literal::None),
53			what: Expr::Literal(Literal::None),
54			field_kind: None,
55			flexible: false,
56			readonly: false,
57			inline: false,
58			value: None,
59			assert: None,
60			computed: None,
61			default: DefineDefault::None,
62			permissions: Permissions::default(),
63			comment: Expr::Literal(Literal::None),
64			reference: None,
65			graphql_alias: None,
66			graphql_deprecated: None,
67		}
68	}
69}