Struct Attachment
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§
§impl Attachment
impl Attachment
pub fn pick_multiple(cb: impl FnOnce(Result<Vec<Attachment>, ()>) + 'static)
pub fn pick_multiple(cb: impl FnOnce(Result<Vec<Attachment>, ()>) + 'static)
Pick files from the file system using a system dialog.
- On web, async API is required to pick files.
- On macos, sync API is required and must be called from the main UI thread.
- This is the reason why it takes a closure instead of returning a Future.
Because on native
spawnmay run in a separate thread. So we can’t generalize.
- This is the reason why it takes a closure instead of returning a Future.
Because on native
- We follow macos requirements on all native platforms just in case.
pub fn from_bytes(
name: String,
content_type: Option<String>,
content: &[u8],
) -> Attachment
pub fn from_bytes( name: String, content_type: Option<String>, content: &[u8], ) -> Attachment
Creates a new in-memory attachment from the given bytes.
pub fn from_base64(
name: String,
content_type: Option<String>,
base64_content: &str,
) -> Result<Attachment, Error>
pub fn from_base64( name: String, content_type: Option<String>, base64_content: &str, ) -> Result<Attachment, Error>
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]>, Error>
pub async fn read_base64(&self) -> Result<String, Error>
pub fn save(&self)
pub fn save(&self)
Crate private utility to save/download the attachment to the file system.
- On web, this triggers a download.
- On native platforms, this opens a “Save As” dialog.
pub 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.
pub fn get_persistence_key(&self) -> Option<&str>
pub fn get_persistence_key(&self) -> Option<&str>
Get the persistence key if set.
pub 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.
pub fn has_persistence_reader(&self) -> bool
pub fn has_persistence_reader(&self) -> bool
Check if this attachment has a reader implementation set.
pub 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.
pub fn set_persistence_reader(
&mut self,
reader: impl Fn(&str) -> Pin<Box<dyn PlatformSendFuture<Output = Result<Arc<[u8]>, Error>>>> + Send + Sync + 'static,
)
pub fn set_persistence_reader( &mut self, reader: impl Fn(&str) -> Pin<Box<dyn PlatformSendFuture<Output = Result<Arc<[u8]>, Error>>>> + 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§
§impl Clone for Attachment
impl Clone for Attachment
§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 more§impl Debug for Attachment
impl Debug for Attachment
§impl Default for Attachment
impl Default for Attachment
§fn default() -> Attachment
fn default() -> Attachment
§impl<'de> Deserialize<'de> for Attachment
impl<'de> Deserialize<'de> for Attachment
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Attachment, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Attachment, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl Hash for Attachment
impl Hash for Attachment
§impl PartialEq for Attachment
impl PartialEq for Attachment
§impl Serialize for Attachment
impl Serialize for Attachment
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§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.