hammer-plugin-kit 0.8.0

Write plugins for the Hammer editor in Rust: Hammer's imports, typed operations, and the export, action, and text check types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Hammer's plugin kit for Rust. See hammer-editor's docs/CLIENT-PLUGINS.md for the exports a plugin
//! provides (`export`, `action`, `command`, `diagnose`) and the JSON each one is given, which always
//! includes `locale`, the UI's language tag, for replying in the user's language. [export], [action],
//! and [diagnose] have types for theirs.
//!
//! An export is a `#[unsafe(no_mangle)] pub extern "C" fn` returning `i32`, whose body is [run]:
//!
//! ```ignore
//! #[unsafe(no_mangle)]
//! pub extern "C" fn action() -> i32 {
//!     hammer::run(|| {
//!         let request: Request = hammer::input_json()?;
//!         Reply::message(format!("Hello, {}", request.project)).to_json()
//!     })
//! }
//! ```

use serde::Serialize;
use serde::de::DeserializeOwned;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::Once;

pub mod action;
pub mod base64;
pub mod diagnose;
pub mod export;
pub mod http;
pub mod images;
pub mod ops;
pub mod server;
#[cfg(test)]
mod ops_tests;
mod word_classes;
#[cfg(test)]
mod text_tests;

/// The host API version this kit targets; the manifest's `api` must match.
pub const API_VERSION: u32 = 1;

pub const CACHE_MAX_KEY: usize = 1024;
pub const CACHE_MAX_VALUE: usize = 1024 * 1024;

// Extism's kernel keeps the call's input, output, and any memory passed to or from Hammer. Offsets are
// into the kernel's memory, not the module's.
#[link(wasm_import_module = "extism:host/env")]
unsafe extern "C" {
    fn input_length() -> u64;
    fn input_load_u8(index: u64) -> u8;
    fn input_load_u64(index: u64) -> u64;
    fn alloc(length: u64) -> u64;
    fn free(offset: u64);
    fn length(offset: u64) -> u64;
    fn load_u8(offset: u64) -> u8;
    fn load_u64(offset: u64) -> u64;
    fn store_u8(offset: u64, value: u8);
    fn store_u64(offset: u64, value: u64);
    fn output_set(offset: u64, length: u64);
    fn error_set(offset: u64);
    fn log_debug(offset: u64);
    fn log_info(offset: u64);
    fn log_warn(offset: u64);
    fn log_error(offset: u64);
}

#[link(wasm_import_module = "extism:host/user")]
unsafe extern "C" {
    fn hammer_dispatch(request: u64) -> u64;
    fn hammer_cache_get(key: u64) -> u64;
    fn hammer_cache_set(key: u64, value: u64);
    fn hammer_progress(report: u64);
    fn hammer_resource(name: u64) -> u64;
}

fn to_kernel(bytes: &[u8]) -> u64 {
    unsafe {
        let offset = alloc(bytes.len() as u64);
        let mut chunks = bytes.chunks_exact(8);
        let mut at = offset;
        for chunk in &mut chunks {
            store_u64(at, u64::from_le_bytes(chunk.try_into().unwrap()));
            at += 8;
        }
        for &byte in chunks.remainder() {
            store_u8(at, byte);
            at += 1;
        }
        offset
    }
}

fn from_kernel(offset: u64) -> Vec<u8> {
    unsafe {
        let size = length(offset) as usize;
        let mut bytes = Vec::with_capacity(size);
        let mut at = 0;
        while at + 8 <= size {
            bytes.extend_from_slice(&load_u64(offset + at as u64).to_le_bytes());
            at += 8;
        }
        while at < size {
            bytes.push(load_u8(offset + at as u64));
            at += 1;
        }
        bytes
    }
}

/// A failure to report to the user. `?` converts any error into one, and `.into()` a message.
pub type Error = Box<dyn std::error::Error>;

pub type Result<T> = std::result::Result<T, Error>;

/// Runs an export's body: its output on success, its message on failure, and a panic's message too.
pub fn run<O: AsRef<[u8]>>(body: impl FnOnce() -> Result<O>) -> i32 {
    static PANIC_HOOK: Once = Once::new();
    // A panic aborts the call with a trap; the message set here is what the user sees.
    PANIC_HOOK.call_once(|| std::panic::set_hook(Box::new(|info| set_error(&info.to_string()))));
    match body() {
        Ok(output) => {
            let output = output.as_ref();
            unsafe { output_set(to_kernel(output), output.len() as u64) };
            0
        }
        Err(error) => {
            set_error(&error.to_string());
            1
        }
    }
}

fn set_error(message: &str) {
    unsafe { error_set(to_kernel(message.as_bytes())) };
}

pub fn input() -> Vec<u8> {
    unsafe {
        let size = input_length() as usize;
        let mut bytes = Vec::with_capacity(size);
        let mut at = 0;
        while at + 8 <= size {
            bytes.extend_from_slice(&input_load_u64(at as u64).to_le_bytes());
            at += 8;
        }
        while at < size {
            bytes.push(input_load_u8(at as u64));
            at += 1;
        }
        bytes
    }
}

pub fn input_json<T: DeserializeOwned>() -> Result<T> {
    Ok(serde_json::from_slice(&input())?)
}

#[derive(serde::Deserialize)]
struct Reply {
    #[serde(default)]
    output: serde_json::Value,
    error: Option<ReplyError>,
}

#[derive(serde::Deserialize)]
struct ReplyError {
    kind: String,
    #[serde(default)]
    message: String,
}

/// Runs a Hammer operation the manifest has permission for, such as
/// `call::<Tree>("scene.tree", &json!({"project": name}))`. A failure's message starts with its kind:
/// NotFound, InvalidInput, PermissionDenied, or Failed.
pub fn call<T: DeserializeOwned>(operation: &str, input: &impl Serialize) -> Result<T> {
    #[derive(Serialize)]
    struct Request<'a, I> {
        operation: &'a str,
        input: &'a I,
    }
    let request = to_kernel(&serde_json::to_vec(&Request { operation, input })?);
    let reply = unsafe { hammer_dispatch(request) };
    unsafe { free(request) };
    let bytes = from_kernel(reply);
    unsafe { free(reply) };
    let reply: Reply = serde_json::from_slice(&bytes)?;
    match reply.error {
        Some(error) => Err(format!("{}: {}", error.kind, error.message).into()),
        // An output of null is still a success.
        None => Ok(serde_json::from_value(reply.output)?),
    }
}

// The plugin's own cache, kept by Hammer between calls and runs. Hammer may drop any value at any
// time, and clears them all when the plugin is installed again or uninstalled. A call still running on
// the old version during an install may write after the clear, so put a version in keys whose values'
// format may change.

/// The value cached under `key`, or None when there is none.
pub fn cache_get(key: &str) -> Option<Vec<u8>> {
    if key.len() > CACHE_MAX_KEY {
        return None;
    }
    let key = to_kernel(key.as_bytes());
    let value = unsafe { hammer_cache_get(key) };
    unsafe { free(key) };
    if value == 0 {
        return None;
    }
    let bytes = from_kernel(value);
    unsafe { free(value) };
    Some(bytes)
}

/// Caches `value` under `key`; an empty value removes it. False, storing nothing, when either is over
/// its limit.
pub fn cache_set(key: &str, value: &[u8]) -> bool {
    if key.len() > CACHE_MAX_KEY || value.len() > CACHE_MAX_VALUE {
        return false;
    }
    let key = to_kernel(key.as_bytes());
    let value = if value.is_empty() { 0 } else { to_kernel(value) };
    unsafe {
        hammer_cache_set(key, value);
        free(key);
        if value != 0 {
            free(value);
        }
    }
    true
}

/// Reads a file from the package's `resources/` by its path there, such as `words/en.txt`: None when
/// the package has none by that name, or it is empty. Hammer reads it from the package on every call,
/// so keep one you use often.
pub fn resource(name: &str) -> Option<Vec<u8>> {
    let name = to_kernel(name.as_bytes());
    let value = unsafe { hammer_resource(name) };
    unsafe { free(name) };
    if value == 0 {
        return None;
    }
    let bytes = from_kernel(value);
    unsafe { free(value) };
    Some(bytes)
}

/// Reports how far an action has got: `fraction` from 0 to 1 when known, and a line saying what it is
/// doing. Hammer shows it while the action runs. When the user has stopped the run, the call does not
/// return: the action ends there.
pub fn progress(fraction: Option<f64>, message: Option<&str>) {
    #[derive(Serialize)]
    struct Report<'a> {
        #[serde(skip_serializing_if = "Option::is_none")]
        fraction: Option<f64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        message: Option<&'a str>,
    }
    let fraction = fraction.map(|it| it.clamp(0.0, 1.0));
    let Ok(report) = serde_json::to_vec(&Report { fraction, message }) else { return };
    let handle = to_kernel(&report);
    unsafe {
        hammer_progress(handle);
        free(handle);
    }
}

/// The UTF-8 byte offset of each character of `text`, plus one for its end, so a range counted in
/// characters becomes the byte range Hammer expects.
pub fn char_byte_offsets(text: &str) -> Vec<usize> {
    text.char_indices().map(|(at, _)| at).chain(std::iter::once(text.len())).collect()
}

/// A word in a text: `text`, from byte `start` up to `end`, the offsets a [`diagnose::Diagnostic`] takes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Word<'a> {
    pub text: &'a str,
    pub start: usize,
    pub end: usize,
}

/// The words in `text`, in order. A word is a run of letters, with any combining marks on them, so
/// accents, Indic vowel signs, and Arabic vowel marks stay in their word. An apostrophe (`'` or `’`), the
/// Catalan middle dot, Hebrew's geresh and gershayim, ZWNJ, ZWJ, or a soft hyphen between two letters
/// stays inside too, so "don't", "l'arbre", and "col·lecció" are one word each. Digits, hyphens, and
/// everything else end a word.
///
/// Chinese and Japanese put no spaces between words, so each of their characters (Han, kana, and
/// Bopomofo) is a word of its own. Thai, Lao, Khmer, and Burmese don't either, but there each run of
/// letters is one word.
///
/// Letters come from a table of Unicode 17, the same one the Kotlin kit's `words` and the C kit's
/// `hammer_words` use, so all three split text the same way.
pub fn words(text: &str) -> impl Iterator<Item = Word<'_>> {
    let class_at = move |at: usize| text[at..].chars().next().map_or(word_class::OTHER, word_class::of);
    let after = move |at: usize| at + text[at..].chars().next().map_or(0, char::len_utf8);
    let mut at = 0;
    std::iter::from_fn(move || {
        while at < text.len() {
            let start = at;
            let class = class_at(at);
            at = after(at);
            match class {
                word_class::IDEOGRAPH => {
                    while class_at(at) == word_class::MARK {
                        at = after(at);
                    }
                }
                word_class::LETTER => loop {
                    let next = class_at(at);
                    let inside = next == word_class::LETTER
                        || next == word_class::MARK
                        || (next == word_class::JOINER && class_at(after(at)) == word_class::LETTER);
                    if !inside {
                        break;
                    }
                    at = after(at);
                },
                _ => continue,
            }
            return Some(Word { text: &text[start..at], start, end: at });
        }
        None
    })
}

mod word_class {
    pub const OTHER: u8 = 0;
    pub const LETTER: u8 = 1;
    pub const MARK: u8 = 2;
    pub const IDEOGRAPH: u8 = 3;
    pub const JOINER: u8 = 4;

    pub fn of(c: char) -> u8 {
        match c {
            'a'..='z' | 'A'..='Z' => LETTER,
            '\'' => JOINER,
            '\0'..='\x7F' => OTHER,
            _ => {
                let code = c as u32;
                crate::word_classes::WORD_CLASSES
                    .binary_search_by(|&(first, last, _)| {
                        if last < code {
                            std::cmp::Ordering::Less
                        } else if first > code {
                            std::cmp::Ordering::Greater
                        } else {
                            std::cmp::Ordering::Equal
                        }
                    })
                    .map_or(OTHER, |i| crate::word_classes::WORD_CLASSES[i].2)
            }
        }
    }
}

/// An allocator that zeroes memory as it is freed. A module pre-initialized with Wizer keeps only its
/// memory's non-zero bytes, so with this it carries what start-up kept, not what it threw away.
///
/// ```ignore
/// #[global_allocator]
/// static ALLOCATOR: hammer::ZeroingAllocator = hammer::ZeroingAllocator;
/// ```
pub struct ZeroingAllocator;

unsafe impl GlobalAlloc for ZeroingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        unsafe { System.alloc(layout) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        unsafe {
            ptr.write_bytes(0, layout.size());
            System.dealloc(ptr, layout)
        }
    }
}

#[cfg(feature = "getrandom")]
mod random {
    #[link(wasm_import_module = "wasi_snapshot_preview1")]
    unsafe extern "C" {
        fn random_get(buf: *mut u8, len: usize) -> u16;
    }

    /// getrandom's backend, from the one WASI function Hammer offers for it. Needs
    /// `--cfg getrandom_backend="custom"`, which this workspace's `.cargo/config.toml` sets.
    #[unsafe(no_mangle)]
    unsafe extern "Rust" fn __getrandom_v03_custom(dest: *mut u8, len: usize) -> Result<(), getrandom::Error> {
        match unsafe { random_get(dest, len) } {
            0 => Ok(()),
            _ => Err(getrandom::Error::UNSUPPORTED),
        }
    }
}

pub mod log {
    fn send(to: unsafe extern "C" fn(u64), message: &str) {
        let handle = super::to_kernel(message.as_bytes());
        unsafe {
            to(handle);
            super::free(handle);
        }
    }

    pub fn debug(message: &str) {
        send(super::log_debug, message)
    }

    pub fn info(message: &str) {
        send(super::log_info, message)
    }

    pub fn warn(message: &str) {
        send(super::log_warn, message)
    }

    pub fn error(message: &str) {
        send(super::log_error, message)
    }
}