use topcoat::context::Cx;
use super::Action;
use crate::{policy::Ability, resource::Resource};
impl<M> Ability<'_, M> {
pub fn is_action<A, R>(self) -> bool
where
A: Action<R>,
R: Resource<Model = M>,
{
matches!(self, Ability::RunAny { action } | Ability::Run { action, .. } if action == A::NAME)
}
}
impl<M> crate::policy::Abilities<M> {
#[must_use]
pub fn run<A, R>(mut self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static) -> Self
where
A: Action<R>,
R: Resource<Model = M>,
{
self.set_action_record(A::NAME, Box::new(check));
self
}
#[must_use]
pub fn run_any<A, R>(mut self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static) -> Self
where
A: Action<R>,
R: Resource<Model = M>,
{
self.set_action_any(A::NAME, Box::new(check));
self
}
}