Skip to main content

Blocks

Struct Blocks 

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

Fluent builder for a list of Anthropic content blocks.

All chained appenders return &mut Self; finalize with Blocks::build (consumes the builder) or Blocks::build_message.

Implementations§

Source§

impl Blocks

Source

pub fn new() -> Self

Create an empty builder.

Source

pub fn text(&mut self, s: impl Into<String>) -> &mut Self

Append a plain text block.

Source

pub fn text_with_cache( &mut self, s: impl Into<String>, cache_control: CacheControl, ) -> &mut Self

Append a text block carrying a cache_control marker.

Source

pub fn image_b64( &mut self, data: &[u8], media_type: impl Into<String>, ) -> Result<&mut Self, BlockError>

Append a base64-encoded inline image block.

data is the raw image bytes; the builder base64-encodes them internally so callers never have to think about the encoding.

Returns BlockError::UnsupportedImageMediaType if media_type is not in VALID_IMAGE_MEDIA_TYPES.

Source

pub fn image_b64_with_cache( &mut self, data: &[u8], media_type: impl Into<String>, cache_control: CacheControl, ) -> Result<&mut Self, BlockError>

Same as Blocks::image_b64 but also tags the block with a cache_control marker.

Source

pub fn image_url(&mut self, url: impl Into<String>) -> &mut Self

Append an image block that references a remote URL.

Source

pub fn tool_use( &mut self, id: impl Into<String>, name: impl Into<String>, input: Value, ) -> &mut Self

Append a tool_use block. input is taken as a serde_json::Value so callers may use the serde_json::json! macro or pass any type implementing Into<Value>.

Source

pub fn tool_result( &mut self, tool_use_id: impl Into<String>, content: Value, is_error: bool, ) -> &mut Self

Append a tool_result block.

Source

pub fn document_b64( &mut self, data: &[u8], media_type: impl Into<String>, ) -> Result<&mut Self, BlockError>

Append a base64-encoded document block. Defaults to PDF when no media type is supplied (see Blocks::document_pdf_b64).

Returns BlockError::UnsupportedDocumentMediaType if media_type is not in VALID_DOCUMENT_MEDIA_TYPES.

Source

pub fn document_pdf_b64(&mut self, data: &[u8]) -> &mut Self

Convenience wrapper for the common application/pdf case.

Source

pub fn extend<I>(&mut self, blocks: I) -> &mut Self
where I: IntoIterator<Item = ContentBlock>,

Splice an existing iterable of content blocks into the builder.

Source

pub fn build(&mut self) -> Vec<ContentBlock>

Drain the builder and return the accumulated block list.

Takes &mut self (rather than self) so it composes naturally with the &mut Self-returning fluent chain (e.g. Blocks::new().text("hi").build()). The builder is left empty after the call; calling .build() twice yields [blocks, []].

Source

pub fn build_message(&mut self, role: impl Into<String>) -> Message

Drain the builder and wrap the accumulated blocks in a {role, content} envelope.

Source

pub fn len(&self) -> usize

Return the current number of accumulated blocks.

Source

pub fn is_empty(&self) -> bool

Return true when no blocks have been appended yet.

Source

pub fn tool_result_block( tool_use_id: impl Into<String>, content: Value, is_error: bool, ) -> ContentBlock

Build a single tool_result block without going through the builder. Mirrors the Python Blocks.tool_result classmethod.

Trait Implementations§

Source§

impl Clone for Blocks

Source§

fn clone(&self) -> Blocks

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Blocks

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Blocks

Source§

fn default() -> Blocks

Returns the “default value” for a type. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.