strafe-client/lib/deferred_loader/src/rbxassetid.rs

49 lines
1.5 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)]
2024-10-03 21:37:16 +00:00
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)]
2024-10-01 20:00:54 +00:00
pub enum RobloxAssetIdParseErr{
Url(url::ParseError),
2024-10-03 21:37:16 +00:00
UnknownScheme,
2024-10-01 20:00:54 +00:00
ParseInt(std::num::ParseIntError),
2024-10-03 21:37:16 +00:00
MissingAssetId,
2024-10-01 20:00:54 +00:00
}
impl std::str::FromStr for RobloxAssetId{
2024-10-03 21:37:16 +00:00
type Err=StringWithError;
2024-10-01 20:00:54 +00:00
fn from_str(s:&str)->Result<Self,Self::Err>{
2024-10-03 21:37:16 +00:00
let url=url::Url::parse(s).map_err(|e|StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::Url(e)))?;
2024-10-01 20:00:54 +00:00
let parsed_asset_id=match url.scheme(){
2024-10-03 21:37:16 +00:00
"rbxassetid"=>url.domain().ok_or_else(||StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::MissingAssetId))?.parse(),
2024-10-01 20:00:54 +00:00
"http"|"https"=>{
let (_,asset_id)=url.query_pairs()
.find(|(id,_)|match id.as_ref(){
"ID"|"id"|"Id"|"iD"=>true,
_=>false,
2024-10-03 21:37:16 +00:00
}).ok_or_else(||StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::MissingAssetId))?;
2024-10-01 20:00:54 +00:00
asset_id.parse()
},
2024-10-03 21:37:16 +00:00
_=>Err(StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::UnknownScheme))?,
2024-10-01 20:00:54 +00:00
};
2024-10-03 21:37:16 +00:00
Ok(Self(parsed_asset_id.map_err(|e|StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::ParseInt(e)))?))
2024-10-01 20:00:54 +00:00
}
}