Skip to content

fix: better suggestion when encoding is not implemented #843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025

Conversation

AlessandroRuggiero
Copy link
Contributor

@AlessandroRuggiero AlessandroRuggiero commented Mar 31, 2025

Here is a code snippet that demonstrates the issue:

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:

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

Copy link
Contributor

@zshipko zshipko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants