Skip to content

Commit 1b3af10

Browse files
committed
Plugins: Resolve TODOs & Refactoring & Documentation.
* Plugin api: Remove checks for building with all features since they are not conflicting anymore. * Remove all code for forcing parser plugin via environment variables since it's not needed anymore for parser plugins. * Resolve smaller todos in plugins host and api. * Documentation and comments about plugins in Rust code.
1 parent 3dc3202 commit 1b3af10

File tree

35 files changed

+175
-200
lines changed

35 files changed

+175
-200
lines changed

application/apps/indexer/indexer_cli/src/interactive.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,22 @@ pub(crate) async fn handle_interactive_session(input: Option<PathBuf>) {
9999
println!("plugin command received");
100100
const PLUGIN_PATH_ENV: &str = "WASM_PLUGIN_PATH";
101101

102-
//TODO AAZ: Find a better way to deliver plugin path than environment variables
102+
// Delivering the plugins info via env variables is good enough since this
103+
// tool is going to be deprecated soon.
103104
let plugin_path = match std::env::var(PLUGIN_PATH_ENV) {
104105
Ok(path) => path,
105-
Err(err) => panic!("Retrieving plugin path environment variable failed. Err {err}") ,
106+
Err(err) => panic!("Retrieving plugin path environment variable failed.\n\
107+
Please set the plugin path via the environment variable {PLUGIN_PATH_ENV}.\n\
108+
Err {err}") ,
106109
};
107110
start = Instant::now();
108111
let uuid = Uuid::new_v4();
109112
let file_path = input.clone().expect("input must be present");
110113
let proto_plugin_path = PathBuf::from(plugin_path);
111114

112-
//TODO AAZ: plugins configuration aren't delivered here.
115+
//NOTE: plugins configuration aren't delivered here.
113116
let plugin_configs = Vec::new();
114-
let plugin_parser_settings = stypes::PluginParserSettings::prototyping(proto_plugin_path, plugin_configs);
117+
let plugin_parser_settings = stypes::PluginParserSettings::new(proto_plugin_path, Default::default() ,plugin_configs);
115118
session.observe(uuid, stypes::ObserveOptions::file(file_path, stypes::FileFormat::Binary, stypes::ParserType::Plugin(plugin_parser_settings))).expect("observe failed");
116119
}
117120
Some(Command::Grab) => {

application/apps/indexer/plugins_api/build.rs

-13
This file was deleted.

application/apps/indexer/plugins_api/src/bytesource/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ macro_rules! bytesource_export {
180180
// SAFETY: Bytesource host implements read trait, which takes a mutable reference
181181
// to self when called. Therefor it's not possible to have multiple references on
182182
// the static bytesource instance here at once.
183-
//TODO AAZ: Find better way than denying the warning.
183+
//TODO AAZ: Measure the impact on this unsafe function and provide explanation for
184+
// suppressing the warning here.
184185
#[allow(static_mut_refs)]
185186
let source =
186187
unsafe { BYTESOURCE.as_mut().expect("Bytesource already initialized") };

application/apps/indexer/plugins_api/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! The provided types and functions match the definitions from the `WIT` files defined in Chipmunk
1111
//! repository.
1212
//!
13-
//! TODO: This is basic documentation that need a lot of improvements and examples
13+
//! TODO AAZ: This is basic documentation that need a lot of improvements and examples
1414
//!
1515
1616
mod shared;
@@ -36,10 +36,6 @@ pub use shared::{
3636
plugin_logger::{LogSend as __LogSend, PluginLogger as __PluginLogger},
3737
};
3838

39-
//TODO AAZ: Check if we can remove the features, after moving shared types.
40-
// NOTE: Calling generate! Macro multiple time on the same crate causes compilation errors with `cargo
41-
// component` in release mode.
42-
4339
#[cfg(feature = "bytesource")]
4440
#[cfg_attr(docsrs, doc(cfg(feature = "bytesource")))]
4541
pub mod bytesource;

application/apps/indexer/plugins_api/src/parser/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ macro_rules! parser_export {
262262
use $crate::parser::Parser;
263263
// SAFETY: Parse method has mutable reference to self and can't be called more than
264264
// once on the same time on host
265-
//TODO AAZ: Find better way than denying the warning.
265+
//TODO AAZ: Measure the impact on this unsafe function and provide explanation for
266+
// suppressing the warning here.
266267
#[allow(static_mut_refs)]
267268
let parser = unsafe { PARSER.as_mut().expect("parser already initialized") };
268269
parser.parse(&data, timestamp).map(|items| items.collect())

application/apps/indexer/plugins_host/benches/plugin_parser_init.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ fn plugin_parser_init(c: &mut Criterion) {
2424
//TODO AAZ: Deliver plugin configurations for benchmarks
2525
let plugin_configs = Vec::new();
2626

27-
let settings = black_box(stypes::PluginParserSettings::prototyping(
27+
let settings = black_box(stypes::PluginParserSettings::new(
2828
plugin_file,
29+
stypes::PluginParserGeneralSettings::default(),
2930
plugin_configs,
3031
));
3132

application/apps/indexer/plugins_host/src/bytesource_shared/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
PluginHostInitError, WasmPlugin,
1212
};
1313

14+
/// Interface name for the byte-source plugin with the package name as defined in WIT file.
1415
const BYTESOURCE_INTERFACE_NAME: &str = "chipmunk:bytesource/byte-source";
1516

1617
/// The maximum number of consecutive returns with empty bytes allowed from a plugin.
@@ -139,6 +140,7 @@ impl PluginsByteSource {
139140
}
140141
}
141142

143+
/// Calls the function read on the plugin with provided length return the read byte.
142144
async fn read_next(&mut self, len: usize) -> io::Result<Vec<u8>> {
143145
let res = match &mut self.source {
144146
PlugVerByteSource::Ver010(source) => source.read_next(len).await,
@@ -183,6 +185,7 @@ impl WasmPlugin for PluginsByteSource {
183185
}
184186
}
185187

188+
// PluginsByteSource implements Read trait so we can use it with `BinaryByteSource`
186189
impl Read for PluginsByteSource {
187190
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
188191
let len = buf.len();

application/apps/indexer/plugins_host/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Library contains implementation for plugins system in Chipmunk using WASM with
2+
//! the component model approach.
3+
//! The used runtime for the plugin is [wasmtime](https://docs.rs/wasmtime/latest/wasmtime/)
4+
15
mod bytesource_shared;
26
mod parser_shared;
37
pub mod plugins_manager;
@@ -14,7 +18,7 @@ pub use bytesource_shared::PluginsByteSource;
1418
pub use plugins_shared::plugin_errors::{PluginGuestInitError, PluginHostInitError};
1519
use stypes::{PluginType, SemanticVersion};
1620

17-
// Trait is used with Chipmunk only.
21+
/// Provided needed method and definitions for all WASM plugins in Chipmunk.
1822
#[allow(async_fn_in_trait)]
1923
pub trait WasmPlugin {
2024
/// Provides the Type of the plugin.

application/apps/indexer/plugins_host/src/parser_shared/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::{Path, PathBuf};
22

3-
use stypes::{ParserRenderOptions, SemanticVersion, ValidPluginInfo};
3+
use stypes::{SemanticVersion, ValidPluginInfo};
44
use wasmtime::component::Component;
55

66
use crate::{
@@ -10,6 +10,7 @@ use crate::{
1010

1111
pub mod plugin_parse_message;
1212

13+
/// Interface name for the parser plugin with the package name as defined in WIT file.
1314
const PARSER_INTERFACE_NAME: &str = "chipmunk:parser/parser";
1415

1516
/// Marker for a column separator in the output string.
@@ -139,12 +140,6 @@ impl PluginsParser {
139140
))),
140141
}
141142
}
142-
143-
pub async fn get_render_options(&mut self) -> Result<ParserRenderOptions, PluginError> {
144-
match &mut self.parser {
145-
PlugVerParser::Ver010(parser) => parser.get_render_options().await,
146-
}
147-
}
148143
}
149144

150145
impl WasmPlugin for PluginsParser {

application/apps/indexer/plugins_host/src/parser_shared/plugin_parse_message.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,13 @@ use std::fmt::Display;
33
use parsers::LogMessage;
44
use serde::Serialize;
55

6+
/// Represent the message of the parsed item returned by plugins.
67
#[derive(Debug, Serialize)]
78
pub struct PluginParseMessage {
9+
/// The content of the message as string.
810
pub content: String,
911
}
1012

11-
impl PluginParseMessage {
12-
pub fn new(content: String) -> Self {
13-
Self { content }
14-
}
15-
}
16-
17-
impl From<String> for PluginParseMessage {
18-
fn from(content: String) -> Self {
19-
Self::new(content)
20-
}
21-
}
22-
23-
impl From<PluginParseMessage> for String {
24-
fn from(value: PluginParseMessage) -> Self {
25-
value.content
26-
}
27-
}
28-
2913
impl Display for PluginParseMessage {
3014
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3115
write!(f, "{}", self.content)
@@ -37,6 +21,7 @@ impl LogMessage for PluginParseMessage {
3721
&self,
3822
writer: &mut W,
3923
) -> Result<usize, std::io::Error> {
24+
//TODO AAZ: Make sure plugins messages should support export as binary.
4025
let bytes = self.content.as_bytes();
4126
let len = bytes.len();
4227
writer.write_all(bytes)?;

application/apps/indexer/plugins_host/src/plugins_manager/errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ use thiserror::Error;
55

66
use crate::wasm_host::WasmHostInitError;
77

8+
/// Plugins manager initialization Error.
89
#[derive(Debug, Error)]
910
pub enum InitError {
11+
/// Errors while initializing wasm host for plugins.
1012
#[error("Initialization of WASM host failed. {0}")]
1113
WasmHost(#[from] WasmHostInitError),
14+
/// Errors from WASM runtime.
1215
#[error(transparent)]
1316
WasmRunTimeError(#[from] anyhow::Error),
17+
/// IO Errors.
1418
#[error("IO Error. {0}")]
1519
IO(#[from] io::Error),
20+
/// Errors not included in the other error types.
1621
#[error("Error during initialization. {0}")]
1722
Other(String),
1823
}

application/apps/indexer/plugins_host/src/plugins_manager/load/mod.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
mod paths;
2-
#[cfg(test)]
3-
mod tests;
42

53
use std::{
64
fs::{self, read_to_string},
@@ -63,9 +61,13 @@ fn get_dirs(dir_path: &PathBuf) -> Result<impl Iterator<Item = PathBuf>, io::Err
6361
Ok(dirs)
6462
}
6563

64+
/// Loads parser infos and metadata from the provided parser directory.
6665
async fn load_parser(dir: PathBuf) -> Result<PluginEntity, InitError> {
6766
let (wasm_file, metadata_file) = match validate_plugin_files(&dir)? {
68-
PluginValidationState::Valid { wasm, metadata } => (wasm, metadata),
67+
PluginValidationState::Valid {
68+
wasm_path: wasm,
69+
metadata,
70+
} => (wasm, metadata),
6971
PluginValidationState::Invalid { err_msg } => {
7072
let invalid_entity = PluginEntity {
7173
dir_path: dir,
@@ -127,17 +129,25 @@ async fn load_parser(dir: PathBuf) -> Result<PluginEntity, InitError> {
127129
Ok(valid_plugin)
128130
}
129131

132+
/// Represents Plugins State and the corresponding infos.
130133
#[derive(Debug, Clone)]
131134
enum PluginValidationState {
135+
/// Represents valid plugin with its infos and metadata.
132136
Valid {
133-
wasm: PathBuf,
137+
/// The path for the plugin wasm file.
138+
wasm_path: PathBuf,
139+
/// Metadata of the plugins found in plugins metadata toml file.
134140
metadata: Option<PathBuf>,
135141
},
142+
/// Represents an invalid plugin with infos about validation error.
136143
Invalid {
144+
/// Error message explaining why the plugin is invalid.
137145
err_msg: String,
138146
},
139147
}
140148

149+
/// Loads the files inside plugin directory and validate their content return the state
150+
/// of the plugin.
141151
fn validate_plugin_files(dir: &PathBuf) -> Result<PluginValidationState, InitError> {
142152
use PluginValidationState as Re;
143153
let mut wasm_file = None;
@@ -172,7 +182,7 @@ fn validate_plugin_files(dir: &PathBuf) -> Result<PluginValidationState, InitErr
172182

173183
let res = match wasm_file {
174184
Some(wasm) => Re::Valid {
175-
wasm,
185+
wasm_path: wasm,
176186
metadata: metadata_file,
177187
},
178188
None => {
@@ -185,13 +195,15 @@ fn validate_plugin_files(dir: &PathBuf) -> Result<PluginValidationState, InitErr
185195
Ok(res)
186196
}
187197

198+
/// Parser the plugin metadata from the provided toml file.
188199
fn parse_metadata(file: &PathBuf) -> Result<PluginMetadata, String> {
189200
let content = read_to_string(file)
190201
.map_err(|err| format!("Reading metadata file fail. Error {err:#?}"))?;
191202

192203
toml::from_str(&content).map_err(|err| format!("Parsing metadata file fail. Error {err:#?}"))
193204
}
194205

206+
/// Loads all byte-source plugins from their main directory.
195207
async fn load_all_bytesources() -> Result<Vec<PluginEntity>, InitError> {
196208
let mut bytesources = Vec::new();
197209

@@ -211,9 +223,13 @@ async fn load_all_bytesources() -> Result<Vec<PluginEntity>, InitError> {
211223
Ok(bytesources)
212224
}
213225

226+
/// Loads byte-source infos and metadata from the provided parser directory.
214227
async fn load_bytesource(dir: PathBuf) -> Result<PluginEntity, InitError> {
215228
let (wasm_file, metadata_file) = match validate_plugin_files(&dir)? {
216-
PluginValidationState::Valid { wasm, metadata } => (wasm, metadata),
229+
PluginValidationState::Valid {
230+
wasm_path: wasm,
231+
metadata,
232+
} => (wasm, metadata),
217233
PluginValidationState::Invalid { err_msg } => {
218234
let invalid_entity = PluginEntity {
219235
dir_path: dir,

application/apps/indexer/plugins_host/src/plugins_manager/load/paths.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
//TODO: Remove after prototyping
2-
#![allow(unused)]
31
use std::path::PathBuf;
42

53
use crate::plugins_manager::InitError;
64

5+
/// The name of the plugins directory in Chipmunk home directory.
76
const PLUGINS_DIR: &str = "plugins";
7+
8+
/// The name of the parser plugins directory in plugins directory.
89
const PARSER_DIR: &str = "parsers";
10+
11+
/// The name of the byte-source plugins directory in plugins directory.
912
const BYTESOURCE_DIR: &str = "bytesources";
1013

1114
/// Copied from `session/src/paths.rs`
@@ -17,14 +20,17 @@ pub fn get_home_dir() -> Result<PathBuf, InitError> {
1720
.ok_or_else(|| InitError::Other(String::from("Failed to find home directory")))
1821
}
1922

23+
/// Return plugins directory in Chipmunk home directory.
2024
pub fn plugins_dir() -> Result<PathBuf, InitError> {
2125
get_home_dir().map(|home| home.join(PLUGINS_DIR))
2226
}
2327

28+
/// Returns parser plugins directory within Chipmunk home directory.
2429
pub fn parser_dir() -> Result<PathBuf, InitError> {
2530
plugins_dir().map(|plugins| plugins.join(PARSER_DIR))
2631
}
2732

33+
/// Returns byte-source plugins directory within Chipmunk home directory.
2834
pub fn bytesource_dir() -> Result<PathBuf, InitError> {
2935
plugins_dir().map(|plugins| plugins.join(BYTESOURCE_DIR))
3036
}

application/apps/indexer/plugins_host/src/plugins_manager/load/tests.rs

-12
This file was deleted.

0 commit comments

Comments
 (0)