diff options
| 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 |
| commit | fb1832feb868a81e2d10a1707c57df4623304313 (patch) | |
| tree | f77f31d340f19b0e4d96a92ccc23cdeb6b135738 /src | |
| parent | 0d5c8b8cfa1025c29b96db3c52ff54a7d204923f (diff) | |
Create a custom exception type for this library
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 40 |
1 files changed, 36 insertions, 4 deletions
| @@ -17,7 +17,8 @@ use adblock::engine::Engine as RustEngine; | |||
| 17 | use adblock::lists::FilterFormat; | 17 | use adblock::lists::FilterFormat; |
| 18 | use adblock::lists::FilterSet as RustFilterSet; | 18 | use adblock::lists::FilterSet as RustFilterSet; |
| 19 | use pyo3::class::PyObjectProtocol; | 19 | use pyo3::class::PyObjectProtocol; |
| 20 | use pyo3::exceptions::PyValueError; | 20 | use pyo3::create_exception; |
| 21 | use pyo3::exceptions::PyException; | ||
| 21 | use pyo3::prelude::*; | 22 | use pyo3::prelude::*; |
| 22 | use pyo3::types::PyBytes; | 23 | use pyo3::types::PyBytes; |
| 23 | use pyo3::PyErr; | 24 | use 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] |
| 34 | fn adblock(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | 35 | fn 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 | ||
| 161 | create_exception!(adblock, AdblockException, PyException); | ||
| 162 | create_exception!(adblock, BlockerException, AdblockException); | ||
| 163 | create_exception!(adblock, SerializationError, BlockerException); | ||
| 164 | create_exception!(adblock, DeserializationError, BlockerException); | ||
| 165 | create_exception!(adblock, OptimizedFilterExistence, BlockerException); | ||
| 166 | create_exception!(adblock, BadFilterAddUnsupported, BlockerException); | ||
| 167 | create_exception!(adblock, FilterExists, BlockerException); | ||
| 168 | |||
| 144 | impl Into<PyErr> for BlockerError { | 169 | impl 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 | ||
