Skip to main content

systemprompt_traits/
storage.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use std::path::Path;
4
5/// Unique identifier for a stored file
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct StoredFileId(pub String);
8
9impl StoredFileId {
10    #[must_use]
11    pub fn new(id: impl Into<String>) -> Self {
12        Self(id.into())
13    }
14
15    #[must_use]
16    pub fn as_str(&self) -> &str {
17        &self.0
18    }
19}
20
21impl From<String> for StoredFileId {
22    fn from(s: String) -> Self {
23        Self(s)
24    }
25}
26
27impl From<&str> for StoredFileId {
28    fn from(s: &str) -> Self {
29        Self(s.to_string())
30    }
31}
32
33impl std::fmt::Display for StoredFileId {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{}", self.0)
36    }
37}
38
39/// Metadata about a stored file
40#[derive(Debug, Clone)]
41pub struct StoredFileMetadata {
42    pub id: StoredFileId,
43    pub path: String,
44    pub mime_type: String,
45    pub size_bytes: Option<i64>,
46    pub created_at: chrono::DateTime<chrono::Utc>,
47    pub updated_at: chrono::DateTime<chrono::Utc>,
48}
49
50/// Trait for file storage operations
51///
52/// Implementations of this trait handle the actual storage and retrieval
53/// of file contents, whether that's local filesystem, cloud storage, etc.
54#[async_trait]
55pub trait FileStorage: Send + Sync {
56    /// Store a file at the given path with the provided content
57    async fn store(&self, path: &Path, content: &[u8]) -> Result<StoredFileId>;
58
59    /// Retrieve the contents of a file by its ID
60    async fn retrieve(&self, id: &StoredFileId) -> Result<Vec<u8>>;
61
62    /// Delete a file by its ID
63    async fn delete(&self, id: &StoredFileId) -> Result<()>;
64
65    /// Get metadata for a file by its ID
66    async fn metadata(&self, id: &StoredFileId) -> Result<StoredFileMetadata>;
67
68    /// Check if a file exists by its ID
69    async fn exists(&self, id: &StoredFileId) -> Result<bool>;
70
71    /// Get the public URL for a file (if applicable)
72    fn public_url(&self, _id: &StoredFileId) -> Option<String> {
73        None
74    }
75}