From 71210038c16fefbf741d573a29334db08271b4b6 Mon Sep 17 00:00:00 2001 From: dm Date: Fri, 18 Feb 2022 11:05:22 +0800 Subject: refactor: AddResourceException inheritance relationship && RustBlockerResult implementations --- adblock/__init__.py | 8 +++++-- adblock/adblock.pyi | 8 ++++++- src/lib.rs | 54 +++++++++++++++++++++++++++--------------------- tests/test_exceptions.py | 18 +++++++++++++--- 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/adblock/__init__.py b/adblock/__init__.py index 4ba4c4b..b231aeb 100644 --- a/adblock/__init__.py +++ b/adblock/__init__.py @@ -11,7 +11,9 @@ from adblock.adblock import ( OptimizedFilterExistence, BadFilterAddUnsupported, FilterExists, - AddResourceError, + AddResourceException, + InvalidUtf8ContentError, + InvalidBase64ContentError, ) @@ -27,5 +29,7 @@ __all__ = ( "OptimizedFilterExistence", "BadFilterAddUnsupported", "FilterExists", - "AddResourceError", + "AddResourceException", + "InvalidUtf8ContentError", + "InvalidBase64ContentError", ) diff --git a/adblock/adblock.pyi b/adblock/adblock.pyi index 4ed340c..9fae1da 100644 --- a/adblock/adblock.pyi +++ b/adblock/adblock.pyi @@ -21,7 +21,13 @@ class BadFilterAddUnsupported(BlockerException): class FilterExists(BlockerException): pass -class AddResourceError(BlockerException): +class AddResourceException(AdblockException): + pass + +class InvalidUtf8ContentError(AddResourceException): + pass + +class InvalidBase64ContentError(AddResourceException): pass class BlockerResult: diff --git a/src/lib.rs b/src/lib.rs index 5fba7dd..9117254 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,18 @@ fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> { py.get_type::(), )?; m.add("FilterExists", py.get_type::())?; - m.add("AddResourceError", py.get_type::())?; + m.add( + "AddResourceException", + py.get_type::(), + )?; + m.add( + "InvalidBase64ContentError", + py.get_type::(), + )?; + m.add( + "InvalidUtf8ContentError", + py.get_type::(), + )?; Ok(()) } @@ -113,21 +124,14 @@ pub struct BlockerResult { impl From for BlockerResult { fn from(br: RustBlockerResult) -> Self { - let mut redirect: Option = None; - let mut redirect_type: Option = None; - if br.redirect.is_some() { - let resource = br.redirect.unwrap(); - redirect = Option::from(match resource { - Redirection::Resource(resource) => { - redirect_type = Some("resource".to_string()); - resource - } - Redirection::Url(url) => { - redirect_type = Some("url".to_string()); - url - } - }); - } + let (redirect, redirect_type) = if let Some(resource) = br.redirect { + match resource { + Redirection::Resource(resource) => (Some(resource), Some("resource".to_string())), + Redirection::Url(url) => (Some(url), Some("url".to_string())), + } + } else { + (None, None) + }; Self { matched: br.matched, @@ -135,8 +139,8 @@ impl From for BlockerResult { exception: br.exception, filter: br.filter, error: br.error, - redirect_type: redirect_type, - redirect: redirect, + redirect_type, + redirect, } } } @@ -189,12 +193,14 @@ impl Display for BlockerError { create_exception!(adblock, AdblockException, PyException); create_exception!(adblock, BlockerException, AdblockException); +create_exception!(adblock, AddResourceException, AdblockException); +create_exception!(adblock, InvalidBase64ContentError, AddResourceException); +create_exception!(adblock, InvalidUtf8ContentError, AddResourceException); create_exception!(adblock, SerializationError, BlockerException); create_exception!(adblock, DeserializationError, BlockerException); create_exception!(adblock, OptimizedFilterExistence, BlockerException); create_exception!(adblock, BadFilterAddUnsupported, BlockerException); create_exception!(adblock, FilterExists, BlockerException); -create_exception!(adblock, AddResourceError, BlockerException); impl From for PyErr { fn from(err: BlockerError) -> Self { @@ -517,12 +523,12 @@ impl Engine { match result { Ok(_) => Ok(()), Err(err) => match err { - RustAddResourceError::InvalidBase64Content => Err(AddResourceError::new_err( - "invalid base64 content".to_string(), + RustAddResourceError::InvalidBase64Content => Err( + InvalidBase64ContentError::new_err("invalid base64 content".to_string()), + ), + RustAddResourceError::InvalidUtf8Content => Err(InvalidUtf8ContentError::new_err( + "invalid utf content".to_string(), )), - RustAddResourceError::InvalidUtf8Content => { - Err(AddResourceError::new_err("invalid utf content".to_string())) - } }, } } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 2efe2b2..e1c357c 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -5,19 +5,31 @@ import pytest def test_correct_baseclasses(): assert issubclass(adblock.AdblockException, Exception) assert issubclass(adblock.BlockerException, adblock.AdblockException) + assert issubclass(adblock.AddResourceException, adblock.AdblockException) + assert issubclass(adblock.InvalidUtf8ContentError, adblock.AddResourceException) + assert issubclass(adblock.InvalidBase64ContentError, adblock.AddResourceException) assert issubclass(adblock.SerializationError, adblock.BlockerException) assert issubclass(adblock.DeserializationError, adblock.BlockerException) assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException) assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException) assert issubclass(adblock.FilterExists, adblock.BlockerException) - assert issubclass(adblock.AddResourceError, adblock.BlockerException) def test_add_resource_error(): filter_set = adblock.FilterSet() engine = adblock.Engine(filter_set=filter_set) - with pytest.raises(adblock.AddResourceError) as exc: + with pytest.raises(adblock.InvalidBase64ContentError) as exc: engine.add_resource(name="aa", content_type="image/jpeg", content="111") - assert "invalid base64 content" in str(exc.value) + + with pytest.raises(adblock.InvalidUtf8ContentError) as exc: + # // Ensure any text contents are also valid utf8 + # MimeType::ApplicationJavascript | MimeType::TextPlain | MimeType::TextHtml => { + # let _ = String::from_utf8(decoded)?; + # } + # xOO6ww== => base64.b64encode('你好'.encode('gbk')) + engine.add_resource( + name="aa", content_type="application/javascript", content="xOO6ww==" + ) + assert "invalid utf content" in str(exc.value) -- cgit v1.2.3