Skip to content

Commit

Permalink
Plugins: Apply changes for shared types in host.
Browse files Browse the repository at this point in the history
* Make the need adjustments after separating the plugins to different
  packages unifying the implementation for the shared types.
* Generating the shared types doesn't generate not referenced types with
  `bindgen!()` macro. The current workaround is to generate the types
  from parser package inside the shared module.
  • Loading branch information
AmmarAbouZor committed Jan 23, 2025
1 parent 2eac9b0 commit d039c42
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 317 deletions.
150 changes: 87 additions & 63 deletions application/apps/indexer/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions application/apps/indexer/plugins_host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ log.workspace = true
dirs.workspace = true
toml.workspace = true

wasmtime = "28.0"
wasmtime-wasi = "28.0"
wasmtime = "29.0"
wasmtime-wasi = "29.0"

parsers = { path = "../parsers" }
sources = { path = "../sources" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::io;

use crate::PluginGuestInitError;

pub use self::chipmunk::plugin::{bytesource_types, shared_types};
use self::shared_types::{ConfigItem, ConfigSchemaItem, ConfigSchemaType, ConfigValue, Version};
pub use self::chipmunk::bytesource::bytesource_types;

wasmtime::component::bindgen!({
path: "../plugins_api/wit/v_0.1.0/",
world: "bytesource-plugin",
path: "../plugins_api/wit/v0.1.0",
world: "chipmunk:bytesource/bytesource",
async: {
only_imports: [],
},
with: {
"chipmunk:shared/[email protected]": crate::v0_1_0::shared::logging,
"chipmunk:shared/[email protected]": crate::v0_1_0::shared::shared_types,
}
});

impl From<&stypes::PluginByteSourceGeneralSettings> for bytesource_types::SourceConfig {
Expand All @@ -19,7 +20,7 @@ impl From<&stypes::PluginByteSourceGeneralSettings> for bytesource_types::Source
// functionality to log the message from the plugins.
let current_log_level = log::max_level().to_level().unwrap_or(log::Level::Error);

use chipmunk::plugin::logging::Level as PlugLevel;
use crate::v0_1_0::shared::logging::Level as PlugLevel;
let level = match current_log_level {
log::Level::Error => PlugLevel::Error,
log::Level::Warn => PlugLevel::Warn,
Expand All @@ -32,93 +33,15 @@ impl From<&stypes::PluginByteSourceGeneralSettings> for bytesource_types::Source
}
}

impl From<shared_types::InitError> for PluginGuestInitError {
fn from(value: shared_types::InitError) -> Self {
use shared_types::InitError as E;
use PluginGuestInitError as GuestE;
match value {
E::Config(msg) => GuestE::Config(msg),
E::Io(msg) => GuestE::IO(msg),
E::Unsupported(msg) => GuestE::Unsupported(msg),
E::Other(msg) => GuestE::Other(msg),
}
}
}

impl From<bytesource_types::SourceError> for io::Error {
fn from(value: bytesource_types::SourceError) -> Self {
use bytesource_types::SourceError as E;
let msg = match value {
E::Io(msg) => format!("IO Error from bytesoruce plugin. Error: {msg}"),
E::Unsupported => String::from("Unsupported Error from bytesource plugin"),
E::Unsupported(msg) => format!("Unsupported Error from bytesource plugin: {msg}"),
E::Other(msg) => format!("Unknown Error from bytesoruce plugin. Error: {msg}"),
};

io::Error::new(io::ErrorKind::Other, msg)
}
}

use stypes::PluginConfigValue as HostConfValue;
impl From<HostConfValue> for ConfigValue {
fn from(value: HostConfValue) -> Self {
match value {
HostConfValue::Boolean(val) => ConfigValue::Boolean(val),
HostConfValue::Integer(val) => ConfigValue::Integer(val),
HostConfValue::Float(val) => ConfigValue::Float(val),
HostConfValue::Text(val) => ConfigValue::Text(val),
HostConfValue::Files(val) => ConfigValue::Files(
val.into_iter()
.map(|p| p.to_string_lossy().to_string())
.collect(),
),
HostConfValue::Directories(val) => ConfigValue::Directories(
val.into_iter()
.map(|p| p.to_string_lossy().to_string())
.collect(),
),
HostConfValue::Dropdown(val) => ConfigValue::Dropdown(val),
}
}
}

impl From<stypes::PluginConfigItem> for ConfigItem {
fn from(item: stypes::PluginConfigItem) -> Self {
Self {
id: item.id,
value: item.value.into(),
}
}
}

use stypes::PluginConfigSchemaType as HostSchemaType;
use stypes::SemanticVersion;
impl From<ConfigSchemaType> for HostSchemaType {
fn from(value: ConfigSchemaType) -> Self {
match value {
ConfigSchemaType::Boolean => HostSchemaType::Boolean,
ConfigSchemaType::Integer => HostSchemaType::Integer,
ConfigSchemaType::Float => HostSchemaType::Float,
ConfigSchemaType::Text => HostSchemaType::Text,
ConfigSchemaType::Directories => HostSchemaType::Directories,
ConfigSchemaType::Files(exts) => HostSchemaType::Files(exts),
ConfigSchemaType::Dropdown(items) => HostSchemaType::Dropdown(items),
}
}
}

impl From<ConfigSchemaItem> for stypes::PluginConfigSchemaItem {
fn from(item: ConfigSchemaItem) -> Self {
Self {
id: item.id,
title: item.title,
description: item.description,
input_type: item.input_type.into(),
}
}
}

impl From<Version> for SemanticVersion {
fn from(value: Version) -> Self {
Self::new(value.major, value.minor, value.patch)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use wasmtime_wasi::{ResourceTable, WasiCtx, WasiView};

use super::bindings::{
bytesource_types::{self, Level},
chipmunk::plugin::logging,
shared_types,
chipmunk::shared::{logging, shared_types},
};

pub struct ByteSourcePluginState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod bytesource_plugin_state;

use std::io;

use bindings::BytesourcePlugin;
use bindings::Bytesource;
use bytesource_plugin_state::ByteSourcePluginState;
use stypes::{RenderOptions, SemanticVersion};
use wasmtime::{
Expand All @@ -20,7 +20,7 @@ use crate::{

pub struct PluginByteSource {
store: Store<ByteSourcePluginState>,
plugin_bindings: BytesourcePlugin,
plugin_bindings: Bytesource,
}

impl PluginByteSource {
Expand Down Expand Up @@ -51,14 +51,14 @@ impl PluginByteSource {
let mut linker: Linker<ByteSourcePluginState> = Linker::new(engine);
wasmtime_wasi::add_to_linker_async(&mut linker)?;

BytesourcePlugin::add_to_linker(&mut linker, |state| state)?;
Bytesource::add_to_linker(&mut linker, |state| state)?;

let resource_table = ResourceTable::new();

let mut store = Store::new(engine, ByteSourcePluginState::new(ctx, resource_table));

let plugin_bindings =
BytesourcePlugin::instantiate_async(&mut store, &component, &linker).await?;
Bytesource::instantiate_async(&mut store, &component, &linker).await?;

Ok(Self {
store,
Expand All @@ -81,7 +81,7 @@ impl PluginByteSource {

byte_source
.plugin_bindings
.chipmunk_plugin_byte_source()
.chipmunk_bytesource_byte_source()
.call_init(
&mut byte_source.store,
general_config.into(),
Expand All @@ -100,7 +100,7 @@ impl PluginByteSource {
) -> Result<Vec<stypes::PluginConfigSchemaItem>, PluginError> {
let schemas = self
.plugin_bindings
.chipmunk_plugin_byte_source()
.chipmunk_bytesource_byte_source()
.call_get_config_schemas(&mut self.store)
.await?;

Expand All @@ -110,7 +110,7 @@ impl PluginByteSource {
pub async fn plugin_version(&mut self) -> Result<SemanticVersion, PluginError> {
let version = self
.plugin_bindings
.chipmunk_plugin_byte_source()
.chipmunk_bytesource_byte_source()
.call_get_version(&mut self.store)
.await?;

Expand All @@ -120,7 +120,7 @@ impl PluginByteSource {
pub async fn read_next(&mut self, len: usize) -> io::Result<Vec<u8>> {
let bytes_result = self
.plugin_bindings
.chipmunk_plugin_byte_source()
.chipmunk_bytesource_byte_source()
.call_read(&mut self.store, len as u64)
.await
.map_err(|err| {
Expand Down
1 change: 1 addition & 0 deletions application/apps/indexer/plugins_host/src/v0_1_0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bytesource;
pub mod parser;
pub mod shared;
91 changes: 10 additions & 81 deletions application/apps/indexer/plugins_host/src/v0_1_0/parser/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use crate::{parser_shared::COLUMN_SEP, PluginGuestInitError, PluginParseMessage};
use crate::{parser_shared::COLUMN_SEP, PluginParseMessage};

pub use self::chipmunk::plugin::{parse_types::*, shared_types::*};
pub use self::chipmunk::parser::parse_types::*;

use stypes::{ParserRenderOptions, PluginConfigValue as HostConfValue, SemanticVersion};
use stypes::ParserRenderOptions;

wasmtime::component::bindgen!({
path: "../plugins_api/wit/v_0.1.0/",
world: "parse-plugin",
path: "../plugins_api/wit/v0.1.0",
world: "chipmunk:parser/parse",
async: {
only_imports: [],
},
with: {
"chipmunk:shared/[email protected]": crate::v0_1_0::shared::logging,
"chipmunk:shared/[email protected]": crate::v0_1_0::shared::shared_types,
}
});

impl From<&stypes::PluginParserGeneralSettings> for ParserConfig {
Expand All @@ -18,7 +22,7 @@ impl From<&stypes::PluginParserGeneralSettings> for ParserConfig {
// functionality to log the message from the plugins.
let current_log_level = log::max_level().to_level().unwrap_or(log::Level::Error);

use chipmunk::plugin::logging::Level as PlugLevel;
use crate::v0_1_0::shared::logging::Level as PlugLevel;
let level = match current_log_level {
log::Level::Error => PlugLevel::Error,
log::Level::Warn => PlugLevel::Warn,
Expand All @@ -31,18 +35,6 @@ impl From<&stypes::PluginParserGeneralSettings> for ParserConfig {
}
}

impl From<InitError> for PluginGuestInitError {
fn from(value: InitError) -> Self {
use PluginGuestInitError as GuestErr;
match value {
InitError::Config(msg) => GuestErr::Config(msg),
InitError::Io(msg) => GuestErr::IO(msg),
InitError::Unsupported(msg) => GuestErr::Unsupported(msg),
InitError::Other(msg) => GuestErr::Other(msg),
}
}
}

use parsers as p;

impl From<ParseYield> for p::ParseYield<PluginParseMessage> {
Expand Down Expand Up @@ -92,69 +84,6 @@ impl From<ParsedMessage> for PluginParseMessage {
}
}

impl From<HostConfValue> for ConfigValue {
fn from(value: HostConfValue) -> Self {
match value {
HostConfValue::Boolean(val) => ConfigValue::Boolean(val),
HostConfValue::Integer(val) => ConfigValue::Integer(val),
HostConfValue::Float(val) => ConfigValue::Float(val),
HostConfValue::Text(val) => ConfigValue::Text(val),
HostConfValue::Files(val) => ConfigValue::Files(
val.into_iter()
.map(|p| p.to_string_lossy().to_string())
.collect(),
),
HostConfValue::Directories(val) => ConfigValue::Directories(
val.into_iter()
.map(|p| p.to_string_lossy().to_string())
.collect(),
),
HostConfValue::Dropdown(val) => ConfigValue::Dropdown(val),
}
}
}

impl From<stypes::PluginConfigItem> for ConfigItem {
fn from(item: stypes::PluginConfigItem) -> Self {
Self {
id: item.id,
value: item.value.into(),
}
}
}

use stypes::PluginConfigSchemaType as HostSchemaType;
impl From<ConfigSchemaType> for HostSchemaType {
fn from(value: ConfigSchemaType) -> Self {
match value {
ConfigSchemaType::Boolean => HostSchemaType::Boolean,
ConfigSchemaType::Integer => HostSchemaType::Integer,
ConfigSchemaType::Float => HostSchemaType::Float,
ConfigSchemaType::Text => HostSchemaType::Text,
ConfigSchemaType::Directories => HostSchemaType::Directories,
ConfigSchemaType::Files(exts) => HostSchemaType::Files(exts),
ConfigSchemaType::Dropdown(items) => HostSchemaType::Dropdown(items),
}
}
}

impl From<ConfigSchemaItem> for stypes::PluginConfigSchemaItem {
fn from(item: ConfigSchemaItem) -> Self {
Self {
id: item.id,
title: item.title,
description: item.description,
input_type: item.input_type.into(),
}
}
}

impl From<Version> for SemanticVersion {
fn from(value: Version) -> Self {
Self::new(value.major, value.minor, value.patch)
}
}

impl From<RenderOptions> for ParserRenderOptions {
fn from(value: RenderOptions) -> Self {
Self {
Expand Down
Loading

0 comments on commit d039c42

Please sign in to comment.