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.
8pub struct FileHandle {
9    inner: super::model::File,
10    client: Arc<GeminiClient>,
11}
12
13impl FileHandle {
14    pub(crate) fn new(client: Arc<GeminiClient>, inner: super::model::File) -> Self {
15        Self { inner, client }
16    }
17
18    pub fn name(&self) -> &str {
19        &self.inner.name
20    }
21
22    /// Get the file metadata.
23    pub fn get_file_meta(&self) -> &super::model::File {
24        &self.inner
25    }
26
27    /// Delete the file.
28    pub async fn delete(self) -> Result<(), (Self, Error)> {
29        match self
30            .client
31            .delete_file(&self.inner.name)
32            .await
33            .context(ClientSnafu)
34        {
35            Ok(_) => Ok(()),
36            Err(e) => Err((self, e)),
37        }
38    }
39
40    /// Download the file.
41    ///
42    /// Work only for files that was generated by Gemini.
43    pub async fn download(&self) -> Result<Vec<u8>, Error> {
44        self.client
45            .download_file(self.name())
46            .await
47            .context(ClientSnafu)
48    }
49}