33 lines
998 B
Rust
33 lines
998 B
Rust
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<String>,
|
|
pub size: Option<u64>,
|
|
pub content_type: Option<String>,
|
|
pub expires_at: u64, // seconds since UNIX_EPOCH
|
|
}
|
|
|
|
impl Metadata {
|
|
pub async fn from_file(path: &str) -> Result<Self, Error> {
|
|
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::<Self>(&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(())
|
|
}
|
|
}
|