predicates-rs
predicates-rs copied to clipboard
Serde support?
Hi,
(De-)serialization via serde would be a great addition to predicates.
This would be useful when a UI has filters which should be stored (so users don't have to setup the filters every time they open the application).
Or to send predicates between a server and a client.
To clarify, you are wanting the ability to serialize and deserialize predicates?
In theory, we could support this on some of the predicates, so long as they don't take a closure, etc but how would you plan to dispatch between the different predicates since a trait is being used. Would it be expected that the predicates project provides an enum encompassing built-in predicates (which seems like a dead end) or would users do that?
To clarify, you are wanting the ability to serialize and deserialize predicates?
Yes, exactly. Basically, this would be similar to JsonLogic.
how would you plan to dispatch between the different predicates since a trait is being used. Would it be expected that the predicates project provides an enum encompassing built-in predicates (which seems like a dead end) or would users do that?
To be honest, I don't have any idea about any of this. I just had a cursory look at predicates to see whether I could use it, and thought it would be useful to suggest serde support, to enable more use-cases.
I thought it would be possible to just generate a serializable data structure, similar to JsonLogic, which can be deserialized back to a predicate.
In theory, we could support this on some of the predicates, so long as they don't take a closure
I've never used those crates, but maybe closures can be serialized via something like typetag or serde_traitobject.
For example, the creator of serde_traitobject also has the crate serde_closure.
First, I'll be upfront that this is not a priority for maintainers and this will likely only move forward by external contributors.
Second, some possible paths forward for this
- You can experiment with this on your side by using serde's ability to add serialization support for foreign types
- You could add an
unstable-serdefeature (ie all semver guarantees are out the window) and derive serde support for various predicates conditioned on the feature. You can then experiment on your side with how you want to handle dispatching which predicate is being deserialized.
Thanks for the suggestions, @epage!
For my current project I decided to just hard code some predicates, but I might give this a try in the future.
Just to be clear: I never expected that this will be implemented quickly (or at all), but I thought it would be a useful addition, and that an open issue is a good way to see if other people are interested in this as well (via comments and other reactions).