use std::sync::Arc;
use crate::ast;
use crate::ast::PolicySetError;
use crate::entities::JsonDeserializationError;
use crate::parser::err::{parse_errors, ParseErrors};
use crate::parser::{join_with_conjunction, 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("found template slot {slot} in a `{clausetype}` clause")]
#[diagnostic(help("slots are currently unsupported in `{clausetype}` clauses"))]
SlotsInConditionClause {
slot: ast::SlotId,
clausetype: &'static str,
},
#[error("missing operator, found empty object")]
MissingOperator,
#[error("found multiple operators where one was expected: {ops:?}")]
MultipleOperators {
ops: Vec<SmolStr>,
},
#[error(
"multiplication must be by a constant int: neither `{arg1}` nor `{arg2}` is a constant"
)]
MultiplicationByNonConstant {
arg1: ast::Expr,
arg2: ast::Expr,
},
#[error("{}", match .0.first() { Some(err) => format!("{err}"), None => "invalid escape".into() })]
UnescapeError(#[related] Vec<unescape::UnescapeError>),
#[error("invalid entity type: {0}")]
#[diagnostic(transparent)]
InvalidEntityType(ParseErrors),
#[error("Error creating policy set: {0}")]
#[diagnostic(transparent)]
PolicySet(#[from] PolicySetError),
#[error("Error linking policy set: {0}")]
#[diagnostic(transparent)]
Linking(#[from] ast::LinkingError),
#[error("Invalid extension function name: `{0}`")]
UnknownExtFunc(ast::Name),
#[error(transparent)]
#[diagnostic(transparent)]
InvalidActionType(#[from] InvalidActionType),
}
#[derive(Debug, Diagnostic, Error)]
#[diagnostic(help("action entities must have type `Action`, optionally in a namespace"))]
pub struct InvalidActionType {
pub(crate) euids: NonEmpty<Arc<crate::ast::EntityUID>>,
}
impl std::fmt::Display for InvalidActionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"expected that action entity uids would have the type `Action` but got "
)?;
join_with_conjunction(f, "and", self.euids.iter(), |f, e| write!(f, "`{e}`"))
}
}
#[derive(Debug, PartialEq, Diagnostic, Error)]
pub enum InstantiationError {
#[error("failed to instantiate 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())
}
}