pub struct TypedResult { /* private fields */ }Expand description
Wrapper around QueryResult with additional type-safe methods
TypedResult provides convenient methods for deserializing query results into Rust types.
§Examples
use serde::Deserialize;
use graphlite_sdk::GraphLite;
#[derive(Deserialize, Debug)]
struct Person {
name: String,
age: u32,
}
let result = session.query("MATCH (p:Person) RETURN p.name as name, p.age as age")?;
let typed = TypedResult::from(result);
// Deserialize each row into a Person struct
for person in typed.deserialize_rows::<Person>()? {
println!("Person: {:?}", person);
}Implementations§
Source§impl TypedResult
impl TypedResult
Sourcepub fn new(result: QueryResult) -> Self
pub fn new(result: QueryResult) -> Self
Create a new TypedResult from a QueryResult
Sourcepub fn inner(&self) -> &QueryResult
pub fn inner(&self) -> &QueryResult
Get the underlying QueryResult
Sourcepub fn into_inner(self) -> QueryResult
pub fn into_inner(self) -> QueryResult
Consume and get the underlying QueryResult
Sourcepub fn column_names(&self) -> Vec<String>
pub fn column_names(&self) -> Vec<String>
Get the column names (variables from RETURN clause)
Sourcepub fn deserialize_rows<T: DeserializeOwned>(&self) -> Result<Vec<T>>
pub fn deserialize_rows<T: DeserializeOwned>(&self) -> Result<Vec<T>>
Deserialize all rows into a vector of the given type
Each row is converted to a JSON object and then deserialized into the target type using serde.
§Type Parameters
T- Type to deserialize each row into (must implement Deserialize)
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Person { name: String, age: u32 }
let result = session.query("MATCH (p:Person) RETURN p.name as name, p.age as age")?;
let typed = TypedResult::from(result);
let people: Vec<Person> = typed.deserialize_rows()?;Sourcepub fn deserialize_row<T: DeserializeOwned>(&self, row: &Row) -> Result<T>
pub fn deserialize_row<T: DeserializeOwned>(&self, row: &Row) -> Result<T>
Deserialize a single row into the given type
§Type Parameters
T- Type to deserialize the row into (must implement Deserialize)
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Person { name: String, age: u32 }
let result = session.query("MATCH (p:Person) RETURN p.name as name, p.age as age LIMIT 1")?;
let typed = TypedResult::from(result);
if let Some(row) = typed.get_row(0) {
let person: Person = typed.deserialize_row(row)?;
}Sourcepub fn first<T: DeserializeOwned>(&self) -> Result<T>
pub fn first<T: DeserializeOwned>(&self) -> Result<T>
Get the first row as the given type
Convenience method for queries that return a single row.
§Type Parameters
T- Type to deserialize into (must implement Deserialize)
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Count { count: i64 }
let result = session.query("MATCH (p:Person) RETURN count(p) as count")?;
let typed = TypedResult::from(result);
let count: Count = typed.first()?;Sourcepub fn scalar<T: DeserializeOwned>(&self) -> Result<T>
pub fn scalar<T: DeserializeOwned>(&self) -> Result<T>
Get a single value from the first row and first column
Useful for queries that return a single scalar value.
§Examples
let result = session.query("MATCH (p:Person) RETURN count(p)")?;
let typed = TypedResult::from(result);
let count: i64 = typed.scalar()?;Trait Implementations§
Source§impl From<QueryResult> for TypedResult
impl From<QueryResult> for TypedResult
Source§fn from(result: QueryResult) -> Self
fn from(result: QueryResult) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for TypedResult
impl RefUnwindSafe for TypedResult
impl Send for TypedResult
impl Sync for TypedResult
impl Unpin for TypedResult
impl UnsafeUnpin for TypedResult
impl UnwindSafe for TypedResult
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more