Struct QueryBuilder

Source
pub struct QueryBuilder { /* private fields */ }
Expand description

Builder for constructing and executing Claude queries

The QueryBuilder provides a fluent interface for configuring queries before sending them to Claude. It supports different response formats and execution modes.

§Examples

// Simple query
let response = client
    .query("What is Rust?")
    .send()
    .await?;

// Query with session and custom format
let response = client
    .query("Continue the conversation")
    .session("my-session".to_string())
    .format(StreamFormat::Json)
    .send_full()
    .await?;

Implementations§

Source§

impl QueryBuilder

Source

pub fn session(self, session_id: SessionId) -> Self

Specify a session ID for this query

This allows the query to be part of an ongoing conversation with maintained context.

§Examples
let response = client
    .query("Remember this: the key is 42")
    .session("my-session".to_string())
    .send()
    .await?;
Source

pub fn format(self, format: StreamFormat) -> Self

Override the output format for this specific query

This allows you to use a different format than the client’s default configuration for this specific query.

§Examples
let response = client
    .query("What is the weather?")
    .format(StreamFormat::Json)
    .send_full()
    .await?;
Source

pub async fn send(self) -> Result<String>

Send the query and return just the text content

This is the simplest way to get a response from Claude, returning only the text without metadata.

§Examples
let answer = client
    .query("What is 2 + 2?")
    .send()
    .await?;
println!("Answer: {}", answer);
Source

pub async fn send_full(self) -> Result<ClaudeResponse>

Send the query and return the full response with metadata

This provides access to cost information, session IDs, token usage, and the raw JSON response for advanced use cases.

§Examples
let response = client
    .query("Explain quantum computing")
    .send_full()
    .await?;

println!("Response: {}", response.content);
if let Some(metadata) = &response.metadata {
    if let Some(cost) = metadata.cost_usd {
        println!("Cost: ${:.6}", cost);
    }
}
Source

pub async fn stream(self) -> Result<MessageStream>

Send the query and return a stream of messages

This allows for real-time processing of Claude’s response as it’s being generated, useful for implementing streaming UIs.

§Examples
let mut stream = client
    .query("Write a short story")
    .stream()
    .await?;

while let Some(message_result) = stream.next().await {
    match message_result {
        Ok(message) => {
            // Process each message as it arrives
            println!("Message: {:?}", message);
        }
        Err(e) => eprintln!("Stream error: {}", e),
    }
}
Source

pub async fn parse_output<T: DeserializeOwned>(self) -> Result<T>

Send the query and parse the response as JSON

This is a convenience method for when you expect Claude to return structured data that can be deserialized into a specific type.

§Examples
#[derive(Deserialize)]
struct WeatherData {
    temperature: f64,
    humidity: f64,
}

let weather: WeatherData = client
    .query("Return weather data as JSON: {\"temperature\": 22.5, \"humidity\": 65}")
    .parse_output()
    .await?;

println!("Temperature: {}°C", weather.temperature);

Trait Implementations§

Source§

impl Debug for QueryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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 = Infallible

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more