rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use crate::core::index::doc_values_iterator::DocValuesIterator;
use crate::core::index::{BytesRefValue, BytesRefValueEnum2};
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::util::error::lucene_error::{LuceneError, Result};

pub trait BinaryDocValues: DocValuesIterator {
  type Value<'a>: BytesRefValue<'a>
  where
    Self: 'a;
  /// Returns the binary value for the current document ID.
  /// It is illegal to call this method after
  /// [`DocValuesIterator::advance_exact`] returned `false`.
  ///
  /// # Returns
  /// The binary value for the current document ID.
  fn binary_value(&self) -> Result<Self::Value<'_>> {
    Err(LuceneError::not_implemented("this method need implement"))
  }
}

// BinaryDocValues
macro_rules! either_binary_docvalues {
    ($vis:vis $name:ident { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
        $vis enum $name<$( $T ),+> {
            $( $Variant($T), )+
        }

        impl<$( $T ),+> DocValuesIterator for $name<$( $T ),+>
        where
            $( $T: DocValuesIterator ),+
        {
            fn advance_exact(&mut self, target: i32) -> Result<bool> {
                match self {
                    $( Self::$Variant(inner) => inner.advance_exact(target), )+
                }
            }
        }

        impl<$( $T ),+> crate::core::search::doc_id_set_iterator::DocIdSetIteratorExtensions for $name<$( $T ),+>
        where
            $( $T: DocIdSetIterator ),+
        {}
        impl<$( $T ),+> crate::core::search::doc_id_set_iterator::BitSetIteratorAccess for $name<$( $T ),+>
        where
            $( $T: DocIdSetIterator ),+
        {}

        impl<$( $T ),+> DocIdSetIterator for $name<$( $T ),+>
        where
            $( $T: DocIdSetIterator ),+
        {
            fn doc_id(&self) -> i32 {
                match self {
                    $( Self::$Variant(inner) => inner.doc_id(), )+
                }
            }
            fn next_doc(&mut self) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.next_doc(), )+
                }
            }
            fn advance(&mut self, target: i32) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.advance(target), )+
                }
            }
            fn slow_advance(&mut self, target: i32) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.slow_advance(target), )+
                }
            }
            fn cost(&self) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => inner.cost(), )+
                }
            }
        }

        impl<$( $T ),+> BinaryDocValues for $name<$( $T ),+>
        where
            $( $T: BinaryDocValues ),+
        {
            type Value<'a> = BytesRefValueEnum2<$( <$T as BinaryDocValues>::Value<'a> ),+>
            where Self: 'a;

            fn binary_value(&self) -> Result<Self::Value<'_>> {
                match self {
                    $( Self::$Variant(inner) => inner.binary_value().map(BytesRefValueEnum2::$Variant), )+
                }
            }
        }
    };
}
either_binary_docvalues!(pub BinaryDocValuesEnum2 { A: A, B: B });