mirror of
https://github.com/mii443/tokenizers.git
synced 2025-08-23 00:35:35 +00:00
Python - Better error conversions
This commit is contained in:
28
bindings/python/src/error.rs
Normal file
28
bindings/python/src/error.rs
Normal 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))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
mod decoders;
|
mod decoders;
|
||||||
mod encoding;
|
mod encoding;
|
||||||
|
mod error;
|
||||||
mod models;
|
mod models;
|
||||||
mod pre_tokenizers;
|
mod pre_tokenizers;
|
||||||
mod token;
|
mod token;
|
||||||
|
Reference in New Issue
Block a user