gemini_rust/files/
handle.rs

1use snafu::ResultExt;
2use std::sync::Arc;
3
4use super::*;
5use crate::client::GeminiClient;
6
7/// A handle to a file on the Gemini API.
8#[derive(Clone)]
9pub struct FileHandle {
10    inner: super::model::File,
11    client: Arc<GeminiClient>,
12}
13
14impl FileHandle {
15    pub(crate) fn new(client: Arc<GeminiClient>, inner: super::model::File) -> Self {
16        Self { inner, client }
17    }
18
19    pub fn name(&self) -> &str {
20        &self.inner.name
21    }
22
23    /// Get the file metadata.
24    pub fn get_file_meta(&self) -> &super::model::File {
25        &self.inner
26    }
27
28    /// Delete the file.
29    pub async fn delete(self) -> Result<(), (Self, Error)> {
30        match self
31            .client
32            .delete_file(&self.inner.name)
33            .await
34            .context(ClientSnafu)
35        {
36            Ok(_) => Ok(()),
37            Err(e) => Err((self, e)),
38        }
39    }
40
41    /// Download the file.
42    ///
43    /// Work only for files that was generated by Gemini.
44    pub async fn download(&self) -> Result<Vec<u8>, Error> {
45        self.client
46            .download_file(self.name())
47            .await
48            .context(ClientSnafu)
49    }
50}