From 1ea498b5d9fa489eff8a24490108230b334a4501 Mon Sep 17 00:00:00 2001 From: AlessandroRuggiero <50781152+AlessandroRuggiero@users.noreply.github.com> Date: Mon, 31 Mar 2025 17:15:23 +0200 Subject: [PATCH] fix: better suggestion when encoding is not implemented (#843) 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 { 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 { 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_ --- convert-macros/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/convert-macros/src/lib.rs b/convert-macros/src/lib.rs index 63c3eca..e9b7a69 100644 --- a/convert-macros/src/lib.rs +++ b/convert-macros/src/lib.rs @@ -34,11 +34,11 @@ fn extract_encoding(attrs: &[Attribute]) -> Result { .iter() .filter(|attr| attr.path().is_ident("encoding")) .collect(); - ensure!(!encodings.is_empty(), "encoding needs to be specified"; try = "`#[encoding(ToJson)]`"); + ensure!(!encodings.is_empty(), "encoding needs to be specified"; try = "`#[encoding(Json)]`"); ensure!(encodings.len() < 2, encodings[1], "only one encoding can be specified"; try = "remove `{}`", encodings[1].to_token_stream()); Ok(encodings[0].parse_args().map_err( - |e| error_message!(e.span(), "{e}"; note= "expects a path"; try = "`#[encoding(ToJson)]`"), + |e| error_message!(e.span(), "{e}"; note= "expects a path"; try = "`#[encoding(Json)]`"), )?) }