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::impact::Impact;
use crate::core::util::error::lucene_error::Result;

/// Information about upcoming impacts, i.e., (freq, norm) pairs.
pub trait Impacts {
  /// Return the number of levels on which we have impacts.
  ///
  /// The returned value is always greater than 0 and may not always be the
  /// same, even on a single postings list, depending on the current doc
  /// ID.
  fn num_levels(&self) -> usize;

  /// Return the maximum inclusive doc ID until which the list of impacts
  /// returned by `get_impacts(level)` is valid.
  ///
  /// This is a non-decreasing function of `level`.
  fn get_doc_id_upto(&self, level: usize) -> i32;

  /// Return impacts on the given level.
  ///
  /// These impacts are sorted by increasing frequency and increasing unsigned
  /// norm, and only valid until the doc ID returned by
  /// `get_doc_id_upto(level)` (inclusive).
  ///
  /// The returned list is never empty and supports efficient random access when it contains more
  /// than one element.
  ///
  /// NOTE: There is no guarantee that these impacts actually appear in
  /// postings, only that they trigger scores that are greater than or
  /// equal to the impacts that actually appear in postings.
  fn get_impacts(&self, level: usize) -> Result<Vec<Impact>>;
}
macro_rules! define_impacts_enum {
    (
        $enum_name:ident,
        [$($V:ident),+ $(,)?]
    ) => {
        pub enum $enum_name<$($V),+> {
            $($V($V)),+
        }

        impl<$($V),+> Impacts for $enum_name<$($V),+>
        where
            $($V: Impacts,)+
        {
            fn num_levels(&self) -> usize {
                match self {
                    $(Self::$V(t) => t.num_levels(),)+
                }
            }

            fn get_doc_id_upto(&self, level: usize) -> i32 {
                match self {
                    $(Self::$V(t) => t.get_doc_id_upto(level),)+
                }
            }

            fn get_impacts(&self, level: usize) -> Result<Vec<Impact>> {
                match self {
                    $(Self::$V(t) => t.get_impacts(level),)+
                }
            }
        }
    };
}
define_impacts_enum!(ImpactsEnum2, [A, B]);