use std::convert::identity;
use crate::prelude::*;
#[cfg_attr(feature = "ser-de", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct CollectionList {
pub version: u32,
pub collections: Vec<Collection>,
}
impl CollectionList {
pub fn from_bytes(bytes: &[u8]) -> Result<CollectionList, Error> {
Ok(collections(bytes).map(|(_rem, collections)| collections)?)
}
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<CollectionList, Error> {
Self::from_bytes(&fs::read(path)?)
}
pub fn to_writer<W: Write>(&self, mut out: W) -> io::Result<()> {
self.wr(&mut out)
}
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.to_writer(BufWriter::new(File::create(path)?))
}
}
#[cfg_attr(feature = "ser-de", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct Collection {
pub name: Option<String>,
pub beatmap_hashes: Vec<Option<String>>,
}
fn collections(bytes: &[u8]) -> IResult<&[u8], CollectionList> {
let (rem, version) = int(bytes)?;
let (rem, collections) = length_count(map(int, identity), collection)(rem)?;
let list = CollectionList {
version,
collections,
};
Ok((rem, list))
}
fn collection(bytes: &[u8]) -> IResult<&[u8], Collection> {
let (rem, name) = opt_string(bytes)?;
let (rem, beatmap_hashes) = length_count(map(int, identity), opt_string)(rem)?;
let collection = Collection {
name,
beatmap_hashes,
};
Ok((rem, collection))
}
writer!(CollectionList [this,out] {
this.version.wr(out)?;
PrefixedList(&this.collections).wr(out)?;
});
writer!(Collection [this,out] {
this.name.wr(out)?;
PrefixedList(&this.beatmap_hashes).wr(out)?;
});