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
impl QueryBuilder
Sourcepub fn session(self, session_id: SessionId) -> Self
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?;
Sourcepub fn format(self, format: StreamFormat) -> Self
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?;
Sourcepub async fn send(self) -> Result<String>
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);
Sourcepub async fn send_full(self) -> Result<ClaudeResponse>
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);
}
}
Sourcepub async fn stream(self) -> Result<MessageStream>
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),
}
}
Sourcepub async fn parse_output<T: DeserializeOwned>(self) -> Result<T>
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§
Auto Trait Implementations§
impl Freeze for QueryBuilder
impl RefUnwindSafe for QueryBuilder
impl Send for QueryBuilder
impl Sync for QueryBuilder
impl Unpin for QueryBuilder
impl UnwindSafe for QueryBuilder
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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