use crate::common::*; #[derive(Deserialize, Serialize)] pub struct Metainfo { pub announce: String, #[serde(rename = "announce-list")] pub announce_list: Option>>, pub comment: Option, #[serde(rename = "created by")] pub created_by: Option, #[serde(rename = "creation date")] pub creation_date: Option, pub encoding: Option, pub info: Info, } impl Metainfo { pub(crate) fn _load(path: impl AsRef) -> Result { 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) -> 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, bytes: &[u8]) -> Result { let path = path.as_ref(); serde_bencode::de::from_bytes(&bytes).context(error::MetainfoLoad { path }) } pub(crate) fn serialize(&self) -> Result, Error> { serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize) } }