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§
Sourcetype Response: DeserializeOwned
type Response: DeserializeOwned
The intermediate response shape deserialized by Serde.
Required Methods§
Provided Methods§
Sourcefn named(self, id: impl Into<String>) -> Result<NamedQuery<Self>, ProviderError>where
Self: Sized,
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.
Sourcefn join<Q: Query>(self, other: Q) -> JoinedQuery<Self, Q>where
Self: Sized,
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.
Sourcefn map<A, F>(self, map: F) -> MappedQuery<Self, F>
fn map<A, F>(self, map: F) -> MappedQuery<Self, F>
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".