mirror of
https://github.com/extism/extism.git
synced 2026-04-23 03:00:11 -04:00
Here is a code snippet that demonstrates the issue:
```rust
use extism_pdk::{encoding, plugin_fn, FnResult, FromBytes, Json, ToBytes};
use serde::{Serialize, Deserialize};
#[derive(Deserialize,FromBytes)]
pub struct Input {
value:String
}
#[derive(Serialize, ToBytes)]
pub struct Output {
value: f64,
}
#[plugin_fn]
pub fn convert(in_value: Input) -> FnResult<Output> {
Ok(Output { value: in_value.value.parse().expect("Conversion failed") })
}
```
Because the structs don't derive `encoding` the rust-analyzer gives this
suggestion:
```
encoding needs to be specified
= try: `#[encoding(ToJson)]`
```
I don't think this suggestion is correct because i could not find
`ToJson` anywhere. Adding `#[encoding(Json)]` solves the issue (as
described in the docs).
The final code would look as follows:
```rust
use extism_pdk::{encoding, plugin_fn, FnResult, FromBytes, Json, ToBytes};
use serde::{Serialize, Deserialize};
#[derive(Deserialize,FromBytes)]
#[encoding(Json)]
pub struct Input {
value:String
}
#[derive(Serialize, ToBytes)]
#[encoding(Json)]
pub struct Output {
value: f64,
}
#[plugin_fn]
pub fn convert(in_value: Input) -> FnResult<Output> {
Ok(Output { value: in_value.value.parse().expect("Conversion failed") })
}
```
And compiles perfectly.
I think the suggestion given by the rust-analyzer is incorrect so i am
proposing a fix for this.
Since I'm new to the project, any feedback is appreciated.
Thanks!
_Alessandro Ruggiero_