diagram plugins

This commit is contained in:
Martin Kavík 2024-11-25 22:13:39 +01:00
parent 1cdb558823
commit e8a0051ea7
29 changed files with 1781 additions and 267 deletions

View file

@ -0,0 +1,10 @@
{
"rust-analyzer.check.overrideCommand": [
"cargo",
"component",
"check",
"--workspace",
"--all-targets",
"--message-format=json"
],
}

View file

@ -0,0 +1,26 @@
[package]
name = "rust_diagram_connector"
version.workspace = true
edition.workspace = true
repository.workspace = true
authors.workspace = true
readme.workspace = true
publish.workspace = true
[dependencies]
wit-bindgen-rt = { version = "0.26.0", features = ["bitflags"] }
[lib]
crate-type = ["cdylib"]
[profile.release]
codegen-units = 1
opt-level = "s"
debug = false
strip = true
lto = true
[package.metadata.component]
package = "component:rust-diagram-connector"
[package.metadata.component.dependencies]

View file

@ -0,0 +1,7 @@
How to create and build the Rust component:
1. `cargo install cargo-component`
2. `cargo component new rust_diagram_connector --lib`
3. `cd rust_diagram_connector`
4. Update code as needed
5. `cargo component build --release --target wasm32-unknown-unknown && cp ../../../target/wasm32-unknown-unknown/release/rust_diagram_connector.wasm .`

View file

@ -0,0 +1,329 @@
// Generated by `wit-bindgen` 0.25.0. DO NOT EDIT!
// Options used:
#[allow(dead_code)]
pub mod component {
#[allow(dead_code)]
pub mod diagram_connector {
#[allow(dead_code, clippy::all)]
pub mod host {
#[used]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
static __FORCE_SECTION_REF: fn() =
super::super::super::__link_custom_section_describing_imports;
use super::super::super::_rt;
#[allow(unused_unsafe, clippy::all)]
pub fn log(message: &str) {
unsafe {
let vec0 = message;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "log"]
fn wit_import(_: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn listen_for_component_text_changes(
diagram_connect_name: &str,
component_id: &str,
) {
unsafe {
let vec0 = diagram_connect_name;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let vec1 = component_id;
let ptr1 = vec1.as_ptr().cast::<u8>();
let len1 = vec1.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "listen-for-component-text-changes"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1.cast_mut(), len1);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn set_component_text(component_id: &str, text: &str) {
unsafe {
let vec0 = component_id;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let vec1 = text;
let ptr1 = vec1.as_ptr().cast::<u8>();
let len1 = vec1.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "set-component-text"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1.cast_mut(), len1);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn address_and_way(time_text: &str) -> Result<(_rt::String, Option<u32>), ()> {
unsafe {
#[repr(align(4))]
struct RetArea([::core::mem::MaybeUninit<u8>; 20]);
let mut ret_area = RetArea([::core::mem::MaybeUninit::uninit(); 20]);
let vec0 = time_text;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "address-and-way"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1);
let l2 = i32::from(*ptr1.add(0).cast::<u8>());
match l2 {
0 => {
let e = {
let l3 = *ptr1.add(4).cast::<*mut u8>();
let l4 = *ptr1.add(8).cast::<usize>();
let len5 = l4;
let bytes5 = _rt::Vec::from_raw_parts(l3.cast(), len5, len5);
let l6 = i32::from(*ptr1.add(12).cast::<u8>());
(
_rt::string_lift(bytes5),
match l6 {
0 => None,
1 => {
let e = {
let l7 = *ptr1.add(16).cast::<i32>();
l7 as u32
};
Some(e)
}
_ => _rt::invalid_enum_discriminant(),
},
)
};
Ok(e)
}
1 => {
let e = ();
Err(e)
}
_ => _rt::invalid_enum_discriminant(),
}
}
}
}
}
}
#[allow(dead_code)]
pub mod exports {
#[allow(dead_code)]
pub mod component {
#[allow(dead_code)]
pub mod diagram_connector {
#[allow(dead_code, clippy::all)]
pub mod diagram_connector {
#[used]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
static __FORCE_SECTION_REF: fn() =
super::super::super::super::__link_custom_section_describing_imports;
use super::super::super::super::_rt;
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_init_cabi<T: Guest>() {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
T::init();
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_name_cabi<T: Guest>() -> *mut u8 {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
let result0 = T::name();
let ptr1 = _RET_AREA.0.as_mut_ptr().cast::<u8>();
let vec2 = (result0.into_bytes()).into_boxed_slice();
let ptr2 = vec2.as_ptr().cast::<u8>();
let len2 = vec2.len();
::core::mem::forget(vec2);
*ptr1.add(4).cast::<usize>() = len2;
*ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut();
ptr1
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn __post_return_name<T: Guest>(arg0: *mut u8) {
let l0 = *arg0.add(0).cast::<*mut u8>();
let l1 = *arg0.add(4).cast::<usize>();
_rt::cabi_dealloc(l0, l1, 1);
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_on_component_text_changed_cabi<T: Guest>(
arg0: *mut u8,
arg1: usize,
arg2: *mut u8,
arg3: usize,
) {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
let len0 = arg1;
let bytes0 = _rt::Vec::from_raw_parts(arg0.cast(), len0, len0);
let len1 = arg3;
let bytes1 = _rt::Vec::from_raw_parts(arg2.cast(), len1, len1);
T::on_component_text_changed(
_rt::string_lift(bytes0),
_rt::string_lift(bytes1),
);
}
pub trait Guest {
fn init();
fn name() -> _rt::String;
fn on_component_text_changed(component_id: _rt::String, text: _rt::String);
}
#[doc(hidden)]
macro_rules! __export_component_diagram_connector_diagram_connector_cabi{
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
#[export_name = "component:diagram-connector/diagram-connector#init"]
unsafe extern "C" fn export_init() {
$($path_to_types)*::_export_init_cabi::<$ty>()
}
#[export_name = "component:diagram-connector/diagram-connector#name"]
unsafe extern "C" fn export_name() -> *mut u8 {
$($path_to_types)*::_export_name_cabi::<$ty>()
}
#[export_name = "cabi_post_component:diagram-connector/diagram-connector#name"]
unsafe extern "C" fn _post_return_name(arg0: *mut u8,) {
$($path_to_types)*::__post_return_name::<$ty>(arg0)
}
#[export_name = "component:diagram-connector/diagram-connector#on-component-text-changed"]
unsafe extern "C" fn export_on_component_text_changed(arg0: *mut u8,arg1: usize,arg2: *mut u8,arg3: usize,) {
$($path_to_types)*::_export_on_component_text_changed_cabi::<$ty>(arg0, arg1, arg2, arg3)
}
};);
}
#[doc(hidden)]
pub(crate) use __export_component_diagram_connector_diagram_connector_cabi;
#[repr(align(4))]
struct _RetArea([::core::mem::MaybeUninit<u8>; 8]);
static mut _RET_AREA: _RetArea = _RetArea([::core::mem::MaybeUninit::uninit(); 8]);
}
}
}
}
mod _rt {
pub use alloc_crate::string::String;
pub use alloc_crate::vec::Vec;
pub unsafe fn string_lift(bytes: Vec<u8>) -> String {
if cfg!(debug_assertions) {
String::from_utf8(bytes).unwrap()
} else {
String::from_utf8_unchecked(bytes)
}
}
pub unsafe fn invalid_enum_discriminant<T>() -> T {
if cfg!(debug_assertions) {
panic!("invalid enum discriminant")
} else {
core::hint::unreachable_unchecked()
}
}
#[cfg(target_arch = "wasm32")]
pub fn run_ctors_once() {
wit_bindgen_rt::run_ctors_once();
}
pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
if size == 0 {
return;
}
let layout = alloc::Layout::from_size_align_unchecked(size, align);
alloc::dealloc(ptr as *mut u8, layout);
}
extern crate alloc as alloc_crate;
pub use alloc_crate::alloc;
}
/// Generates `#[no_mangle]` functions to export the specified type as the
/// root implementation of all generated traits.
///
/// For more information see the documentation of `wit_bindgen::generate!`.
///
/// ```rust
/// # macro_rules! export{ ($($t:tt)*) => (); }
/// # trait Guest {}
/// struct MyType;
///
/// impl Guest for MyType {
/// // ...
/// }
///
/// export!(MyType);
/// ```
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! __export_component_impl {
($ty:ident) => (self::export!($ty with_types_in self););
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
$($path_to_types_root)*::exports::component::diagram_connector::diagram_connector::__export_component_diagram_connector_diagram_connector_cabi!($ty with_types_in $($path_to_types_root)*::exports::component::diagram_connector::diagram_connector);
)
}
#[doc(inline)]
pub(crate) use __export_component_impl as export;
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.25.0:component:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 550] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xa6\x03\x01A\x02\x01\
A\x04\x01B\x0b\x01@\x01\x07messages\x01\0\x04\0\x03log\x01\0\x01@\x02\x14diagram\
-connect-names\x0ccomponent-ids\x01\0\x04\0!listen-for-component-text-changes\x01\
\x01\x01@\x02\x0ccomponent-ids\x04texts\x01\0\x04\0\x12set-component-text\x01\x02\
\x01ky\x01o\x02s\x03\x01j\x01\x04\0\x01@\x01\x09time-texts\0\x05\x04\0\x0faddres\
s-and-way\x01\x06\x03\x01\x20component:diagram-connector/host\x05\0\x01B\x06\x01\
@\0\x01\0\x04\0\x04init\x01\0\x01@\0\0s\x04\0\x04name\x01\x01\x01@\x02\x0ccompon\
ent-ids\x04texts\x01\0\x04\0\x19on-component-text-changed\x01\x02\x04\x01-compon\
ent:diagram-connector/diagram-connector\x05\x01\x04\x01%component:diagram-connec\
tor/component\x04\0\x0b\x0f\x01\0\x09component\x03\0\0\0G\x09producers\x01\x0cpr\
ocessed-by\x02\x0dwit-component\x070.208.1\x10wit-bindgen-rust\x060.25.0";
#[inline(never)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub fn __link_custom_section_describing_imports() {
wit_bindgen_rt::maybe_link_cabi_realloc();
}

View file

@ -0,0 +1,63 @@
use std::borrow::Cow;
use std::cell::RefCell;
#[allow(warnings)]
mod bindings;
use bindings::component::diagram_connector::host;
use bindings::exports::component::diagram_connector::diagram_connector;
macro_rules! log {
($($arg:tt)*) => (host::log(&format!($($arg)*)))
}
static NAME: &str = "Rust Test Diagram Connector";
// Note: Ids from `test_files/cache_diagram.excalidraw`
const ADDRESS_COMPONENT_ID: &str = "ITxhJ7NtZ74YFd9JQ0_pl";
const TIME_COMPONENT_ID: &str = "afXu8_6Kqfq-q2IsjtAcP";
const STATUS_COMPONENT_ID: &str = "0iH5yRbH4IEseV3mnof3A";
thread_local! {
static TIME_TEXT: RefCell<String> = <_>::default();
}
struct Component;
impl diagram_connector::Guest for Component {
fn init() {
host::listen_for_component_text_changes(NAME, TIME_COMPONENT_ID);
log!("'{NAME}' initialized")
}
fn name() -> String {
NAME.to_string()
}
fn on_component_text_changed(component_id: String, text: String) {
match component_id.as_str() {
TIME_COMPONENT_ID => {
TIME_TEXT.set(text);
refresh_fields();
}
_ => (),
}
}
}
fn refresh_fields() {
let way = TIME_TEXT.with_borrow(|time_text| host::address_and_way(&time_text));
let Ok((address, way)) = way else {
return;
};
host::set_component_text(ADDRESS_COMPONENT_ID, &address);
let status_text: Cow<str> = if let Some(way) = way {
format!("VALID, WAY: {way}").into()
} else {
"NOT VALID".into()
};
host::set_component_text(STATUS_COMPONENT_ID, &status_text);
}
bindings::export!(Component with_types_in bindings);

View file

@ -0,0 +1,19 @@
package component:diagram-connector;
interface host {
log: func(message: string);
listen-for-component-text-changes: func(diagram-connect-name: string, component-id: string);
set-component-text: func(component-id: string, text: string);
address-and-way: func(time-text: string) -> result<tuple<string, option<u32>>>;
}
interface diagram-connector {
init: func();
name: func() -> string;
on-component-text-changed: func(component-id: string, text: string);
}
world component {
import host;
export diagram-connector;
}