Skip to content
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

How to use ramhorns with serde_json::Value? #44

Open
thesurlydev opened this issue Mar 14, 2021 · 7 comments
Open

How to use ramhorns with serde_json::Value? #44

thesurlydev opened this issue Mar 14, 2021 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@thesurlydev
Copy link

I'd like to be able to use ramhorns with arbitrary data supplied via JSON. From what I can tell, serde_json::Value fits the requirement for doing this but ramhorns uses a derive trait which won't work with serde_json::Value because it's an enum.

Is there a workaround or an easier way to accomplish using arbitrary data with ramhorns that doesn't require using the ramhorns::Content trait?

@maciejhirsz
Copy link
Owner

Ramhorns supports HashMaps and BTreeMaps so you can construct arbitrary dynamic structures that way. To work with serde_json::Value directly the Content trait would need to be implemented for it, we could add it to the ramhorns crate via a feature with optional serde_json dependency.

This should be a good first issue if anyone wants to drop a PR, I might not have time to tackle this within the next week or two.

@maciejhirsz maciejhirsz added enhancement New feature or request good first issue Good for newcomers labels Mar 14, 2021
@thesurlydev
Copy link
Author

I will try implementing Content trait for serde_json::Value

@thesurlydev
Copy link
Author

If serde_json is to be an optional dependency, then how shall I organize the added support for serde_json::Value? I'm somewhat new to Rust, so providing as much detail as possible would be appreciated.

@maciejhirsz
Copy link
Owner

Just put the impl for serde_json::Value in a separate module, like serde.rs. In lib.rs do a conditional import:

#[cfg(feature = "json")]
mod serde;

Adding serde_json as optional feature in Cargo.toml:

[dependencies]
# ...all the other deps
serde_json = { version = "1.0", optional = true }

[features]
default = []

# Implements `Content` for `serde_json::Value`
json = ["serde_json"]

That way enabling the json feature will build Ramhorns with serde_json and the module that contains the trait impl.

@eeyun
Copy link

eeyun commented Sep 24, 2021

I think I might be hitting this as well.

But to make sure - there is no alternative means of using arbitary json (even if say, converted to some string type) with a template and ramhorns today, correct? I've been unsuccessful anyways.

If that is the case then I'm a huge 👍 on this feature request and I would be curious to know if there has been any progress on the feature!

@maciejhirsz
Copy link
Owner

@eeyun don't think there has been, happy to accept a PR.

@eeyun
Copy link

eeyun commented Sep 30, 2021

Cool I just wanted to make sure. It would be a great feature. I may try to get something together for it but a bit underwater currently. Thanks for maintaining the project either way!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants