Struct Attachment

Source
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§

Source§

impl Attachment

Source

pub fn from_bytes( name: String, content_type: Option<String>, content: &[u8], ) -> Self

Creates a new in-memory attachment from the given bytes.

Source

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.

Source

pub fn is_available(&self) -> bool

Source

pub fn is_image(&self) -> bool

Source

pub fn is_pdf(&self) -> bool

Source

pub async fn read(&self) -> Result<Arc<[u8]>>

Source

pub async fn read_base64(&self) -> Result<String>

Source

pub fn content_type_or_octet_stream(&self) -> &str

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

Source

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

Get the persistence key if set.

Source

pub fn has_persistence_key(&self) -> bool

Check if the attachment has a persistence key set, indicating it was persisted.

Source

pub fn has_persistence_reader(&self) -> bool

Check if this attachment has a reader implementation set.

Source

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.

Source

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

Source§

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
Source§

impl Debug for Attachment

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Attachment

Source§

fn default() -> Attachment

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

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

Source§

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

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

impl Hash for Attachment

Source§

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

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
Source§

impl PartialEq for Attachment

Source§

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.
Source§

impl Serialize for Attachment

Source§

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

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

impl Eq for Attachment

Source§

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
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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> PlatformSend for T

Source§

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