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)
}
}