summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordm <dm@work-pc>2022-02-18 11:05:22 +0800
committerÁrni Dagur <arni@dagur.eu>2022-02-26 22:06:53 +0100
commit71210038c16fefbf741d573a29334db08271b4b6 (patch)
tree5e8f99474fd5b0ff5944d5e7b9725a8865f20935 /src
parent64c1fa2c91b55a35eceeffab6e0715374a5a7231 (diff)
refactor: AddResourceException inheritance relationship && RustBlockerResult implementations
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs54
1 files changed, 30 insertions, 24 deletions
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<()> {
58 py.get_type::<BadFilterAddUnsupported>(), 58 py.get_type::<BadFilterAddUnsupported>(),
59 )?; 59 )?;
60 m.add("FilterExists", py.get_type::<FilterExists>())?; 60 m.add("FilterExists", py.get_type::<FilterExists>())?;
61 m.add("AddResourceError", py.get_type::<AddResourceError>())?; 61 m.add(
62 "AddResourceException",
63 py.get_type::<AddResourceException>(),
64 )?;
65 m.add(
66 "InvalidBase64ContentError",
67 py.get_type::<InvalidBase64ContentError>(),
68 )?;
69 m.add(
70 "InvalidUtf8ContentError",
71 py.get_type::<InvalidUtf8ContentError>(),
72 )?;
62 Ok(()) 73 Ok(())
63} 74}
64 75
@@ -113,21 +124,14 @@ pub struct BlockerResult {
113 124
114impl From<RustBlockerResult> for BlockerResult { 125impl From<RustBlockerResult> for BlockerResult {
115 fn from(br: RustBlockerResult) -> Self { 126 fn from(br: RustBlockerResult) -> Self {
116 let mut redirect: Option<String> = None; 127 let (redirect, redirect_type) = if let Some(resource) = br.redirect {
117 let mut redirect_type: Option<String> = None; 128 match resource {
118 if br.redirect.is_some() { 129 Redirection::Resource(resource) => (Some(resource), Some("resource".to_string())),
119 let resource = br.redirect.unwrap(); 130 Redirection::Url(url) => (Some(url), Some("url".to_string())),
120 redirect = Option::from(match resource { 131 }
121 Redirection::Resource(resource) => { 132 } else {
122 redirect_type = Some("resource".to_string()); 133 (None, None)
123 resource 134 };
124 }
125 Redirection::Url(url) => {
126 redirect_type = Some("url".to_string());
127 url
128 }
129 });
130 }
131 135
132 Self { 136 Self {
133 matched: br.matched, 137 matched: br.matched,
@@ -135,8 +139,8 @@ impl From<RustBlockerResult> for BlockerResult {
135 exception: br.exception, 139 exception: br.exception,
136 filter: br.filter, 140 filter: br.filter,
137 error: br.error, 141 error: br.error,
138 redirect_type: redirect_type, 142 redirect_type,
139 redirect: redirect, 143 redirect,
140 } 144 }
141 } 145 }
142} 146}
@@ -189,12 +193,14 @@ impl Display for BlockerError {
189 193
190create_exception!(adblock, AdblockException, PyException); 194create_exception!(adblock, AdblockException, PyException);
191create_exception!(adblock, BlockerException, AdblockException); 195create_exception!(adblock, BlockerException, AdblockException);
196create_exception!(adblock, AddResourceException, AdblockException);
197create_exception!(adblock, InvalidBase64ContentError, AddResourceException);
198create_exception!(adblock, InvalidUtf8ContentError, AddResourceException);
192create_exception!(adblock, SerializationError, BlockerException); 199create_exception!(adblock, SerializationError, BlockerException);
193create_exception!(adblock, DeserializationError, BlockerException); 200create_exception!(adblock, DeserializationError, BlockerException);
194create_exception!(adblock, OptimizedFilterExistence, BlockerException); 201create_exception!(adblock, OptimizedFilterExistence, BlockerException);
195create_exception!(adblock, BadFilterAddUnsupported, BlockerException); 202create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
196create_exception!(adblock, FilterExists, BlockerException); 203create_exception!(adblock, FilterExists, BlockerException);
197create_exception!(adblock, AddResourceError, BlockerException);
198 204
199impl From<BlockerError> for PyErr { 205impl From<BlockerError> for PyErr {
200 fn from(err: BlockerError) -> Self { 206 fn from(err: BlockerError) -> Self {
@@ -517,12 +523,12 @@ impl Engine {
517 match result { 523 match result {
518 Ok(_) => Ok(()), 524 Ok(_) => Ok(()),
519 Err(err) => match err { 525 Err(err) => match err {
520 RustAddResourceError::InvalidBase64Content => Err(AddResourceError::new_err( 526 RustAddResourceError::InvalidBase64Content => Err(
521 "invalid base64 content".to_string(), 527 InvalidBase64ContentError::new_err("invalid base64 content".to_string()),
528 ),
529 RustAddResourceError::InvalidUtf8Content => Err(InvalidUtf8ContentError::new_err(
530 "invalid utf content".to_string(),
522 )), 531 )),
523 RustAddResourceError::InvalidUtf8Content => {
524 Err(AddResourceError::new_err("invalid utf content".to_string()))
525 }
526 }, 532 },
527 } 533 }
528 } 534 }