summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorÁrni Dagur <arni@dagur.eu>2021-06-26 16:20:29 +0000
committerÁrni Dagur <arni@dagur.eu>2021-06-26 19:10:50 +0000
commitfb1832feb868a81e2d10a1707c57df4623304313 (patch)
treef77f31d340f19b0e4d96a92ccc23cdeb6b135738 /src
parent0d5c8b8cfa1025c29b96db3c52ff54a7d204923f (diff)
Create a custom exception type for this library
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 54031dd..172f025 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -17,7 +17,8 @@ use adblock::engine::Engine as RustEngine;
17use adblock::lists::FilterFormat; 17use adblock::lists::FilterFormat;
18use adblock::lists::FilterSet as RustFilterSet; 18use adblock::lists::FilterSet as RustFilterSet;
19use pyo3::class::PyObjectProtocol; 19use pyo3::class::PyObjectProtocol;
20use pyo3::exceptions::PyValueError; 20use pyo3::create_exception;
21use pyo3::exceptions::PyException;
21use pyo3::prelude::*; 22use pyo3::prelude::*;
22use pyo3::types::PyBytes; 23use pyo3::types::PyBytes;
23use pyo3::PyErr; 24use pyo3::PyErr;
@@ -31,12 +32,28 @@ use std::io::{Read, Write};
31 32
32/// Brave's adblocking library in Python! 33/// Brave's adblocking library in Python!
33#[pymodule] 34#[pymodule]
34fn adblock(_py: Python<'_>, m: &PyModule) -> PyResult<()> { 35fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> {
35 m.add("__version__", env!("CARGO_PKG_VERSION"))?; 36 m.add("__version__", env!("CARGO_PKG_VERSION"))?;
36 m.add_class::<Engine>()?; 37 m.add_class::<Engine>()?;
37 m.add_class::<FilterSet>()?; 38 m.add_class::<FilterSet>()?;
38 m.add_class::<BlockerResult>()?; 39 m.add_class::<BlockerResult>()?;
39 m.add_class::<UrlSpecificResources>()?; 40 m.add_class::<UrlSpecificResources>()?;
41 m.add("AdblockException", py.get_type::<AdblockException>())?;
42 m.add("BlockerException", py.get_type::<BlockerException>())?;
43 m.add("SerializationError", py.get_type::<SerializationError>())?;
44 m.add(
45 "DeserializationError",
46 py.get_type::<DeserializationError>(),
47 )?;
48 m.add(
49 "OptimizedFilterExistence",
50 py.get_type::<OptimizedFilterExistence>(),
51 )?;
52 m.add(
53 "BadFilterAddUnsupported",
54 py.get_type::<BadFilterAddUnsupported>(),
55 )?;
56 m.add("FilterExists", py.get_type::<FilterExists>())?;
40 Ok(()) 57 Ok(())
41} 58}
42 59
@@ -141,9 +158,24 @@ impl Display for BlockerError {
141 } 158 }
142} 159}
143 160
161create_exception!(adblock, AdblockException, PyException);
162create_exception!(adblock, BlockerException, AdblockException);
163create_exception!(adblock, SerializationError, BlockerException);
164create_exception!(adblock, DeserializationError, BlockerException);
165create_exception!(adblock, OptimizedFilterExistence, BlockerException);
166create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
167create_exception!(adblock, FilterExists, BlockerException);
168
144impl Into<PyErr> for BlockerError { 169impl Into<PyErr> for BlockerError {
145 fn into(self) -> PyErr { 170 fn into(self) -> PyErr {
146 PyErr::new::<PyValueError, _>(format!("{:?}", self)) 171 let msg = format!("{:?}", self);
172 match self {
173 Self::SerializationError => PyErr::new::<SerializationError, _>(msg),
174 Self::DeserializationError => PyErr::new::<DeserializationError, _>(msg),
175 Self::OptimizedFilterExistence => PyErr::new::<OptimizedFilterExistence, _>(msg),
176 Self::BadFilterAddUnsupported => PyErr::new::<BadFilterAddUnsupported, _>(msg),
177 Self::FilterExists => PyErr::new::<FilterExists, _>(msg),
178 }
147 } 179 }
148} 180}
149 181
@@ -163,7 +195,7 @@ fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
163 match filter_format { 195 match filter_format {
164 "standard" => Ok(FilterFormat::Standard), 196 "standard" => Ok(FilterFormat::Standard),
165 "hosts" => Ok(FilterFormat::Hosts), 197 "hosts" => Ok(FilterFormat::Hosts),
166 _ => Err(PyErr::new::<PyValueError, _>("Invalid format value")), 198 _ => Err(PyErr::new::<AdblockException, _>("Invalid format value")),
167 } 199 }
168} 200}
169 201