diff --git a/bindings/python/src/error.rs b/bindings/python/src/error.rs new file mode 100644 index 00000000..63994ad5 --- /dev/null +++ b/bindings/python/src/error.rs @@ -0,0 +1,28 @@ +use pyo3::exceptions; +use pyo3::prelude::*; +use std::fmt::{Display, Formatter, Result as FmtResult}; +use tokenizers::tokenizer::Result; + +#[derive(Debug)] +pub struct PyError(pub String); +impl PyError { + pub fn from(s: &str) -> Self { + PyError(String::from(s)) + } +} +impl Display for PyError { + fn fmt(&self, fmt: &mut Formatter) -> FmtResult { + write!(fmt, "{}", self.0) + } +} +impl std::error::Error for PyError {} + +pub struct ToPyResult(pub Result); +impl std::convert::Into> for ToPyResult { + fn into(self) -> PyResult { + match self.0 { + Ok(o) => Ok(o), + Err(e) => Err(exceptions::Exception::py_err(format!("{}", e))), + } + } +} diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 7fd6b3da..57f20a87 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -1,5 +1,6 @@ mod decoders; mod encoding; +mod error; mod models; mod pre_tokenizers; mod token;