wrapped types + implement error trait

This commit is contained in:
Quaternions 2024-02-13 21:27:28 -08:00
parent 1c33646765
commit 0b630576d4

View File

@ -3,24 +3,34 @@ use std::io::Read;
mod rbx;
mod primitives;
pub struct Dom(rbx_dom_weak::WeakDom);
#[derive(Debug)]
pub enum Error{
pub enum ReadError{
RbxBinary(rbx_binary::DecodeError),
RbxXml(rbx_xml::DecodeError),
Io(std::io::Error),
UnknownFileFormat,
}
impl std::fmt::Display for ReadError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for ReadError{}
fn load_dom<R:Read>(input:R)->Result<rbx_dom_weak::WeakDom,Error>{
pub fn read<R:Read>(input:R)->Result<Dom,ReadError>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(Error::Io)?;
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?;
match &peek[0..8]{
b"<roblox!"=>return rbx_binary::from_reader(buf).map_err(Error::RbxBinary),
b"<roblox "=>return rbx_xml::from_reader_default(buf).map_err(Error::RbxXml),
_=>Err(Error::UnknownFileFormat),
b"<roblox!"=>rbx_binary::from_reader(buf).map(Dom).map_err(ReadError::RbxBinary),
b"<roblox "=>rbx_xml::from_reader_default(buf).map(Dom).map_err(ReadError::RbxXml),
_=>Err(ReadError::UnknownFileFormat),
}
}
pub fn read<R:Read,F:FnMut(&str)->Option<strafesnet_common::model::TextureId>>(input:R,acquire_id:F)->Result<strafesnet_common::map::CompleteMap,Error>{
Ok(rbx::convert(load_dom(input)?,acquire_id))
//ConvertError
pub fn convert<F:FnMut(&str)->Option<strafesnet_common::model::TextureId>>(dom:&Dom,acquire_id:F)->strafesnet_common::map::CompleteMap{
rbx::convert(&dom.0,acquire_id)
}