pub struct Attachment {
pub name: String,
pub content_type: Option<String>,
/* private fields */
}Expand description
Represents a file/image/document sent or received as part of a message.
§Examples
- An attachment sent by the user.
- An image generated by the AI.
§Equality
When comparing, two Attachments are considered equal if they have the same
metadata (name, content type, etc), and they point to the same data.
This means:
- For in-memory attachments, the content is compared by reference (pointer equality).
- For attachments picked from a native file system, the path is compared.
- For attachments picked on the web, the (wrapped) file handle must be the same.
- For persisted attachments, the storage key is compared (independent of the reader).
The content itself is never compared, because not all attachments can be read synchronously, and it would be expensive to do so.
§Serialization
Unless a persistence key is configured, when serializing this type, the “pointer” to data is skipped and the attachment will become “unavailable” when deserialized back.
Two unavailable attachments are considered equal if they have the same metadata.
If a persistence key is configured, the attachment will be serialized with the key. That key will be restored when deserializing, however, you will need to manually restore its reader implementation.
§Abstraction details
Different than other abstraction in crate::protocol, this one not only
acts as “data model”, but also as system and I/O interface. This coupling was
originally intended to give “pragmatic” access to methods like read() and pick_multiple(),
but as everything mixing concerns, this now causes some issues like making the persistence
feature uglier to integrate. So this abstraction is likely to change in the future.
Fields§
§name: StringNormally the original filename.
content_type: Option<String>Mime type of the content, if known.
Implementations§
Source§impl Attachment
impl Attachment
Sourcepub fn from_bytes(
name: String,
content_type: Option<String>,
content: &[u8],
) -> Self
pub fn from_bytes( name: String, content_type: Option<String>, content: &[u8], ) -> Self
Creates a new in-memory attachment from the given bytes.
Sourcepub fn from_base64(
name: String,
content_type: Option<String>,
base64_content: &str,
) -> Result<Self>
pub fn from_base64( name: String, content_type: Option<String>, base64_content: &str, ) -> Result<Self>
Creates a new in-memory attachment from a base64 encoded string.
pub fn is_available(&self) -> bool
pub fn is_image(&self) -> bool
pub fn is_pdf(&self) -> bool
pub async fn read(&self) -> Result<Arc<[u8]>>
pub async fn read_base64(&self) -> Result<String>
Sourcepub fn content_type_or_octet_stream(&self) -> &str
pub fn content_type_or_octet_stream(&self) -> &str
Get the content type or “application/octet-stream” if not set.
Sourcepub fn get_persistence_key(&self) -> Option<&str>
pub fn get_persistence_key(&self) -> Option<&str>
Get the persistence key if set.
Sourcepub fn has_persistence_key(&self) -> bool
pub fn has_persistence_key(&self) -> bool
Check if the attachment has a persistence key set, indicating it was persisted.
Sourcepub fn has_persistence_reader(&self) -> bool
pub fn has_persistence_reader(&self) -> bool
Check if this attachment has a reader implementation set.
Sourcepub fn set_persistence_key(&mut self, key: String)
pub fn set_persistence_key(&mut self, key: String)
Give this attachment a custom persistence key.
This means you persisted this attachment somewhere and you will take care of how it’s read.
You should set this key only after you really persisted (wrote) the attachment.
If you call this, you should also call Self::set_persistence_reader
to configure how this attachment will be read using this key.
Sourcepub fn set_persistence_reader(
&mut self,
reader: impl Fn(&str) -> BoxPlatformSendFuture<'static, Result<Arc<[u8]>>> + Send + Sync + 'static,
)
pub fn set_persistence_reader( &mut self, reader: impl Fn(&str) -> BoxPlatformSendFuture<'static, Result<Arc<[u8]>>> + Send + Sync + 'static, )
Gives this attachment a custom implementation to read the persisted content.
Can only be used after setting a persistence key with Self::set_persistence_key.
Trait Implementations§
Source§impl Clone for Attachment
impl Clone for Attachment
Source§fn clone(&self) -> Attachment
fn clone(&self) -> Attachment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Attachment
impl Debug for Attachment
Source§impl Default for Attachment
impl Default for Attachment
Source§fn default() -> Attachment
fn default() -> Attachment
Source§impl<'de> Deserialize<'de> for Attachment
impl<'de> Deserialize<'de> for Attachment
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 Hash for Attachment
impl Hash for Attachment
Source§impl PartialEq for Attachment
impl PartialEq for Attachment
Source§impl Serialize for Attachment
impl Serialize for Attachment
impl Eq for Attachment
impl StructuralPartialEq for Attachment
Auto Trait Implementations§
impl Freeze for Attachment
impl !RefUnwindSafe for Attachment
impl Send for Attachment
impl Sync for Attachment
impl Unpin for Attachment
impl !UnwindSafe for Attachment
Blanket Implementations§
§impl<T> ActionTrait for T
impl<T> ActionTrait for T
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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.