Skip to main content

TextAccumulator

Struct TextAccumulator 

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

Accumulates countable text strings from structured Gemini API objects.

This is a faithful port of the Python SDK’s _TextsAccumulator class. It traverses Content, Tool, FunctionCall, FunctionResponse, and Schema objects, extracting all text that Google counts when computing token counts.

§How text is extracted

  • Text parts: the text string itself
  • Function calls: the function name, plus all dict keys and string values from the args
  • Function responses: the function name, plus all dict keys and string values from the response
  • Function declarations (tools): the name, description, plus recursive schema traversal of parameters and response
  • Schemas: format, description, enum values, required field names, property keys, and recursive traversal of nested schemas

Implementations§

Source§

impl TextAccumulator

Source

pub fn new() -> Self

Creates a new empty accumulator.

Source

pub fn get_texts(&self) -> &[String]

Returns the accumulated text segments.

Source

pub fn into_texts(self) -> Vec<String>

Consumes the accumulator and returns the accumulated text segments.

Source

pub fn add_contents(&mut self, contents: &[Content])

Adds all text from multiple content objects.

Source

pub fn add_content(&mut self, content: &Content)

Adds all countable text from a single content object.

Processes each part in the content by delegating to add_part.

Source

pub fn add_part(&mut self, part: &Part)

Adds countable text from a single content part.

Processes the part’s fields:

Source

pub fn add_function_call(&mut self, function_call: &FunctionCall)

Adds countable text from a function call.

Extracts the function name and traverses the args dictionary, collecting all keys and string values.

Source

pub fn add_tools(&mut self, tools: &[Tool])

Adds countable text from multiple tools.

Source

pub fn add_tool(&mut self, tool: &Tool)

Adds countable text from a single tool definition.

Processes each function declaration in the tool.

Source

pub fn add_function_responses(&mut self, responses: &[FunctionResponse])

Adds countable text from multiple function responses.

Source

pub fn add_function_response(&mut self, function_response: &FunctionResponse)

Adds countable text from a function response.

Extracts the function name and traverses the response dictionary, collecting all keys and string values.

Source

pub fn add_schema(&mut self, schema: &Schema)

Adds countable text from a schema definition.

Extracts format, description, enum values, required field names, property keys, and recursively processes nested schemas (items, properties, examples).

Trait Implementations§

Source§

impl Default for TextAccumulator

Source§

fn default() -> Self

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

Source§

type Output = T

Should always be Self
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.