use crate::{
access::principal::Principal,
types::{ChatId, Update, UserId},
};
#[cfg(test)]
mod tests;
#[derive(Debug)]
pub struct AccessRule {
principal: Principal,
is_granted: bool,
}
impl AccessRule {
pub fn new<T>(principal: T, is_granted: bool) -> Self
where
T: Into<Principal>,
{
AccessRule {
principal: principal.into(),
is_granted,
}
}
pub fn allow<T>(value: T) -> Self
where
T: Into<Principal>,
{
Self::new(value, true)
}
pub fn deny<T>(value: T) -> Self
where
T: Into<Principal>,
{
Self::new(value, false)
}
pub fn allow_all() -> Self {
Self::allow(Principal::All)
}
pub fn deny_all() -> Self {
Self::deny(Principal::All)
}
pub fn allow_user<T>(value: T) -> Self
where
T: Into<UserId>,
{
Self::allow(value.into())
}
pub fn deny_user<T>(value: T) -> Self
where
T: Into<UserId>,
{
Self::deny(value.into())
}
pub fn allow_chat<T>(value: T) -> Self
where
T: Into<ChatId>,
{
Self::allow(value.into())
}
pub fn deny_chat<T>(value: T) -> Self
where
T: Into<ChatId>,
{
Self::deny(value.into())
}
pub fn allow_chat_user<A, B>(chat_id: A, user_id: B) -> Self
where
A: Into<ChatId>,
B: Into<UserId>,
{
Self::allow((chat_id.into(), user_id.into()))
}
pub fn deny_chat_user<A, B>(chat_id: A, user_id: B) -> Self
where
A: Into<ChatId>,
B: Into<UserId>,
{
Self::deny((chat_id.into(), user_id.into()))
}
pub fn accepts(&self, update: &Update) -> bool {
self.principal.accepts(update)
}
pub fn is_granted(&self) -> bool {
self.is_granted
}
}