pub mod host;
pub mod rt;
pub mod storage;
pub use jasper_core as core;
pub use rt::PluginError;
pub use serde_json;
#[cfg(target_arch = "wasm32")]
mod rand_shim {
fn no_entropy(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Err(getrandom::Error::UNSUPPORTED)
}
getrandom::register_custom_getrandom!(no_entropy);
}
#[macro_export]
macro_rules! register {
( $($rest:tt)* ) => {
$crate::__register_accum! { hook = (), storage = (), command = (); $($rest)* }
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __register_accum {
( hook = ($($h:path)?), storage = ($($s:ty)?), command = ($($c:path)?); before_save: $f:path $(, $($rest:tt)*)? ) => {
$crate::__register_accum! { hook = ($f), storage = ($($s)?), command = ($($c)?); $($($rest)*)? }
};
( hook = ($($h:path)?), storage = ($($s:ty)?), command = ($($c:path)?); storage: $t:ty $(, $($rest:tt)*)? ) => {
$crate::__register_accum! { hook = ($($h)?), storage = ($t), command = ($($c)?); $($($rest)*)? }
};
( hook = ($($h:path)?), storage = ($($s:ty)?), command = ($($c:path)?); command: $f:path $(, $($rest:tt)*)? ) => {
$crate::__register_accum! { hook = ($($h)?), storage = ($($s)?), command = ($f); $($($rest)*)? }
};
( hook = ($($h:path)?), storage = ($($s:ty)?), command = ($($c:path)?); ) => {
$crate::__register_dispatch! { hook = ($($h)?), storage = ($($s)?), command = ($($c)?) }
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __register_dispatch {
( hook = ($($hook:path)?), storage = ($($st:ty)?), command = ($($cmd:path)?) ) => {
#[allow(dead_code)]
fn __jasper_dispatch(
method: &str,
params: $crate::serde_json::Value,
) -> ::std::result::Result<$crate::serde_json::Value, $crate::PluginError> {
$(
if let Some(r) = $crate::storage::dispatch_storage::<$st>(method, ¶ms) {
return r;
}
)?
match method {
"metadata" => Ok($crate::serde_json::json!({ "ok": true })),
$(
"hook.before_save" => {
let note: $crate::core::model::Note = $crate::serde_json::from_value(
params.get("note").cloned().unwrap_or($crate::serde_json::Value::Null),
)
.map_err(|e| $crate::PluginError::invalid(format!("note 解析失败: {e}")))?;
let hook: fn(
$crate::core::model::Note,
) -> ::std::result::Result<$crate::core::model::Note, String> = $hook;
let out = hook(note).map_err($crate::PluginError::internal)?;
Ok($crate::serde_json::json!({ "note": out }))
}
)?
$(
"command" => {
let id = params
.get("id")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let args = params.get("args").cloned().unwrap_or($crate::serde_json::Value::Null);
let run: fn(
&str,
$crate::serde_json::Value,
) -> ::std::result::Result<$crate::serde_json::Value, $crate::PluginError> = $cmd;
run(&id, args)
}
)?
other => Err($crate::PluginError::unsupported(format!("未知方法: {other}"))),
}
}
#[cfg(target_arch = "wasm32")]
mod __jasper_plugin_abi {
#[no_mangle]
pub extern "C" fn plugin_alloc(size: u32) -> u32 {
$crate::rt::alloc(size as usize) as u32
}
#[no_mangle]
pub extern "C" fn plugin_free(ptr: u32, size: u32) {
$crate::rt::free(ptr as usize, size as usize)
}
#[no_mangle]
pub extern "C" fn plugin_dispatch(ptr: u32, len: u32) -> u64 {
$crate::rt::dispatch(ptr as usize, len as usize, super::__jasper_dispatch)
}
}
};
}
#[cfg(test)]
mod tests {
use crate::core::model::{MarkupLanguage, Note};
use serde_json::json;
fn trim_hook(mut note: Note) -> Result<Note, String> {
note.body = note.body.lines().map(|l| l.trim_end()).collect::<Vec<_>>().join("\n");
Ok(note)
}
crate::register! { before_save: trim_hook }
fn sample_note() -> Note {
Note {
id: "a".repeat(32),
parent_id: String::new(),
title: "t".into(),
body: "x \ny\t".into(),
created_time: 0,
updated_time: 0,
markup_language: MarkupLanguage::Markdown,
is_todo: false,
todo_completed: false,
is_conflict: false,
source_url: String::new(),
order: 0,
}
}
#[test]
fn dispatch_metadata_and_hook() {
let r = __jasper_dispatch("metadata", json!(null)).unwrap();
assert_eq!(r, json!({ "ok": true }));
let r = __jasper_dispatch("hook.before_save", json!({ "note": sample_note() })).unwrap();
assert_eq!(r["note"]["body"], "x\ny");
let e = __jasper_dispatch("nope", json!(null)).unwrap_err();
assert_eq!(e.code, "unsupported");
}
}