pub trait TryIntoContents {
// Required method
fn try_into_contents(self) -> Result<Vec<Content>, Error>;
// Provided method
fn try_into_cached_content_for(
self,
model_name: &str,
) -> Result<CachedContent, Error>
where Self: Sized { ... }
}
Expand description
Fallible conversion to multiple Content
items
Use this for types that need to perform validation or conditional
construction of content. Prefer IntoContents
for infallible conversions.
§Examples
Custom serialization with media attachment:
use google_ai_rs::{TryIntoContents, Content, Error, IntoParts as _, Part};
struct UserInput {
message: String,
media: Option<MediaAttachment>
}
impl TryIntoContents for UserInput {
fn try_into_contents(self) -> Result<Vec<Content>, Error> {
let mut parts = self.message.into_parts();
if let Some(media) = self.media {
let part = match media {
MediaAttachment::Image(data) => Part::blob("image/png", data),
MediaAttachment::VoiceNote(data) => Part::blob("audio/wave", data),
};
parts.push(part);
}
Ok(vec![Content::from(parts)])
}
}
§Implementations
IntoContents
Required Methods§
Provided Methods§
Sourcefn try_into_cached_content_for(
self,
model_name: &str,
) -> Result<CachedContent, Error>where
Self: Sized,
fn try_into_cached_content_for(
self,
model_name: &str,
) -> Result<CachedContent, Error>where
Self: Sized,
Create cached content targeting a specific AI model