KodrAus / json_str

Rust library for tidy json string literals
Apache License 2.0
5 stars 0 forks source link

Replacements #11

Closed KodrAus closed 7 years ago

KodrAus commented 7 years ago

Take another look at supporting simple replacements for serde::Serialize values. They can just be spliced in line.

This means json_str will depend on serde, but that's not the end of the world.

This kind of thing will be important for building json strings that aren't purely constant data.

KodrAus commented 7 years ago

Getting replacements into scope is probably going to be a nuisance, since the macro isn't seeing individual idents. It would be a bit crummy to have to add them into the macro like they were before.

KodrAus commented 7 years ago

So I'm thinking a json_fn macro could work, that would bind arguments in a closure that can be used as replacements:

let f = json_fn!(|qry: &str| {
    query: {
        query_string: $qry
    }
});

That will parse the given body until a replacement is found, parae its ident, and repeat until the end.

The result of the macro would be a closure that can be called with arguments and pushes each fragment onto a result string which is returned.

KodrAus commented 7 years ago

The arguments should just be str refs, and the caller can decide how that value is constructed. This means quoted strings will be a bit less ergonomic, but the idea is that you have control over whether or not the value is stringified. If serde is used then it'll handle this for you.