strafe-client/src/rbxassetid.rs

36 lines
1.1 KiB
Rust
Raw Normal View History

2024-10-01 20:00:54 +00:00
#[derive(Hash,Eq,PartialEq)]
pub struct RobloxAssetId(pub u64);
#[derive(Debug)]
#[allow(dead_code)]
pub enum RobloxAssetIdParseErr{
Url(url::ParseError),
UnknownScheme(String),
ParseInt(std::num::ParseIntError),
MissingAssetId(String),
}
impl std::str::FromStr for RobloxAssetId{
type Err=RobloxAssetIdParseErr;
fn from_str(s:&str)->Result<Self,Self::Err>{
let url=url::Url::parse(s).map_err(RobloxAssetIdParseErr::Url)?;
let parsed_asset_id=match url.scheme(){
"rbxassetid"=>url.domain().ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?.parse(),
"http"|"https"=>{
let (_,asset_id)=url.query_pairs()
.find(|(id,_)|match id.as_ref(){
"ID"|"id"|"Id"|"iD"=>true,
_=>false,
}).ok_or_else(||RobloxAssetIdParseErr::MissingAssetId(s.to_owned()))?;
2024-10-01 20:00:54 +00:00
asset_id.parse()
},
_=>Err(RobloxAssetIdParseErr::UnknownScheme(s.to_owned()))?,
};
Ok(Self(parsed_asset_id.map_err(RobloxAssetIdParseErr::ParseInt)?))
}
}
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{}