neo4j-labs / neo4rs

Neo4j driver for rust

Home Page:https://docs.rs/neo4rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to cast the `row.get()` method result to a custom Struct?

tausifcreates opened this issue · comments

The docs only shows ways to cast to Node, String etc. What if I return a collect from the query which holds a list of, say a custom struct type. How do we do that?

I'm gonna refer to my comment from the other issue #108 (comment)

This will be possible with the serde integration, here is a unit test that deserializes a row into custom structs: https://github.com/knutwalker/neo4rs/blob/9681029210e47f55e2114f1572bfc875749cdfd7/lib/src/row.rs#L299
I would also like to add something like your example on the other issue as an example/integration test.

@knutwalker Thanks for your response! It surely will solve my pain points.

@tausifcreates we've just released 0.7.0-rc.1 with serde support. It's missing proper documentation, but I can see that maybe something like this works for you

#[derive(Debug, serde::Deserialize)]
struct Thing {
  some_prop: String,
  another_prop: u32,
}

// ...

let things = row.to::<Vec<Thing>>()?;
// or maybe
let things = row.get::<Vec<Things>>("column")?;

It would be great if you could give it a try and see if that works for you.

Hey thanks @knutwalker! Actually I shared the release announcement on reddit