use std::{io::ErrorKind, time::Instant}; use serde::{Deserialize, Serialize}; use tokio::{fs::File, io::AsyncWriteExt}; use crate::Error; #[derive(Deserialize, Serialize, Debug)] pub struct Metadata { pub subject: String, pub nonce: String, pub etag: Option, pub size: Option, pub content_type: Option, pub expires_at: u64, // seconds since UNIX_EPOCH } impl Metadata { pub async fn from_file(path: &str) -> Result { let metadata = match tokio::fs::read_to_string(path).await { Ok(x) => Ok(x), Err(err) if err.kind() == ErrorKind::NotFound => Err(Error::BinNotFound), Err(x) => Err(x.into()), }?; Ok(toml::from_str::(&metadata)?) } pub async fn to_file(&self, path: &str) -> Result<(), Error> { let data = toml::to_string(&self)?; let mut file = File::create(path).await?; file.write_all(data.as_ref()).await?; Ok(()) } }