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

Simplify manual implementations of QueryArg/Queryable #269

Open
XAMPPRocky opened this issue Jul 19, 2023 · 0 comments
Open

Simplify manual implementations of QueryArg/Queryable #269

XAMPPRocky opened this issue Jul 19, 2023 · 0 comments

Comments

@XAMPPRocky
Copy link

Currently QueryArg's definition is pretty opaque and how an implementor is supposed to implement encode_slot and check_descriptor is undocumented, and a user is mostly expected to use this trait through the derive macros. However using derive macros is not possible when the types are defined in another crate. This requires you to duplicate the type definition in your own crate and write a From conversion. This approach is not scalable and hinders the ability to use EdgeDB with types in the Rust ecosystem.

I don't have all the context on why these traits are defined like this, but looking at the source it seems like it's leaking some encoder specific details that aren't relevant for users. It would be great if the implementations of these traits could be simplified into a codec model like serde so that users could easily write their own manual implementations.

// External crate…
struct A<T: std::fmt::Display> {
    foo: String,
    bar: T,
    baz: HashMap<String, serde_json::Value>,
}

// My crate…
struct B<T: std::fmt::Display>(A<T>);

impl<T: std::fmt::Display> edgedb_protocol::QueryArg for B<T> {
    fn encode(&self, enc: &mut edgedb_protocol::Encoder) -> Result<(), Error> {
        self.0.foo.encode(enc)?;
        enc.encode_str(&self.0.bar.to_string())?;
        enc.encode_json(&serde_json::encode_to_value(&self.0.baz).map_err(Error::custom)?)?;
        Ok(())
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant