Skip to main content

HookContextExt

Trait HookContextExt 

Source
pub trait HookContextExt {
    // Required methods
    fn with_meta(self, key: impl Into<String>, value: impl Into<String>) -> Self;
    fn with_metas<I, K, V>(self, entries: I) -> Self
       where I: IntoIterator<Item = (K, V)>,
             K: Into<String>,
             V: Into<String>;
}
Expand description

HookContext Builder 扩展 trait

HookContext 补充 with_meta/with_metas builder 链式 API, 与 sz-orm-core 的 with_tenant/with_operator/with_timestamp 风格一致。

§示例

use sz_rust_core::hooks::{hook_context, HookContextExt};

let ctx = hook_context()
    .with_tenant(42)
    .with_operator(1)
    .with_timestamp(1700000000)
    .with_meta("source", "api")
    .with_meta("ip", "127.0.0.1");

assert_eq!(ctx.tenant_id, Some(42));
assert_eq!(ctx.operator_id, Some(1));
assert_eq!(ctx.timestamp, 1700000000);
assert_eq!(ctx.get_meta("source"), Some(&"api".to_string()));
assert_eq!(ctx.get_meta("ip"), Some(&"127.0.0.1".to_string()));

Required Methods§

Source

fn with_meta(self, key: impl Into<String>, value: impl Into<String>) -> Self

链式添加单个元数据(消耗 self,返回新实例)

对齐 sz-orm-core with_tenant/with_operator/with_timestamp 的 builder 风格, 便于一行链式构造完整上下文。与 set_meta(&mut self, ...) 的区别:

  • with_meta:消耗 self,返回新实例,适合 builder 链式调用
  • set_meta&mut self,就地在 HashMap 中插入,适合多次修改同一实例
Source

fn with_metas<I, K, V>(self, entries: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

链式批量添加元数据(消耗 self,返回新实例)

接受 IntoIterator<Item = (impl Into<String>, impl Into<String>)>, 便于从 HashMap/Vec/数组等多种数据源批量构造元数据。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§