Python - Better error conversions

This commit is contained in:
Anthony MOI
2019-12-13 12:14:27 -05:00
parent 7711946882
commit 1c7be358b7
2 changed files with 29 additions and 0 deletions

View File

@ -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<T>(pub Result<T>);
impl<T> std::convert::Into<PyResult<T>> for ToPyResult<T> {
fn into(self) -> PyResult<T> {
match self.0 {
Ok(o) => Ok(o),
Err(e) => Err(exceptions::Exception::py_err(format!("{}", e))),
}
}
}

View File

@ -1,5 +1,6 @@
mod decoders;
mod encoding;
mod error;
mod models;
mod pre_tokenizers;
mod token;