Skip to main content

yosh_plugin_api/
lib.rs

1use std::ffi::{c_char, c_void};
2
3/// API version for compatibility checks between yosh and plugins.
4pub const YOSH_PLUGIN_API_VERSION: u32 = 2;
5
6// ── Capability bitflags ───────────────────────────────────────────────
7
8pub const CAP_VARIABLES_READ: u32 = 0x01;
9pub const CAP_VARIABLES_WRITE: u32 = 0x02;
10pub const CAP_FILESYSTEM: u32 = 0x04;
11pub const CAP_IO: u32 = 0x08;
12pub const CAP_HOOK_PRE_EXEC: u32 = 0x10;
13pub const CAP_HOOK_POST_EXEC: u32 = 0x20;
14pub const CAP_HOOK_ON_CD: u32 = 0x40;
15pub const CAP_HOOK_PRE_PROMPT: u32 = 0x80;
16
17/// All capability bits OR'd together.
18pub const CAP_ALL: u32 = CAP_VARIABLES_READ
19    | CAP_VARIABLES_WRITE
20    | CAP_FILESYSTEM
21    | CAP_IO
22    | CAP_HOOK_PRE_EXEC
23    | CAP_HOOK_POST_EXEC
24    | CAP_HOOK_ON_CD
25    | CAP_HOOK_PRE_PROMPT;
26
27/// Plugin metadata returned by yosh_plugin_decl().
28#[repr(C)]
29pub struct PluginDecl {
30    pub api_version: u32,
31    pub name: *const c_char,
32    pub version: *const c_char,
33    pub required_capabilities: u32,
34}
35
36// SAFETY: PluginDecl contains raw pointers to static string data only.
37// These are initialized once and never modified, making the struct safe to share.
38unsafe impl Send for PluginDecl {}
39unsafe impl Sync for PluginDecl {}
40
41/// API callbacks yosh provides to plugins.
42///
43/// `ctx` is an opaque pointer to yosh internals. Plugins pass it back to each
44/// callback but must not dereference or store it beyond the current call.
45#[repr(C)]
46pub struct HostApi {
47    pub ctx: *mut c_void,
48
49    // Variable operations
50    pub get_var: unsafe extern "C" fn(ctx: *mut c_void, name: *const c_char) -> *const c_char,
51    pub set_var:
52        unsafe extern "C" fn(ctx: *mut c_void, name: *const c_char, value: *const c_char) -> i32,
53    pub export_var:
54        unsafe extern "C" fn(ctx: *mut c_void, name: *const c_char, value: *const c_char) -> i32,
55
56    // Environment
57    pub get_cwd: unsafe extern "C" fn(ctx: *mut c_void) -> *const c_char,
58    pub set_cwd: unsafe extern "C" fn(ctx: *mut c_void, path: *const c_char) -> i32,
59
60    // Output
61    pub write_stdout:
62        unsafe extern "C" fn(ctx: *mut c_void, data: *const c_char, len: usize) -> i32,
63    pub write_stderr:
64        unsafe extern "C" fn(ctx: *mut c_void, data: *const c_char, len: usize) -> i32,
65}