Skip to content

Commit ddde66e

Browse files
committed
Plugins Parser: Adjustment according to master
* Change parse return type from collection of results to a result of collection and make the needed adjustments in wit files and macros. * Adjustments on the unit tests for parser macro accordingly.
1 parent 54c06a5 commit ddde66e

File tree

7 files changed

+80
-76
lines changed

7 files changed

+80
-76
lines changed

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

+17-13
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub trait Parser {
6969
&mut self,
7070
data: &[u8],
7171
timestamp: Option<u64>,
72-
) -> impl IntoIterator<Item = Result<ParseReturn, ParseError>> + Send;
72+
) -> Result<impl Iterator<Item = ParseReturn>, ParseError>;
7373
}
7474

7575
#[macro_export]
@@ -102,8 +102,8 @@ pub trait Parser {
102102
/// # &mut self,
103103
/// # _data: &[u8],
104104
/// # _timestamp: Option<u64>,
105-
/// # ) -> impl IntoIterator<Item = Result<ParseReturn, ParseError>> + Send {
106-
/// # Vec::new()
105+
/// # ) -> Result<impl Iterator<Item = ParseReturn>, ParseError> {
106+
/// # Ok(std::iter::empty())
107107
/// # }
108108
/// }
109109
///
@@ -157,25 +157,29 @@ macro_rules! parser_export {
157157
fn parse(
158158
data: ::std::vec::Vec<u8>,
159159
timestamp: ::std::option::Option<u64>,
160-
) -> ::std::vec::Vec<
161-
::std::result::Result<$crate::parser::ParseReturn, $crate::parser::ParseError>,
160+
) -> ::std::result::Result<
161+
::std::vec::Vec<$crate::parser::ParseReturn>,
162+
$crate::parser::ParseError,
162163
> {
163164
// SAFETY: Parse method has mutable reference to self and can't be called more than
164165
// once on the same time on host
165166
let parser = unsafe { PARSER.as_mut().expect("parser already initialized") };
166-
parser.parse(&data, timestamp).into_iter().collect()
167+
parser.parse(&data, timestamp).map(|items| items.collect())
167168
}
168169

169170
/// Parse the given bytes returning the results to the host one by one using the function `add` provided by the host.
170-
fn parse_with_add(data: ::std::vec::Vec<u8>, timestamp: ::std::option::Option<u64>) {
171+
fn parse_with_add(
172+
data: ::std::vec::Vec<u8>,
173+
timestamp: ::std::option::Option<u64>,
174+
) -> ::std::result::Result<(), $crate::parser::ParseError> {
171175
// SAFETY: Parse method has mutable reference to self and can't be called more than
172176
// once on the same time on host
173177
let parser = unsafe { PARSER.as_mut().expect("parser already initialized") };
174-
for item in parser.parse(&data, timestamp) {
175-
$crate::parser::__internal_bindings::chipmunk::plugin::host_add::add(
176-
item.as_ref(),
177-
);
178+
for item in parser.parse(&data, timestamp)? {
179+
$crate::parser::__internal_bindings::chipmunk::plugin::host_add::add(&item);
178180
}
181+
182+
Ok(())
179183
}
180184
}
181185

@@ -208,8 +212,8 @@ mod prototyping {
208212
&mut self,
209213
_data: &[u8],
210214
_timestamp: Option<u64>,
211-
) -> impl IntoIterator<Item = Result<ParseReturn, ParseError>> + Send {
212-
Vec::new()
215+
) -> Result<impl Iterator<Item = ParseReturn>, ParseError> {
216+
Ok(std::iter::empty())
213217
}
214218
}
215219

application/apps/indexer/plugins_api/tests/parser_macro/extend_trait_pass.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ impl crate::parser::Parser for Dummy {
2020
&mut self,
2121
_data: &[u8],
2222
_timestamp: Option<u64>,
23-
) -> impl IntoIterator<Item = Result<crate::parser::ParseReturn, crate::parser::ParseError>> + Send
24-
{
25-
Vec::new()
23+
) -> Result<impl Iterator<Item = crate::parser::ParseReturn>, crate::parser::ParseError> {
24+
Ok(std::iter::empty())
2625
}
2726
}
2827

application/apps/indexer/plugins_api/tests/parser_macro/imp_parser_diff_mod_pass.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ mod impl_mod {
2121
&mut self,
2222
_data: &[u8],
2323
_timestamp: Option<u64>,
24-
) -> impl IntoIterator<Item = Result<crate::parser::ParseReturn, crate::parser::ParseError>> + Send
24+
) -> Result<impl Iterator<Item = crate::parser::ParseReturn>, crate::parser::ParseError>
2525
{
26-
Vec::new()
26+
Ok(std::iter::empty())
2727
}
2828
}
2929
}

application/apps/indexer/plugins_api/tests/parser_macro/imp_parser_pass.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ impl crate::parser::Parser for Dummy {
1818
&mut self,
1919
_data: &[u8],
2020
_timestamp: Option<u64>,
21-
) -> impl IntoIterator<Item = Result<crate::parser::ParseReturn, crate::parser::ParseError>> + Send
22-
{
23-
Vec::new()
21+
) -> Result<impl Iterator<Item = crate::parser::ParseReturn>, crate::parser::ParseError> {
22+
Ok(std::iter::empty())
2423
}
2524
}
2625

application/apps/indexer/plugins_api/wit/v_0.1.0/parser.wit

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ interface parser {
5050
/// Initialize the parser with the given configurations
5151
init: func(general-configs: parser-config, plugin-configs: option<string>) -> result<_, init-error>;
5252

53-
/// Parse the given bytes returning a list of plugins results
54-
parse: func(data: list<u8>, timestamp: option<u64>) -> list<result<parse-return, parse-error>>;
53+
/// Parse the given bytes returning a list of parsed items,
54+
/// or parse error if an error occurred and no item has been parsed.
55+
parse: func(data: list<u8>, timestamp: option<u64>) -> result<list<parse-return>, parse-error>;
5556

5657
/// Parse the given bytes returning the results to the host one by one using the function `add` provided by the host.
57-
parse-with-add: func(data: list<u8>, timestamp: option<u64>);
58+
/// Otherwise it will return a parsing error only if
59+
parse-with-add: func(data: list<u8>, timestamp: option<u64>) -> result<_, parse-error>;
5860
}
5961

6062
/// Provides methods to add parse items directly by the host
6163
interface host-add {
6264
use parse-types.{parse-return, parse-error};
6365

64-
/// Add parse results one by one directly at the host memory
65-
add: func(item: result<parse-return, parse-error>);
66+
/// Add parsed item one by one directly at the host memory
67+
add: func(item: parse-return);
6668
}
6769

6870

application/apps/indexer/plugins_host/src/v0_1_0/parser/mod.rs

+47-47
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use wasmtime::{
1111
use wasmtime_wasi::ResourceTable;
1212

1313
use crate::{
14-
plugins_shared::get_wasi_ctx_builder, v0_1_0::parser::bindings::ParseError,
15-
wasm_host::get_wasm_host, PluginGuestInitError, PluginHostInitError, PluginParseMessage,
16-
PluginType, WasmPlugin,
14+
plugins_shared::get_wasi_ctx_builder, wasm_host::get_wasm_host, PluginGuestInitError,
15+
PluginHostInitError, PluginParseMessage, PluginType, WasmPlugin,
1716
};
1817

1918
use self::{
@@ -81,8 +80,8 @@ impl PluginParser {
8180
&mut self,
8281
input: &[u8],
8382
timestamp: Option<u64>,
84-
) -> impl IntoIterator<Item = Result<(usize, Option<p::ParseYield<PluginParseMessage>>), p::Error>>
85-
+ Send {
83+
) -> Result<impl Iterator<Item = (usize, Option<p::ParseYield<PluginParseMessage>>)>, p::Error>
84+
{
8685
let call_res =
8786
futures::executor::block_on(self.plugin_bindings.chipmunk_plugin_parser().call_parse(
8887
&mut self.store,
@@ -91,15 +90,16 @@ impl PluginParser {
9190
));
9291

9392
let parse_results = match call_res {
94-
Ok(results) => results,
93+
Ok(results) => results?,
9594
Err(call_err) => {
96-
vec![Err(ParseError::Unrecoverable(format!(
95+
return Err(p::Error::Unrecoverable(format!(
9796
"Call parse on the plugin failed. Error: {call_err}"
98-
)))]
97+
)))
9998
}
10099
};
101100

102-
parse_results.into_iter().map(guest_to_host_parse_results)
101+
let res = parse_results.into_iter().map(guest_to_host_parse_results);
102+
Ok(res)
103103
}
104104

105105
#[inline]
@@ -110,48 +110,48 @@ impl PluginParser {
110110
timestamp: Option<u64>,
111111
) -> Result<impl Iterator<Item = (usize, Option<p::ParseYield<PluginParseMessage>>)>, p::Error>
112112
{
113-
//TODO AAZ: Temporary fix.
114-
// In original implementation we were returning Vec<Result<>>
115-
// Now we should return Result<Vec<>>.
116-
117-
// Old solution:
118-
// debug_assert!(
119-
// self.store.data_mut().results_queue.is_empty(),
120-
// "Host results most be empty at the start of parse call"
121-
// );
122-
//
123-
// let call_res = futures::executor::block_on(
124-
// self.plugin_bindings
125-
// .chipmunk_plugin_parser()
126-
// .call_parse_with_add(&mut self.store, input, timestamp),
127-
// );
128-
//
129-
// let parse_results = if let Err(call_err) = call_res {
130-
// vec![Err(ParseError::Unrecoverable(format!(
131-
// "Call parse on the plugin failed. Error: {call_err}"
132-
// )))]
133-
// } else {
134-
// std::mem::take(&mut self.store.data_mut().results_queue)
135-
// };
136-
//
137-
//
138-
// parse_results.into_iter().map(guest_to_host_parse_results)
139-
140-
// Temporary:
141-
Ok([].into_iter())
113+
debug_assert!(
114+
self.store.data_mut().results_queue.is_empty(),
115+
"Host results most be empty at the start of parse call"
116+
);
117+
118+
let parse_res = futures::executor::block_on(
119+
self.plugin_bindings
120+
.chipmunk_plugin_parser()
121+
.call_parse_with_add(&mut self.store, input, timestamp),
122+
)
123+
.map_err(|call_err| {
124+
p::Error::Unrecoverable(format!(
125+
"Call parse on the plugin failed. Error: {call_err}"
126+
))
127+
})?;
128+
129+
if let Err(parse_err) = parse_res {
130+
//TODO AAZ: Decide what to do if we have already parsed items.
131+
132+
if !self.store.data().results_queue.is_empty() {
133+
self.store.data_mut().results_queue.clear();
134+
return Err(p::Error::Unrecoverable(format!("Plugin return parse error and submitted parsed items on the same call. Plugin Error: {parse_err}")));
135+
} else {
136+
return Err(parse_err.into());
137+
}
138+
}
139+
140+
let parse_results = std::mem::take(&mut self.store.data_mut().results_queue);
141+
142+
let res = parse_results.into_iter().map(guest_to_host_parse_results);
143+
144+
Ok(res)
142145
}
143146
}
144147

145148
fn guest_to_host_parse_results(
146-
guest_res: Result<ParseReturn, ParseError>,
147-
) -> Result<(usize, Option<p::ParseYield<PluginParseMessage>>), p::Error> {
148-
match guest_res {
149-
Ok(parse_res) => Ok((
150-
parse_res.consumed as usize,
151-
parse_res.value.map(|v| v.into()),
152-
)),
153-
Err(parse_err) => Err(parse_err.into()),
154-
}
149+
parse_res: ParseReturn,
150+
) -> (usize, Option<p::ParseYield<PluginParseMessage>>) {
151+
(
152+
parse_res.consumed as usize,
153+
parse_res.value.map(|v| v.into()),
154+
)
155155
}
156156

157157
use parsers as p;

application/apps/indexer/plugins_host/src/v0_1_0/parser/parser_plugin_state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use super::bindings::{
66
logging::{self, Level},
77
parse_types, shared_types,
88
},
9-
ParseError, ParseReturn,
9+
ParseReturn,
1010
};
1111

1212
pub struct ParserPluginState {
1313
pub ctx: WasiCtx,
1414
pub table: ResourceTable,
15-
pub results_queue: Vec<Result<ParseReturn, ParseError>>,
15+
pub results_queue: Vec<ParseReturn>,
1616
}
1717

1818
impl ParserPluginState {
@@ -37,7 +37,7 @@ impl WasiView for ParserPluginState {
3737

3838
impl Host for ParserPluginState {
3939
// Add parse results one by one directly at the host memory
40-
fn add(&mut self, item: Result<ParseReturn, ParseError>) {
40+
fn add(&mut self, item: ParseReturn) {
4141
self.results_queue.push(item);
4242
}
4343
}

0 commit comments

Comments
 (0)