use std::os::fd::BorrowedFd;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{Fd, Type};
use crate::{error::PortalError, proxy::Proxy, Error};
#[derive(Debug, Deserialize_repr, Serialize_repr, PartialEq, Type)]
#[repr(u32)]
enum TrashStatus {
Failed = 0,
Succeeded = 1,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Trash")]
pub struct TrashProxy<'a>(Proxy<'a>);
impl<'a> TrashProxy<'a> {
pub async fn new() -> Result<TrashProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Trash").await?;
Ok(Self(proxy))
}
#[doc(alias = "TrashFile")]
#[doc(alias = "xdp_portal_trash_file")]
pub async fn trash_file(&self, fd: &BorrowedFd<'_>) -> Result<(), Error> {
let status = self.0.call("TrashFile", &(Fd::from(fd))).await?;
match status {
TrashStatus::Failed => Err(Error::Portal(PortalError::Failed(
"Failed to trash file".to_string(),
))),
TrashStatus::Succeeded => Ok(()),
}
}
}
impl<'a> std::ops::Deref for TrashProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[doc(alias = "xdp_portal_trash_file")]
pub async fn trash_file(fd: &BorrowedFd<'_>) -> Result<(), Error> {
let proxy = TrashProxy::new().await?;
proxy.trash_file(fd).await
}
#[cfg(test)]
mod test {
use super::TrashStatus;
#[test]
fn status_serde() {
#[derive(serde::Serialize, serde::Deserialize)]
struct Test {
status: TrashStatus,
}
let status = Test {
status: TrashStatus::Failed,
};
let x = serde_json::to_string(&status).unwrap();
let y: Test = serde_json::from_str(&x).unwrap();
assert_eq!(y.status, TrashStatus::Failed);
}
}