use mant_protocol::ScopeTextError;
use mant_query::SearchError;
use std::{error::Error, fmt};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryValidationError {
EmptySelection,
TooManySelections {
maximum: usize,
},
EmptySelector,
InvalidContentSelector,
InvalidReferenceProjection(&'static str),
InvalidEntryKinds,
EmptyEntry,
InvalidViewSelector {
field: &'static str,
error: ScopeTextError,
},
InvalidSearch(SearchError),
InvalidExplanation(mant_query::ExplanationError),
}
impl fmt::Display for QueryValidationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptySelection => formatter.write_str("at least one outline node is required"),
Self::TooManySelections { maximum } => {
write!(
formatter,
"outline nodes must not contain more than {maximum} values"
)
}
Self::EmptySelector => formatter.write_str("outline node must not be empty"),
Self::InvalidContentSelector => mant_protocol::InvalidContentSelector.fmt(formatter),
Self::InvalidReferenceProjection(reason) => formatter.write_str(reason),
Self::InvalidEntryKinds => {
formatter.write_str("outline entry kinds must contain between 1 and 9 values")
}
Self::EmptyEntry => formatter.write_str("semantic entry must not be empty"),
Self::InvalidViewSelector { field, error } => {
write!(formatter, "{field} {}", view_selector_error_message(*error))
}
Self::InvalidSearch(error) => error.fmt(formatter),
Self::InvalidExplanation(error) => error.fmt(formatter),
}
}
}
impl Error for QueryValidationError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::InvalidSearch(error) => Some(error),
Self::InvalidExplanation(error) => Some(error),
_ => None,
}
}
}
fn view_selector_error_message(error: ScopeTextError) -> String {
match error {
ScopeTextError::Empty => "must not be empty".to_owned(),
ScopeTextError::ControlCharacter => "must not contain control characters".to_owned(),
ScopeTextError::TooLong { maximum } => {
format!("must not exceed {maximum} Unicode scalar values")
}
}
}