Skip to main content

reifydb_engine/procedure/
identity_inject.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::{
5	internal_error,
6	value::column::{Column, columns::Columns, data::ColumnData},
7};
8use reifydb_transaction::transaction::Transaction;
9use reifydb_type::{Result, params::Params, value::Value};
10
11use super::{Procedure, context::ProcedureContext};
12
13/// Procedure that injects a new identity into the current session.
14///
15/// Takes 1 positional parameter: the IdentityId to inject.
16/// Returns a single-column result containing the IdentityId value;
17/// the VM intercepts this result and updates its identity accordingly.
18pub struct IdentityInject;
19
20impl IdentityInject {
21	pub fn new() -> Self {
22		Self
23	}
24}
25
26impl Procedure for IdentityInject {
27	fn call(&self, ctx: &ProcedureContext, _tx: &mut Transaction<'_>) -> Result<Columns> {
28		let identity_id = match ctx.params {
29			Params::Positional(args) if args.len() == 1 => match &args[0] {
30				Value::IdentityId(id) => *id,
31				other => {
32					return Err(internal_error!(
33						"identity::inject expects an IdentityId argument, got {:?}",
34						other
35					));
36				}
37			},
38			_ => {
39				return Err(internal_error!(
40					"identity::inject requires exactly 1 positional IdentityId argument"
41				));
42			}
43		};
44
45		let col = Column::new("identity_id", ColumnData::identity_id(vec![identity_id]));
46		Ok(Columns::new(vec![col]))
47	}
48}