use crate::AttachmentId;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio::io::{AsyncRead, AsyncWrite};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct AttachmentInfo {
id: AttachmentId,
name: String,
size: u64,
content_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
keywords: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachmentInfosResponse {
pub attachment_infos: Vec<AttachmentInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct AddAttachmentResult {
#[serde(default)]
object_id: Option<crate::ObjectId>,
#[serde(skip_serializing_if = "Option::is_none")]
global_id: Option<String>,
success: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AddAttachmentResponse {
pub(crate) add_attachment_result: AddAttachmentResult,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct UpdateAttachmentResult {
object_id: crate::ObjectId,
success: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct UpdateAttachmentResponse {
pub(crate) update_attachment_result: UpdateAttachmentResult,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteAttachmentsResponse {
pub delete_attachment_results: Vec<DeleteAttachmentResult>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct DeleteAttachmentResult {
object_id: crate::ObjectId,
#[serde(skip_serializing_if = "Option::is_none")]
global_id: Option<String>,
success: bool,
}
pub enum AttachmentSource {
Path(PathBuf),
Bytes {
filename: String,
data: Vec<u8>,
content_type: Option<String>,
},
Stream {
filename: String,
reader: Box<dyn AsyncRead + Send + Sync + Unpin>,
content_type: String,
size: Option<u64>,
},
}
impl AttachmentSource {
pub fn from_path(path: impl Into<PathBuf>) -> Self {
Self::Path(path.into())
}
pub fn from_bytes(filename: impl Into<String>, data: Vec<u8>) -> Self {
Self::Bytes {
filename: filename.into(),
data,
content_type: None,
}
}
pub fn from_bytes_with_type(
filename: impl Into<String>,
data: Vec<u8>,
content_type: impl Into<String>,
) -> Self {
Self::Bytes {
filename: filename.into(),
data,
content_type: Some(content_type.into()),
}
}
pub fn from_stream(
filename: impl Into<String>,
reader: Box<dyn AsyncRead + Send + Sync + Unpin>,
content_type: impl Into<String>,
size: Option<u64>,
) -> Self {
Self::Stream {
filename: filename.into(),
reader,
content_type: content_type.into(),
size,
}
}
}
pub enum DownloadTarget {
Path(PathBuf),
Bytes,
Writer(Box<dyn AsyncWrite + Send + Sync + Unpin>),
}
impl DownloadTarget {
pub fn to_path(path: impl Into<PathBuf>) -> Self {
Self::Path(path.into())
}
pub fn to_bytes() -> Self {
Self::Bytes
}
pub fn to_writer(writer: Box<dyn AsyncWrite + Send + Sync + Unpin>) -> Self {
Self::Writer(writer)
}
}
#[derive(Debug)]
pub enum DownloadResult {
Path(PathBuf),
Bytes(Vec<u8>),
Written(u64),
}
impl DownloadResult {
pub fn path(&self) -> Option<&PathBuf> {
match self {
Self::Path(p) => Some(p),
_ => None,
}
}
pub fn bytes(&self) -> Option<&[u8]> {
match self {
Self::Bytes(b) => Some(b),
_ => None,
}
}
pub fn written(&self) -> Option<u64> {
match self {
Self::Written(n) => Some(*n),
_ => None,
}
}
pub fn into_bytes(self) -> Option<Vec<u8>> {
match self {
Self::Bytes(b) => Some(b),
_ => None,
}
}
}