use crate::core::analysis::analyzer::Analyzer;
use crate::core::analysis::reader::ReaderEnum;
use crate::core::analysis::token_stream::{AnalyzerTokenStreams, TokenStreamEnum2};
use crate::core::document::field::{BinaryTokenStream, StringTokenStream};
use crate::core::document::field::{FieldDataEnum, IndexingTokenStreamEnum3};
use crate::core::document::fields::FieldTokenStreamEnum;
use crate::core::document::invertable_field::InvertableType;
use crate::core::index::indexable_field_type::IndexableFieldType;
use crate::core::index::{BytesRef, BytesRefValue, BytesRefValueEnum};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::number::Number;
use std::borrow::{Borrow, Cow};
use std::cell::RefMut;
use std::fmt::Display;
pub trait IndexableField: Display {
fn name(&self) -> &str;
type FieldType<'a>: IndexableFieldType
where
Self: 'a;
fn field_type(&self) -> Self::FieldType<'_>;
fn token_stream<'a, A>(
&'a mut self,
analyzer: &'a A,
reuse_token_stream: &'a mut Option<ReusedIndexingTokenStream>,
) -> Result<IndexingTokenStream<'a>>
where
A: Analyzer;
fn binary_value(&self) -> Result<Option<BinaryValueEnum<'_>>>;
fn take_binary_value(&mut self) -> Result<Option<BytesRef<Vec<u8>>>>;
fn string_value(&self) -> Result<Option<Cow<'_, str>>>;
fn take_string_value(&mut self) -> Result<Option<String>>;
fn get_char_sequence_value(&self) -> Result<Option<Cow<'_, str>>> {
self.string_value()
}
fn take_reader_value(&mut self) -> Result<Option<ReaderEnum>>;
fn numeric_value(&self) -> Result<Option<Number>>;
type StoredValue<'a>: Borrow<FieldDataEnum>
where
Self: 'a;
fn stored_value(&self) -> Result<Option<Self::StoredValue<'_>>>;
fn invertable_type(&self) -> &InvertableType;
fn is_reserved(&self) -> bool {
false
}
fn float_vector_value(&self) -> Result<&[f32]> {
Err(LuceneError::unsupported_operation(""))
}
fn byte_vector_value(&self) -> Result<&[u8]> {
Err(LuceneError::unsupported_operation(""))
}
}
pub enum StoredValueEnum<'a> {
Borrowed(&'a FieldDataEnum),
Owned(FieldDataEnum),
}
impl Borrow<FieldDataEnum> for StoredValueEnum<'_> {
fn borrow(&self) -> &FieldDataEnum {
match self {
Self::Borrowed(value) => value,
Self::Owned(value) => value,
}
}
}
impl<'a> From<&'a FieldDataEnum> for StoredValueEnum<'a> {
fn from(value: &'a FieldDataEnum) -> Self {
Self::Borrowed(value)
}
}
impl From<FieldDataEnum> for StoredValueEnum<'_> {
fn from(value: FieldDataEnum) -> Self {
Self::Owned(value)
}
}
pub type IndexingTokenStream<'a> = Option<
IndexingTokenStreamEnum3<
RefMut<'a, AnalyzerTokenStreams>,
&'a mut ReusedIndexingTokenStream,
&'a mut FieldTokenStreamEnum,
>,
>;
pub type ReusedIndexingTokenStream = TokenStreamEnum2<BinaryTokenStream, StringTokenStream>;
#[derive(Debug, PartialEq, Eq)]
pub enum BinaryValueEnum<'a> {
Borrowed(&'a BytesRef<Vec<u8>>),
Owned(BytesRef<Vec<u8>>),
Slice(BytesRef<&'a [u8]>),
Inline {
bytes: [u8; 8],
length: usize,
},
}
impl From<BytesRef<Vec<u8>>> for BinaryValueEnum<'_> {
#[inline]
fn from(value: BytesRef<Vec<u8>>) -> Self {
Self::Owned(value)
}
}
impl<'a> From<&'a BytesRef<Vec<u8>>> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a BytesRef<Vec<u8>>) -> Self {
Self::Borrowed(value)
}
}
impl From<Vec<u8>> for BinaryValueEnum<'_> {
#[inline]
fn from(value: Vec<u8>) -> Self {
let length = value.len();
Self::Owned(BytesRef {
bytes: value,
offset: 0,
length,
})
}
}
impl From<String> for BinaryValueEnum<'_> {
#[inline]
fn from(value: String) -> Self {
Self::from(value.into_bytes())
}
}
impl<'a> From<&'a [u8]> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a [u8]) -> Self {
Self::Slice(BytesRef {
bytes: value,
offset: 0,
length: value.len(),
})
}
}
impl<'a> From<&'a Vec<u8>> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a Vec<u8>) -> Self {
Self::from(value.as_slice())
}
}
impl<'a> From<&'a str> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a str) -> Self {
Self::from(value.as_bytes())
}
}
impl<'a> From<&'a String> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a String) -> Self {
Self::from(value.as_bytes())
}
}
impl<const N: usize> From<[u8; N]> for BinaryValueEnum<'_> {
#[inline]
fn from(value: [u8; N]) -> Self {
Self::from(Vec::from(value))
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for BinaryValueEnum<'a> {
#[inline]
fn from(value: &'a [u8; N]) -> Self {
Self::from(value.as_slice())
}
}
impl<'a> BinaryValueEnum<'a> {
pub fn into_cow(self) -> Cow<'a, BytesRef<Vec<u8>>> {
match self {
Self::Borrowed(value) => Cow::Borrowed(value),
Self::Owned(value) => Cow::Owned(value),
Self::Slice(value) => Cow::Owned(BytesRef::from(value.as_bytes().to_vec())),
Self::Inline { bytes, length } => Cow::Owned(BytesRef::from(bytes[..length].to_vec())),
}
}
pub fn into_owned(self) -> BytesRef<Vec<u8>> {
match self {
Self::Borrowed(value) => value.clone(),
Self::Owned(value) => value,
Self::Slice(value) => BytesRef::from(value.as_bytes().to_vec()),
Self::Inline { bytes, length } => BytesRef::from(bytes[..length].to_vec()),
}
}
#[inline(always)]
pub fn as_bytes_ref(&self) -> BytesRef<&[u8]> {
match self {
Self::Borrowed(value) => value.as_bytes_ref(),
Self::Owned(value) => value.as_bytes_ref(),
Self::Slice(value) => value.as_bytes_ref(),
Self::Inline { bytes, length } => BytesRef {
bytes,
offset: 0,
length: *length,
},
}
}
#[inline(always)]
pub fn as_byte_slice(&self) -> &[u8] {
match self {
Self::Borrowed(value) => value.as_byte_slice(),
Self::Owned(value) => value.as_byte_slice(),
Self::Slice(value) => value.as_byte_slice(),
Self::Inline { bytes, length } => &bytes[..*length],
}
}
}
impl Display for BinaryValueEnum<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.as_bytes_ref(), f)
}
}
impl<'a> BytesRefValue<'a> for BinaryValueEnum<'a> {
#[inline(always)]
fn as_bytes_ref(&self) -> BytesRef<&[u8]> {
BinaryValueEnum::as_bytes_ref(self)
}
fn into_value(self) -> BytesRefValueEnum<'a> {
match self {
Self::Borrowed(value) => BytesRefValueEnum::Buffer(Cow::Borrowed(value)),
Self::Owned(value) => BytesRefValueEnum::Buffer(Cow::Owned(value)),
Self::Slice(value) => BytesRefValueEnum::Slice(value),
Self::Inline { bytes, length } => {
BytesRefValueEnum::Buffer(Cow::Owned(BytesRef::from(bytes[..length].to_vec())))
},
}
}
}