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::util::error::lucene_error::LuceneError;

#[derive(Debug, Clone)]
pub struct Parse {
  pub message: String,
  pub position: i32,
  pub error: Option<LuceneError>,
}

impl Parse {
  pub fn new<T>(msg: T, position: i32) -> Self
  where
    T: Into<String>,
  {
    Self {
      message: msg.into(),
      position,
      error: None,
    }
  }
  pub fn with_error<T>(msg: T, error: Option<LuceneError>) -> Self
  where
    T: Into<String>,
  {
    Self {
      message: msg.into(),
      position: 0,
      error,
    }
  }
}

impl std::fmt::Display for Parse {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    if let Some(error) = self.error.as_ref() {
      write!(
        f,
        "Parse Error at {}: {} reason: {}",
        self.position, self.message, error
      )
    } else {
      write!(f, "Parse Error at {}: {}", self.position, self.message)
    }
  }
}

impl std::error::Error for Parse {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    self.error.as_ref().map(|e| e as &dyn std::error::Error)
  }
}