intermodal/src/metainfo.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-24 10:25:55 +02:00
use crate::common::*;
#[derive(Deserialize, Serialize)]
pub struct Metainfo {
pub announce: String,
#[serde(rename = "announce-list")]
pub announce_list: Option<Vec<Vec<String>>>,
2019-05-24 10:25:55 +02:00
pub comment: Option<String>,
#[serde(rename = "created by")]
pub created_by: Option<String>,
#[serde(rename = "creation date")]
pub creation_date: Option<u64>,
pub encoding: Option<String>,
2019-05-24 10:25:55 +02:00
pub info: Info,
}
impl Metainfo {
pub(crate) fn _load(path: impl AsRef<Path>) -> Result<Metainfo, Error> {
let path = path.as_ref();
let bytes = fs::read(path).context(error::Filesystem { path })?;
Self::deserialize(path, &bytes)
}
pub(crate) fn dump(&self, path: impl AsRef<Path>) -> Result<(), Error> {
let path = path.as_ref();
let bytes = serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize)?;
fs::write(path, &bytes).context(error::Filesystem { path })?;
Ok(())
}
pub(crate) fn deserialize(path: impl AsRef<Path>, bytes: &[u8]) -> Result<Metainfo, Error> {
let path = path.as_ref();
serde_bencode::de::from_bytes(&bytes).context(error::MetainfoLoad { path })
}
pub(crate) fn serialize(&self) -> Result<Vec<u8>, Error> {
serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize)
}
}