From a6485263841115cbc1044a312a6b4a8642bbb72a Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 14:37:16 -0700 Subject: [PATCH] include input string with every error --- src/rbxassetid.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/rbxassetid.rs b/src/rbxassetid.rs index 53eb682..2cf43d7 100644 --- a/src/rbxassetid.rs +++ b/src/rbxassetid.rs @@ -2,34 +2,47 @@ pub struct RobloxAssetId(pub u64); #[derive(Debug)] #[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{ Url(url::ParseError), - UnknownScheme(String), + UnknownScheme, ParseInt(std::num::ParseIntError), - MissingAssetId(String), + MissingAssetId, } impl std::str::FromStr for RobloxAssetId{ - type Err=RobloxAssetIdParseErr; + type Err=StringWithError; fn from_str(s:&str)->Result{ - 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(){ - "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"=>{ 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()))?; + }).ok_or_else(||StringWithError::new(s.to_owned(),RobloxAssetIdParseErr::MissingAssetId))?; 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{}