lindera/
error.rs

1//! Error types for Lindera operations.
2//!
3//! This module provides error types used throughout the Lindera Python bindings.
4
5use std::fmt;
6
7use pyo3::exceptions::PyException;
8use pyo3::prelude::*;
9
10/// Error type for Lindera operations.
11///
12/// Represents errors that can occur during tokenization, dictionary operations,
13/// or other Lindera functionality.
14#[pyclass(name = "LinderaError")]
15#[derive(Debug, Clone)]
16pub struct PyLinderaError {
17    message: String,
18}
19
20#[pymethods]
21impl PyLinderaError {
22    #[new]
23    pub fn new(message: String) -> Self {
24        PyLinderaError { message }
25    }
26
27    #[getter]
28    pub fn message(&self) -> &str {
29        &self.message
30    }
31
32    fn __str__(&self) -> String {
33        self.message.clone()
34    }
35
36    fn __repr__(&self) -> String {
37        format!("LinderaError('{}')", self.message)
38    }
39}
40
41impl fmt::Display for PyLinderaError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(f, "{}", self.message)
44    }
45}
46
47impl std::error::Error for PyLinderaError {}
48
49impl From<PyLinderaError> for PyErr {
50    fn from(err: PyLinderaError) -> PyErr {
51        PyException::new_err(err.message)
52    }
53}