Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7121003

Browse files
dmArniDagur
dm
authored andcommitted
refactor: AddResourceException inheritance relationship && RustBlockerResult implementations
1 parent64c1fa2 commit7121003

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

‎adblock/__init__.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
OptimizedFilterExistence,
1212
BadFilterAddUnsupported,
1313
FilterExists,
14-
AddResourceError,
14+
AddResourceException,
15+
InvalidUtf8ContentError,
16+
InvalidBase64ContentError,
1517
)
1618

1719

@@ -27,5 +29,7 @@
2729
"OptimizedFilterExistence",
2830
"BadFilterAddUnsupported",
2931
"FilterExists",
30-
"AddResourceError",
32+
"AddResourceException",
33+
"InvalidUtf8ContentError",
34+
"InvalidBase64ContentError",
3135
)

‎adblock/adblock.pyi‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ class BadFilterAddUnsupported(BlockerException):
2121
classFilterExists(BlockerException):
2222
pass
2323

24-
classAddResourceError(BlockerException):
24+
classAddResourceException(AdblockException):
25+
pass
26+
27+
classInvalidUtf8ContentError(AddResourceException):
28+
pass
29+
30+
classInvalidBase64ContentError(AddResourceException):
2531
pass
2632

2733
classBlockerResult:

‎src/lib.rs‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> {
5858
py.get_type::<BadFilterAddUnsupported>(),
5959
)?;
6060
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+
)?;
6273
Ok(())
6374
}
6475

@@ -113,30 +124,23 @@ pub struct BlockerResult {
113124

114125
implFrom<RustBlockerResult>forBlockerResult{
115126
fnfrom(br:RustBlockerResult) ->Self{
116-
letmut redirect:Option<String> =None;
117-
letmut redirect_type:Option<String> =None;
118-
if br.redirect.is_some(){
119-
let resource = br.redirect.unwrap();
120-
redirect =Option::from(match resource{
121-
Redirection::Resource(resource) =>{
122-
redirect_type =Some("resource".to_string());
123-
resource
124-
}
125-
Redirection::Url(url) =>{
126-
redirect_type =Some("url".to_string());
127-
url
128-
}
129-
});
130-
}
127+
let(redirect, redirect_type) =ifletSome(resource) = br.redirect{
128+
match resource{
129+
Redirection::Resource(resource) =>(Some(resource),Some("resource".to_string())),
130+
Redirection::Url(url) =>(Some(url),Some("url".to_string())),
131+
}
132+
}else{
133+
(None,None)
134+
};
131135

132136
Self{
133137
matched: br.matched,
134138
important: br.important,
135139
exception: br.exception,
136140
filter: br.filter,
137141
error: br.error,
138-
redirect_type: redirect_type,
139-
redirect: redirect,
142+
redirect_type,
143+
redirect,
140144
}
141145
}
142146
}
@@ -189,12 +193,14 @@ impl Display for BlockerError {
189193

190194
create_exception!(adblock,AdblockException,PyException);
191195
create_exception!(adblock,BlockerException,AdblockException);
196+
create_exception!(adblock,AddResourceException,AdblockException);
197+
create_exception!(adblock,InvalidBase64ContentError,AddResourceException);
198+
create_exception!(adblock,InvalidUtf8ContentError,AddResourceException);
192199
create_exception!(adblock,SerializationError,BlockerException);
193200
create_exception!(adblock,DeserializationError,BlockerException);
194201
create_exception!(adblock,OptimizedFilterExistence,BlockerException);
195202
create_exception!(adblock,BadFilterAddUnsupported,BlockerException);
196203
create_exception!(adblock,FilterExists,BlockerException);
197-
create_exception!(adblock,AddResourceError,BlockerException);
198204

199205
implFrom<BlockerError>forPyErr{
200206
fnfrom(err:BlockerError) ->Self{
@@ -517,12 +523,12 @@ impl Engine {
517523
match result{
518524
Ok(_) =>Ok(()),
519525
Err(err) =>match err{
520-
RustAddResourceError::InvalidBase64Content =>Err(AddResourceError::new_err(
521-
"invalid base64 content".to_string(),
526+
RustAddResourceError::InvalidBase64Content =>Err(
527+
InvalidBase64ContentError::new_err("invalid base64 content".to_string()),
528+
),
529+
RustAddResourceError::InvalidUtf8Content =>Err(InvalidUtf8ContentError::new_err(
530+
"invalid utf content".to_string(),
522531
)),
523-
RustAddResourceError::InvalidUtf8Content =>{
524-
Err(AddResourceError::new_err("invalid utf content".to_string()))
525-
}
526532
},
527533
}
528534
}

‎tests/test_exceptions.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
deftest_correct_baseclasses():
66
assertissubclass(adblock.AdblockException,Exception)
77
assertissubclass(adblock.BlockerException,adblock.AdblockException)
8+
assertissubclass(adblock.AddResourceException,adblock.AdblockException)
9+
assertissubclass(adblock.InvalidUtf8ContentError,adblock.AddResourceException)
10+
assertissubclass(adblock.InvalidBase64ContentError,adblock.AddResourceException)
811
assertissubclass(adblock.SerializationError,adblock.BlockerException)
912
assertissubclass(adblock.DeserializationError,adblock.BlockerException)
1013
assertissubclass(adblock.OptimizedFilterExistence,adblock.BlockerException)
1114
assertissubclass(adblock.BadFilterAddUnsupported,adblock.BlockerException)
1215
assertissubclass(adblock.FilterExists,adblock.BlockerException)
13-
assertissubclass(adblock.AddResourceError,adblock.BlockerException)
1416

1517

1618
deftest_add_resource_error():
1719
filter_set=adblock.FilterSet()
1820
engine=adblock.Engine(filter_set=filter_set)
1921

20-
withpytest.raises(adblock.AddResourceError)asexc:
22+
withpytest.raises(adblock.InvalidBase64ContentError)asexc:
2123
engine.add_resource(name="aa",content_type="image/jpeg",content="111")
22-
2324
assert"invalid base64 content"instr(exc.value)
25+
26+
withpytest.raises(adblock.InvalidUtf8ContentError)asexc:
27+
# // Ensure any text contents are also valid utf8
28+
# MimeType::ApplicationJavascript | MimeType::TextPlain | MimeType::TextHtml => {
29+
# let _ = String::from_utf8(decoded)?;
30+
# }
31+
# xOO6ww== => base64.b64encode('你好'.encode('gbk'))
32+
engine.add_resource(
33+
name="aa",content_type="application/javascript",content="xOO6ww=="
34+
)
35+
assert"invalid utf content"instr(exc.value)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp