use crate::ast;
use crate::entities::json::err::JsonDeserializationError;
use crate::parser::err::{parse_errors, ParseErrors};
use crate::parser::unescape;
use miette::Diagnostic;
use nonempty::NonEmpty;
use smol_str::SmolStr;
use thiserror::Error;
#[derive(Debug, Diagnostic, Error)]
pub enum FromJsonError {
#[error(transparent)]
#[diagnostic(transparent)]
JsonDeserializationError(#[from] JsonDeserializationError),
#[error(transparent)]
#[diagnostic(transparent)]
TemplateToPolicy(#[from] parse_errors::ExpectedStaticPolicy),
#[error(transparent)]
#[diagnostic(transparent)]
PolicyToTemplate(#[from] parse_errors::ExpectedTemplate),
#[error("invalid slot name or slot used in wrong position")]
#[diagnostic(help(
"principal slots must be named `?principal` and resource slots must be named `?resource`"
))]
InvalidSlotName,
#[error("slots are not allowed for actions")]
ActionSlot,
#[error(transparent)]
#[diagnostic(transparent)]
SlotsInConditionClause(#[from] parse_errors::SlotsInConditionClause),
#[error("missing operator, found empty object")]
MissingOperator,
#[error("found multiple operators where one was expected: {ops:?}")]
MultipleOperators {
ops: Vec<SmolStr>,
},
#[error("{}", .0.first())]
UnescapeError(#[related] NonEmpty<unescape::UnescapeError>),
#[error("invalid entity type: {0}")]
#[diagnostic(transparent)]
InvalidEntityType(ParseErrors),
#[error("invalid extension function: `{0}`")]
UnknownExtensionFunction(ast::Name),
#[error(transparent)]
#[diagnostic(transparent)]
InvalidActionType(#[from] parse_errors::InvalidActionType),
#[cfg(feature = "tolerant-ast")]
#[error("AST error node")]
ASTErrorNode,
}
#[derive(Debug, Diagnostic, Error)]
pub enum PolicySetFromJsonError {
#[error(transparent)]
#[diagnostic(transparent)]
PolicySet(#[from] ast::PolicySetError),
#[error(transparent)]
#[diagnostic(transparent)]
Linking(#[from] ast::LinkingError),
#[error(transparent)]
#[diagnostic(transparent)]
FromJsonError(#[from] FromJsonError),
}
#[derive(Debug, PartialEq, Eq, Diagnostic, Error)]
pub enum LinkingError {
#[error("failed to link template: no value provided for `{slot}`")]
MissedSlot {
slot: ast::SlotId,
},
}
impl From<ast::UnexpectedSlotError> for FromJsonError {
fn from(err: ast::UnexpectedSlotError) -> Self {
Self::TemplateToPolicy(err.into())
}
}