Attachment

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: String

Normally the original filename.

§content_type: Option<String>

Mime type of the content, if known.

Implementations§

§

impl Attachment

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 spawn may run in a separate thread. So we can’t generalize.
  • We follow macos requirements on all native platforms just in case.

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>

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)

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

Get the content type or “application/octet-stream” if not set.

pub fn get_persistence_key(&self) -> Option<&str>

Get the persistence key if set.

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

Check if this attachment has a reader implementation set.

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, )

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

§

fn clone(&self) -> Attachment

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Attachment

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Attachment

§

fn default() -> Attachment

Returns the “default value” for a type. Read more
§

impl<'de> Deserialize<'de> for Attachment

§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Attachment, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
§

impl Hash for Attachment

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl PartialEq for Attachment

§

fn eq(&self, other: &Attachment) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Serialize for Attachment

§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl Eq for Attachment

§

impl StructuralPartialEq for Attachment

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> ActionTrait for T
where T: 'static + Debug + ?Sized,

§

fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

§

fn ref_cast_type_id(&self) -> TypeId
where Self: 'static,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WidgetActionTrait for T
where T: 'static + Clone + Debug + Send + Sync + ?Sized,

§

fn ref_cast_type_id(&self) -> TypeId

§

fn box_clone(&self) -> Box<dyn WidgetActionTrait>

§

fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> PlatformSend for T
where T: PlatformSendInner,

§

impl<T> PlatformSendInner for T
where T: Send,