uni_plugin/host/context.rs
1//! Procedure-context construction helper.
2//!
3//! Collapses the ~25-line "build host + attach writer + read principal +
4//! construct context" block previously duplicated between the
5//! simple-executor and DataFusion-graph procedure-call paths in
6//! `uni-query` (see `query/executor/procedure.rs` and
7//! `query/df_graph/procedure_call.rs`).
8//!
9//! Writer attachment varies between the two paths (`from_components` vs.
10//! `from_graph_ctx_with_request` plus a writer mutation on the concrete
11//! `QueryProcedureHost`), so it stays at the caller. This helper owns
12//! only the principal-attachment + context-build step, which is the
13//! piece that was textually identical at both sites.
14
15// Rust guideline compliant
16
17use crate::traits::connector::Principal;
18use crate::traits::procedure::{ProcedureContext, ProcedureHost};
19
20/// Build a [`ProcedureContext`] wiring `host` and an optional `principal`.
21///
22/// Replaces the duplicated "construct `ProcedureContext::new()`, chain
23/// `with_host`, then conditionally chain `with_principal`" block. The
24/// principal, when `Some`, is attached via the existing
25/// [`ProcedureContext::with_principal`] builder; when `None` it is
26/// simply omitted.
27///
28/// Writer attachment to the host happens at the call site because the
29/// two host construction paths (`from_components` and
30/// `from_graph_ctx_with_request` on `QueryProcedureHost`) differ in
31/// shape; only the context-build was textually identical.
32///
33/// # Examples
34///
35/// ```no_run
36/// use uni_plugin::host::context::build_procedure_context;
37/// use uni_plugin::traits::connector::Principal;
38/// use uni_plugin::traits::procedure::ProcedureHost;
39///
40/// # fn demo(host: &dyn ProcedureHost, principal: Option<&Principal>) {
41/// let ctx = build_procedure_context(host, principal);
42/// // pass `ctx` to `procedure.invoke(ctx, &args)`
43/// # let _ = ctx;
44/// # }
45/// ```
46#[must_use]
47pub fn build_procedure_context<'a>(
48 host: &'a dyn ProcedureHost,
49 principal: Option<&'a Principal>,
50) -> ProcedureContext<'a> {
51 let mut ctx = ProcedureContext::new().with_host(host);
52 if let Some(p) = principal {
53 ctx = ctx.with_principal(p);
54 }
55 ctx
56}