#[non_exhaustive]pub struct Annotation {
pub start_index: usize,
pub end_index: usize,
pub source: Option<String>,
}Expand description
An annotation linking a text span to its source.
Annotations provide citation information for specific portions of generated text,
typically appearing when using grounding tools like GoogleSearch or UrlContext.
§Byte Indices
Important: The start_index and end_index fields are byte positions (not
character indices) in the text content. This matches the UTF-8 byte offsets used
by the Gemini API. For multi-byte characters (like emoji or non-ASCII text), you
may need to use byte-slicing rather than character indexing.
§Example
// Get all annotations from the response
for annotation in response.all_annotations() {
println!(
"Text at bytes {}..{} sourced from: {}",
annotation.start_index,
annotation.end_index,
annotation.source.as_deref().unwrap_or("<no source>")
);
}§Extracting Annotated Text
To extract the annotated substring from the response text:
if let Some(text) = response.as_text() {
// Use byte slicing since indices are byte positions
let bytes = text.as_bytes();
if annotation.end_index <= bytes.len() {
if let Ok(span) = std::str::from_utf8(&bytes[annotation.start_index..annotation.end_index]) {
println!("Cited text: {}", span);
}
}
}Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.start_index: usizeStart of the segment in the text (byte position, inclusive).
This is a byte offset into the UTF-8 encoded text, not a character index.
end_index: usizeEnd of the segment in the text (byte position, exclusive).
This is a byte offset into the UTF-8 encoded text, not a character index.
source: Option<String>Source attributed for this portion of the text.
This could be a URL, title, or other identifier depending on the grounding
tool used (e.g., GoogleSearch or UrlContext).
Implementations§
Source§impl Annotation
impl Annotation
Sourcepub fn new(start_index: usize, end_index: usize, source: Option<String>) -> Self
pub fn new(start_index: usize, end_index: usize, source: Option<String>) -> Self
Creates a new annotation with the given span indices and optional source.
§Arguments
start_index- Start of the segment in the text (byte position, inclusive)end_index- End of the segment in the text (byte position, exclusive)source- Optional source attribution (URL, title, or identifier)
§Example
let annotation = Annotation::new(0, 10, Some("https://example.com".to_string()));
assert_eq!(annotation.start_index, 0);
assert_eq!(annotation.end_index, 10);
assert!(annotation.has_source());Sourcepub fn byte_len(&self) -> usize
pub fn byte_len(&self) -> usize
Returns the byte length of the annotated span.
This is equivalent to end_index - start_index.
Sourcepub fn has_source(&self) -> bool
pub fn has_source(&self) -> bool
Returns true if this annotation has a source attribution.
Sourcepub fn extract_span<'a>(&self, text: &'a str) -> Option<&'a str>
pub fn extract_span<'a>(&self, text: &'a str) -> Option<&'a str>
Extracts the annotated substring from the given text.
Returns None if the indices are out of bounds or if the byte slice
doesn’t form valid UTF-8.
§Arguments
text- The full text content to extract from
§Example
let annotation = Annotation::new(0, 5, Some("https://example.com".to_string()));
let text = "Hello, world!";
assert_eq!(annotation.extract_span(text), Some("Hello"));Trait Implementations§
Source§impl Clone for Annotation
impl Clone for Annotation
Source§fn clone(&self) -> Annotation
fn clone(&self) -> Annotation
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Annotation
impl Debug for Annotation
Source§impl<'de> Deserialize<'de> for Annotation
impl<'de> Deserialize<'de> for Annotation
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for Annotation
impl PartialEq for Annotation
Source§impl Serialize for Annotation
impl Serialize for Annotation
impl Eq for Annotation
impl StructuralPartialEq for Annotation
Auto Trait Implementations§
impl Freeze for Annotation
impl RefUnwindSafe for Annotation
impl Send for Annotation
impl Sync for Annotation
impl Unpin for Annotation
impl UnwindSafe for Annotation
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);