Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/pine-builtin-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ fn generate_value_conversion(
has_default: bool,
) -> proc_macro2::TokenStream {
let type_str = quote! { #field_type }.to_string();
let field_name_str = field_name.to_string();
let field_name_str = field_name_str.trim_start_matches("r#");

// Check if this is an Option type
let is_option = type_str.contains("Option");
Expand Down Expand Up @@ -841,7 +843,7 @@ fn generate_value_conversion(
if matches!(arg_value, Value::Na) {
None
} else {
Some(<#inner as ::pine_interpreter::FromArg<O>>::from_arg(&arg_value)?)
Some(<#inner as ::pine_interpreter::FromArg<O>>::from_arg(#field_name_str, &arg_value)?)
}
}
}
Expand All @@ -866,7 +868,7 @@ fn generate_value_conversion(
} else {
// Any other type is converted from the argument — e.g. a string-constant
// enum deserialized from a `Value::String`.
quote! { <#field_type as ::pine_interpreter::FromArg<O>>::from_arg(&arg_value)? }
quote! { <#field_type as ::pine_interpreter::FromArg<O>>::from_arg(#field_name_str, &arg_value)? }
};

if has_default {
Expand Down
21 changes: 14 additions & 7 deletions crates/pine-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ use thiserror::Error;

pub use pine_core::LibraryLoader;

/// Convert a call argument into a builtin parameter type. Blanket-implemented for
/// any `serde`-deserializable type by deserializing the argument's string, so a
/// string-constant enum (`LabelStyle`, …) can be a builtin parameter directly.
/// Convert a builtin call argument into a custom parameter type — one that isn't
/// a macro primitive (`String`, `f64`, `bool`, `Color`, `Value`, `Num`).
///
/// The rule: the type implements [`serde::Deserialize`] and is deserialized from
/// the argument's `Value::String`, so a fieldless enum with
/// `#[serde(rename_all = "snake_case")]` accepts the lowercase constants scripts
/// write (`"close"`, `"signed"`, …). On mismatch the error names the argument and
/// — for an enum — lists the accepted variants.
pub trait FromArg<O: PineOutput>: Sized {
fn from_arg(value: &Value<O>) -> Result<Self, RuntimeError>;
fn from_arg(arg: &str, value: &Value<O>) -> Result<Self, RuntimeError>;
}

impl<O: PineOutput, T: serde::de::DeserializeOwned> FromArg<O> for T {
fn from_arg(value: &Value<O>) -> Result<Self, RuntimeError> {
let s = value.as_string()?;
fn from_arg(arg: &str, value: &Value<O>) -> Result<Self, RuntimeError> {
let s = value
.as_string()
.map_err(|_| RuntimeError::TypeError(format!("`{arg}` expects a string constant")))?;
let de = serde::de::value::StrDeserializer::<serde::de::value::Error>::new(s.as_str());
T::deserialize(de).map_err(|e| RuntimeError::TypeError(format!("invalid value `{s}`: {e}")))
T::deserialize(de).map_err(|e| RuntimeError::TypeError(format!("`{arg}`: {e}")))
}
}

Expand Down
Loading