2023-08-04 07:23:31 +00:00
|
|
|
use pyo3::{basic::CompareOp, prelude::*, types::PyType};
|
2019-06-04 09:09:58 +00:00
|
|
|
use tantivy::schema;
|
|
|
|
|
|
|
|
/// A Facet represent a point in a given hierarchy.
|
|
|
|
///
|
|
|
|
/// They are typically represented similarly to a filepath. For instance, an
|
|
|
|
/// e-commerce website could have a Facet for /electronics/tv_and_video/led_tv.
|
|
|
|
///
|
|
|
|
/// A document can be associated to any number of facets. The hierarchy
|
|
|
|
/// implicitely imply that a document belonging to a facet also belongs to the
|
|
|
|
/// ancestor of its facet. In the example above, /electronics/tv_and_video/
|
|
|
|
/// and /electronics.
|
2023-08-04 07:23:31 +00:00
|
|
|
#[pyclass(frozen)]
|
|
|
|
#[derive(Clone, PartialEq)]
|
2019-06-04 09:09:58 +00:00
|
|
|
pub(crate) struct Facet {
|
|
|
|
pub(crate) inner: schema::Facet,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl Facet {
|
|
|
|
/// Create a new instance of the "root facet" Equivalent to /.
|
|
|
|
#[classmethod]
|
|
|
|
fn root(_cls: &PyType) -> Facet {
|
|
|
|
Facet {
|
|
|
|
inner: schema::Facet::root(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the facet is the root facet /.
|
|
|
|
#[getter]
|
|
|
|
fn is_root(&self) -> bool {
|
|
|
|
self.inner.is_root()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if another Facet is a subfacet of this facet.
|
|
|
|
/// Args:
|
|
|
|
/// other (Facet): The Facet that we should check if this facet is a
|
|
|
|
/// subset of.
|
|
|
|
fn is_prefix_of(&self, other: &Facet) -> bool {
|
|
|
|
self.inner.is_prefix_of(&other.inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a Facet object from a string.
|
|
|
|
/// Args:
|
|
|
|
/// facet_string (str): The string that contains a facet.
|
|
|
|
///
|
|
|
|
/// Returns the created Facet.
|
|
|
|
#[classmethod]
|
|
|
|
fn from_string(_cls: &PyType, facet_string: &str) -> Facet {
|
|
|
|
Facet {
|
2022-01-03 13:51:13 +00:00
|
|
|
inner: schema::Facet::from(facet_string),
|
2019-06-04 09:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 11:23:10 +00:00
|
|
|
|
|
|
|
/// Returns the list of `segments` that forms a facet path.
|
|
|
|
///
|
|
|
|
/// For instance `//europe/france` becomes `["europe", "france"]`.
|
|
|
|
fn to_path(&self) -> Vec<&str> {
|
|
|
|
self.inner.to_path()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the facet string representation.
|
|
|
|
fn to_path_str(&self) -> String {
|
|
|
|
self.inner.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __repr__(&self) -> PyResult<String> {
|
|
|
|
Ok(format!("Facet({})", self.to_path_str()))
|
|
|
|
}
|
2023-08-04 07:23:31 +00:00
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 09:09:58 +00:00
|
|
|
}
|