Closed cathaysia closed 1 year ago
I use such codes:
impl<'a> ArgType<'a> for Time {
type Output = Self;
fn from_value(value: Option<&'a Value>) -> Result<Self::Output, minijinja::Error> {
let a = serde_json::to_string(value.unwrap()).unwrap();
Ok(serde_json::from_str(&a).unwrap())
}
}
I think what this is calling for is probably two things:
IntoDeserializer
for Value
.ViaDeserialize<T>
(name tbd) which uses IntoDeserializer
and Deref
s into T
.With that one could do something like this:
fn strftime(time: ViaDeserialize<Time>) -> String {
time.to_string()
}
Yes, this is also a good solution. But is there an implementation that makes the user imperceptible? For example, if Time implements the Deserialize trait, then it can be used directly:
fn strftime(time: Time) -> String {
time.to_string()
}
I don't think this can be done without specialization.
@cathaysia I implemented this in #325. Maybe you could try this and see how it feels to you. Requires enabling the deserialization
feature.
I used the new way instead of the previously implemented procedural macros. it's actually very good. This also fixes the issue of seq not deserializing to vec in the old implementation.
Now suppose there is a type ViaDeserialize
Wouldn't it be nice to implement From for ViaDeserialize?
It would be a breaking change at the very least and it's likely going to be quite a bit slower for quite a few setups.
Currently, all filter parameter types need to implement the ArgType trait. This is mainly to implement the from_value function. But while a variable can be convert to Value by serde::serialize, it cannot be vice versa.
Is there a possibility to deserialize the Value back into a variable? Going a step further, is it possible to implement the ArgType trait for all types that implement Deserialize ?