Skip to main content

EnabledSet

Struct EnabledSet 

Source
pub struct EnabledSet { /* private fields */ }
Expand description

命令启用状态表。廉价共享(内部锁),运行期可改。

Implementations§

Source§

impl EnabledSet

Source

pub fn new() -> EnabledSet

Source

pub fn set(&self, key: impl Into<SwitchKey>, peer: Option<Peer>, enabled: bool)

设置某命令的启用状态:peer=None 为全局覆盖,Some(p) 为按会话覆盖。 在释放写锁后触发 on_change 回调。

同时接受纯字符串键与类型化的 SwitchKey#[command]/#[event] 宏产出的 <FN>_KEY 常量),故触发器键无需手敲字符串。

Examples found in repository?
examples/plugin_switches.rs (line 48)
41async fn disable(reply: Reply, es: State<EnabledSet>, peer: EventPeer, args: ArgText) -> HandlerResult {
42    let key = args.0.trim();
43    if key.is_empty() {
44        reply.text("用法:/disable <key>").await?;
45        return Ok(());
46    }
47    // `State<EnabledSet>` deref 到 `EnabledSet`,所以 `es.set(..)` 直接调通。
48    es.set(key, Some(peer.0), false);
49    // 引用触发命令,让确认消息挂在它下面。
50    reply.reply(format!("已在本会话禁用:{key}")).await?;
51    Ok(())
52}
53
54#[command("/enable", id = "enable", can_disable = false)]
55async fn enable(reply: Reply, es: State<EnabledSet>, peer: EventPeer, args: ArgText) -> HandlerResult {
56    let key = args.0.trim();
57    if key.is_empty() {
58        reply.text("用法:/enable <key>").await?;
59        return Ok(());
60    }
61    es.set(key, Some(peer.0), true);
62    reply.reply(format!("已在本会话启用:{key}")).await?;
63    Ok(())
64}
Source

pub fn reset(&self, key: impl Into<SwitchKey>, peer: Option<Peer>)

清除某命令的覆盖,回到 default_enablepeer=None 清全局,Some 清该会话。

Source

pub fn is_enabled_keyed( &self, plugin_key: &str, trigger_key: &str, plugin_default: bool, plugin_can_disable: bool, trigger_default: bool, trigger_can_disable: bool, peer: Option<Peer>, ) -> bool

分层的触发器判定:

  1. 触发器 can_disable=false → 恒开(豁免,无视总开关);
  2. 插件总开关 OFF(且插件 can_disable)→ 关(一刀压平);
  3. 否则看触发器自身的开关。
Source

pub fn is_enabled( &self, name: &str, default_enable: bool, peer: Option<Peer>, ) -> bool

单键查询(无插件层适用处用它)。

Source

pub fn on_change<F>(&self, f: F)
where F: Fn(&str, Option<Peer>, bool) + Send + Sync + 'static,

注册一个在每次 set 调用后(锁释放后)触发的回调。只能注册一个,再次调用替换前一个。 reset 不触发回调(它是本地回退,不是需持久化的变更)。

Source

pub fn snapshot(&self) -> EnabledOverrides

把当前所有覆盖快照成一个可序列化结构。

Source

pub fn restore(&self, ov: EnabledOverrides)

从先前快照的结构恢复覆盖(替换当前状态)。

Trait Implementations§

Source§

impl Default for EnabledSet

Source§

fn default() -> EnabledSet

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more