include input string with every error

This commit is contained in:
Quaternions 2024-10-03 14:37:16 -07:00
parent ac48fb8a0d
commit a648526384

View File

@ -2,34 +2,47 @@
pub struct RobloxAssetId(pub u64); pub struct RobloxAssetId(pub u64);
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] #[allow(dead_code)]
pub struct StringWithError{
string:String,
error:RobloxAssetIdParseErr,
}
impl std::fmt::Display for StringWithError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for StringWithError{}
impl StringWithError{
const fn new(
string:String,
error:RobloxAssetIdParseErr,
)->Self{
Self{string,error}
}
}
#[derive(Debug)]
pub enum RobloxAssetIdParseErr{ pub enum RobloxAssetIdParseErr{
Url(url::ParseError), Url(url::ParseError),
UnknownScheme(String), UnknownScheme,
ParseInt(std::num::ParseIntError), ParseInt(std::num::ParseIntError),
MissingAssetId(String), MissingAssetId,
} }
impl std::str::FromStr for RobloxAssetId{ impl std::str::FromStr for RobloxAssetId{
type Err=RobloxAssetIdParseErr; type Err=StringWithError;
fn from_str(s:&str)->Result<Self,Self::Err>{ fn from_str(s:&str)->Result<Self,Self::Err>{
let url=url::Url::parse(s).map_err(RobloxAssetIdParseErr::Url)?; let url=url::Url::parse(s).map_err(|e|StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::Url(e)))?;
let parsed_asset_id=match url.scheme(){ let parsed_asset_id=match url.scheme(){
"rbxassetid"=>url.domain().ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?.parse(), "rbxassetid"=>url.domain().ok_or_else(||StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::MissingAssetId))?.parse(),
"http"|"https"=>{ "http"|"https"=>{
let (_,asset_id)=url.query_pairs() let (_,asset_id)=url.query_pairs()
.find(|(id,_)|match id.as_ref(){ .find(|(id,_)|match id.as_ref(){
"ID"|"id"|"Id"|"iD"=>true, "ID"|"id"|"Id"|"iD"=>true,
_=>false, _=>false,
}).ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?; }).ok_or_else(||StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::MissingAssetId))?;
asset_id.parse() asset_id.parse()
}, },
_=>Err(RobloxAssetIdParseErr::UnknownScheme(s.to_owned()))?, _=>Err(StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::UnknownScheme))?,
}; };
Ok(Self(parsed_asset_id.map_err(RobloxAssetIdParseErr::ParseInt)?)) Ok(Self(parsed_asset_id.map_err(|e|StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::ParseInt(e)))?))
} }
} }
impl std::fmt::Display for RobloxAssetIdParseErr{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for RobloxAssetIdParseErr{}