use std::os::fd::{AsFd, BorrowedFd};
#[cfg(feature = "async-std")]
use async_net::unix::UnixStream;
#[cfg(feature = "async-std")]
use futures_util::AsyncReadExt;
#[cfg(feature = "tokio")]
use tokio::{io::AsyncReadExt, net::UnixStream};
use zbus::zvariant::{Fd, SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct RetrieveOptions {
handle_token: HandleToken,
token: Option<String>,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Secret")]
pub struct Secret<'a>(Proxy<'a>);
impl<'a> Secret<'a> {
pub async fn new() -> Result<Secret<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Secret").await?;
Ok(Self(proxy))
}
#[doc(alias = "RetrieveSecret")]
pub async fn retrieve(&self, fd: &BorrowedFd<'_>) -> Result<Request<()>, Error> {
let options = RetrieveOptions::default();
self.0
.empty_request(
&options.handle_token,
"RetrieveSecret",
&(Fd::from(fd), &options),
)
.await
}
}
impl<'a> std::ops::Deref for Secret<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub async fn retrieve() -> Result<Vec<u8>, Error> {
let proxy = Secret::new().await?;
let (mut x1, x2) = UnixStream::pair()?;
proxy.retrieve(&x2.as_fd()).await?;
drop(x2);
let mut buf = Vec::new();
x1.read_to_end(&mut buf).await?;
Ok(buf)
}