Skip to main content

PluginEvent

Trait PluginEvent 

Source
pub trait PluginEvent: Sized {
    const NAME: &'static str;
    const DOC: &'static str;

    // Required methods
    fn encode(&self) -> Vec<u8> ⓘ;
    fn decode(bytes: &[u8]) -> Result<Self, DecodeError>;
}
Expand description

A plugin-defined event: a typed view over the opaque emit-event / on-event wire (PH7.8b.2). Implement via #[derive(PluginEvent)] on a serde-serializable struct; hand-implementing is possible but rarely needed.

NAME is the wire identifier (matched by subscribers, registered via register-event); DOC is the human summary surfaced in :describe-event. encode / decode round-trip the payload as MessagePack.

The derive requires the struct to implement serde’s Serialize and Deserialize; its NAME defaults to the kebab-cased type name (MyCustomEvent → my-custom-event, acronyms degrade per letter), so real plugins namespace it explicitly with #[event(name = "plugin.event")].

§Examples

use lattice_plugin_sdk::PluginEvent;
use serde::{Deserialize, Serialize};

/// Kebab-name fallback event.
#[derive(Debug, PartialEq, Serialize, Deserialize, PluginEvent)]
struct MyCustomEvent {
    value: i64,
}

assert_eq!(MyCustomEvent::NAME, "my-custom-event");
assert_eq!(MyCustomEvent::DOC, "Kebab-name fallback event.");

let bytes = MyCustomEvent { value: 7 }.encode();
assert_eq!(MyCustomEvent::decode(&bytes), Ok(MyCustomEvent { value: 7 }));
// A payload for some other type is a typed error, never a panic.
assert!(MyCustomEvent::decode(&[0xc0]).is_err());

Required Associated Constants§

Source

const NAME: &'static str

The event’s wire name — the identifier crossed to emit-event and matched by subscribers (e.g. "git.hunks-changed").

Source

const DOC: &'static str

The human-facing doc (from the struct’s /// comment), shown by :describe-event once registered.

Required Methods§

Source

fn encode(&self) -> Vec<u8> ⓘ

Serialize to the opaque MessagePack payload emit-event carries.

§Panics

The derived impl panics only if the type’s Serialize impl itself errors, which a plain derived serde struct never does; a hand-written Serialize that can fail is treated as a programming bug.

Source

fn decode(bytes: &[u8]) -> Result<Self, DecodeError>

Deserialize from a payload received on on-event. A malformed / mistyped payload is a typed DecodeError, never a panic.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§