2019-05-24 10:25:55 +02:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct Metainfo {
|
|
|
|
pub announce: String,
|
2020-02-04 16:55:50 +01:00
|
|
|
#[serde(rename = "announce-list")]
|
2020-01-24 23:17:31 +01:00
|
|
|
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>,
|
2020-02-04 16:55:50 +01:00
|
|
|
pub encoding: Option<String>,
|
2019-05-24 10:25:55 +02:00
|
|
|
pub info: Info,
|
|
|
|
}
|
2020-02-04 16:55:50 +01:00
|
|
|
|
|
|
|
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 })
|
|
|
|
}
|
2020-02-04 17:36:00 +01:00
|
|
|
|
|
|
|
pub(crate) fn serialize(&self) -> Result<Vec<u8>, Error> {
|
|
|
|
serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize)
|
|
|
|
}
|
2020-02-04 16:55:50 +01:00
|
|
|
}
|