Skip to main content

Query

Trait Query 

Source
pub trait Query:
    Serialize
    + WasmCompatSend
    + WasmCompatSync {
    type Response: DeserializeOwned;
    type Output;

    // Required method
    fn decode(
        &self,
        response: Self::Response,
    ) -> Result<Self::Output, ProviderError>;

    // Provided methods
    fn named(
        self,
        id: impl Into<String>,
    ) -> Result<NamedQuery<Self>, ProviderError>
       where Self: Sized { ... }
    fn join<Q: Query>(self, other: Q) -> JoinedQuery<Self, Q>
       where Self: Sized { ... }
    fn map<A, F>(self, map: F) -> MappedQuery<Self, F>
       where Self: Sized,
             F: Fn(Self::Output) -> A + WasmCompatSend + WasmCompatSync { ... }
}
Expand description

A serializable query with a statically determined response and output. Struct field names (including Serde renames) supply the wire question IDs. Serde handles encoding and response structure; decode validates each field against its original question and converts it to its application answer.

use rig_core::error::ProviderError;
use rig_typesafeai::{Noul, NoulAnswer, Query};
use serde::{Serialize, Deserialize};

// The fields are declared once, for both questions and answers.
#[derive(Serialize, Deserialize)]
struct Assessment<R, V> {
    ready: R,
    needs_review: V,
}

impl<R: Query, V: Query> Query for Assessment<R, V> {
    type Response = Assessment<R::Response, V::Response>;
    type Output = Assessment<R::Output, V::Output>;

    fn decode(&self, response: Self::Response) -> Result<Self::Output, ProviderError> {
        Ok(Assessment {
            ready: self.ready.decode(response.ready)?,
            needs_review: self.needs_review.decode(response.needs_review)?,
        })
    }
}

let query = Assessment {
    ready: Noul::new("Is this ready to ship?")?,
    needs_review: Noul::new("Does this need human review?")?,
};

Required Associated Types§

Source

type Response: DeserializeOwned

The intermediate response shape deserialized by Serde.

Source

type Output

The validated application answer shape.

Required Methods§

Source

fn decode( &self, response: Self::Response, ) -> Result<Self::Output, ProviderError>

Validate and convert a deserialized response. Composite queries delegate to their fields, keeping validation inside the evaluation error boundary.

Provided Methods§

Source

fn named(self, id: impl Into<String>) -> Result<NamedQuery<Self>, ProviderError>
where Self: Sized,

Give a standalone question a runtime ID. Named struct fields do not need this.

Source

fn join<Q: Query>(self, other: Q) -> JoinedQuery<Self, Q>
where Self: Sized,

Flatten two object-shaped queries into one evaluation request. Duplicate IDs are rejected before sending the request. Child queries must serialize with stable field names: decoding uses those names to partition the response before delegating to each child.

Source

fn map<A, F>(self, map: F) -> MappedQuery<Self, F>
where Self: Sized, F: Fn(Self::Output) -> A + WasmCompatSend + WasmCompatSync,

Project validated answers into an application value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<Q: Query + ?Sized> Query for &Q

Source§

type Response = <Q as Query>::Response

Source§

type Output = <Q as Query>::Output

Source§

fn decode( &self, response: Self::Response, ) -> Result<Self::Output, ProviderError>

Source§

impl<Q: Query> Query for BTreeMap<String, Q>

Source§

type Response = BTreeMap<String, <Q as Query>::Response>

Source§

type Output = BTreeMap<String, <Q as Query>::Output>

Source§

fn decode( &self, response: Self::Response, ) -> Result<Self::Output, ProviderError>

Implementors§