tantivy-py/src/schema.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

2023-08-26 12:13:29 +00:00
use crate::to_pyerr;
use pyo3::{basic::CompareOp, prelude::*, types::PyTuple};
use serde::{Deserialize, Serialize};
2019-08-02 11:23:10 +00:00
use tantivy as tv;
/// Tantivy's Type
#[pyclass(frozen, module = "tantivy.tantivy")]
#[derive(Clone, PartialEq)]
pub(crate) enum FieldType {
Text,
Unsigned,
Integer,
Float,
Boolean,
Date,
Facet,
Bytes,
Json,
IpAddr,
}
impl From<FieldType> for tv::schema::Type {
fn from(field_type: FieldType) -> tv::schema::Type {
match field_type {
FieldType::Text => tv::schema::Type::Str,
FieldType::Unsigned => tv::schema::Type::U64,
FieldType::Integer => tv::schema::Type::I64,
FieldType::Float => tv::schema::Type::F64,
FieldType::Boolean => tv::schema::Type::Str,
FieldType::Date => tv::schema::Type::Date,
FieldType::Facet => tv::schema::Type::Facet,
FieldType::Bytes => tv::schema::Type::Bytes,
FieldType::Json => tv::schema::Type::Json,
FieldType::IpAddr => tv::schema::Type::IpAddr,
}
}
}
/// Tantivy schema.
///
/// The schema is very strict. To build the schema the `SchemaBuilder` class is
/// provided.
2024-01-21 20:16:34 +00:00
#[pyclass(frozen, module = "tantivy.tantivy")]
2023-08-26 12:13:29 +00:00
#[derive(Deserialize, PartialEq, Serialize)]
pub(crate) struct Schema {
2019-08-02 11:23:10 +00:00
pub(crate) inner: tv::schema::Schema,
}
#[pymethods]
impl Schema {
fn __richcmp__(
&self,
other: &Self,
op: CompareOp,
py: Python<'_>,
) -> PyObject {
match op {
CompareOp::Eq => (self == other).into_py(py),
CompareOp::Ne => (self != other).into_py(py),
_ => py.NotImplemented(),
}
}
2023-08-26 12:13:29 +00:00
#[staticmethod]
2024-05-07 14:36:46 +00:00
fn _internal_from_pythonized(serialized: &Bound<PyAny>) -> PyResult<Self> {
pythonize::depythonize_bound(serialized.clone()).map_err(to_pyerr)
2023-08-26 12:13:29 +00:00
}
fn __reduce__<'a>(
slf: PyRef<'a, Self>,
py: Python<'a>,
2024-05-07 14:36:46 +00:00
) -> PyResult<Bound<'a, PyTuple>> {
2023-08-26 12:13:29 +00:00
let serialized = pythonize::pythonize(py, &*slf).map_err(to_pyerr)?;
2024-05-07 14:36:46 +00:00
Ok(PyTuple::new_bound(
2023-08-26 12:13:29 +00:00
py,
[
slf.into_py(py).getattr(py, "_internal_from_pythonized")?,
2024-05-07 14:36:46 +00:00
PyTuple::new_bound(py, [serialized]).to_object(py),
2023-08-26 12:13:29 +00:00
],
))
}
}