Skip to main content

TypedResult

Struct TypedResult 

Source
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

Source

pub fn new(result: QueryResult) -> Self

Create a new TypedResult from a QueryResult

Source

pub fn inner(&self) -> &QueryResult

Get the underlying QueryResult

Source

pub fn into_inner(self) -> QueryResult

Consume and get the underlying QueryResult

Source

pub fn row_count(&self) -> usize

Get the number of rows

Source

pub fn column_names(&self) -> Vec<String>

Get the column names (variables from RETURN clause)

Source

pub fn get_row(&self, index: usize) -> Option<&Row>

Get a specific row by index

Source

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()?;
Source

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)?;
}
Source

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()?;
Source

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()?;
Source

pub fn is_empty(&self) -> bool

Check if the result is empty (no rows)

Source

pub fn rows(&self) -> &[Row]

Iterate over rows

Trait Implementations§

Source§

impl From<QueryResult> for TypedResult

Source§

fn from(result: QueryResult) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.