adguard_flm/filters/parser/
parser_error.rsuse crate::io::error::IOError;
use crate::io::ReadFilterFileError;
use crate::HttpClientError;
use std::fmt::{Display, Formatter};
#[non_exhaustive]
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum FilterParserError {
#[error(transparent)]
Io(IOError),
#[error(transparent)]
Network(HttpClientError),
#[error("EmptyIf")]
EmptyIf,
#[error("UnbalancedElse")]
UnbalancedElse,
#[error("UnbalancedEndIf")]
UnbalancedEndIf,
#[error("UnbalancedIf")]
UnbalancedIf,
#[error("RecursiveInclusion")]
RecursiveInclusion,
#[error("StackIsCorrupted")]
StackIsCorrupted,
#[error("SchemeIsIncorrect: {0}")]
SchemeIsIncorrect(String),
#[error("InvalidBooleanExpression")]
InvalidBooleanExpression,
#[error("Invalid checksum given: {0}, expected: {1}")]
InvalidChecksum(String, String),
#[error("NoContent")]
NoContent,
#[error("Filter content is likely not a filter")]
FilterContentIsLikelyNotAFilter,
#[error("{0}")]
Other(String),
}
impl FilterParserError {
#[inline]
pub(crate) fn other_err_from_to_string<R, S>(error_source: S) -> Result<R, FilterParserError>
where
S: ToString,
{
Err(FilterParserError::Other(error_source.to_string()))
}
#[inline]
pub(crate) fn other_from_to_string<S>(error_source: S) -> FilterParserError
where
S: ToString,
{
FilterParserError::Other(error_source.to_string())
}
#[inline]
pub(crate) fn invalid_checksum<R>(
actual: String,
expected: String,
) -> Result<R, FilterParserError> {
Err(FilterParserError::InvalidChecksum(actual, expected))
}
}
impl From<ReadFilterFileError> for FilterParserError {
fn from(value: ReadFilterFileError) -> Self {
match value {
ReadFilterFileError::Io(io_err) => Self::Io(io_err),
ReadFilterFileError::Other(other_err) => Self::Other(other_err),
}
}
}
#[derive(Debug, thiserror::Error)]
#[cfg_attr(test, derive(PartialEq))]
pub struct FilterParserErrorContext {
pub file: String,
pub line: usize,
pub error: FilterParserError,
}
impl FilterParserErrorContext {
pub(crate) fn new(error: FilterParserError, line: usize, file: String) -> Self {
Self { error, line, file }
}
}
impl Display for FilterParserErrorContext {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Parser error: \"{}\" encountered in {}:{}",
self.error, self.file, self.line
)
}
}