#[macro_export]
macro_rules! define_trust {
(
descriptions {
$($desc_level:ident => $desc:literal,)*
}
functions {
$($func_level:ident => [$($func_name:ident),* $(,)?],)*
}
) => {
pub mod trust {
use crate::bindings::host::common::client::get_sender;
use crate::bindings::host::types::types::Error;
use crate::bindings::permissions::plugin::api as Permissions;
#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
pub enum FunctionName {
$(
$(
$func_name,
)*
)*
}
impl FunctionName {
pub fn as_str(&self) -> &'static str {
match self {
$(
$(
Self::$func_name => stringify!($func_name),
)*
)*
}
}
}
struct TrustRequirement;
impl TrustRequirement {
fn get_descriptions() -> crate::bindings::permissions::plugin::types::Descriptions {
(
$(
$crate::indoc::indoc! { $desc }.to_string(),
)*
)
}
fn get_level(fn_name: FunctionName) -> crate::bindings::permissions::plugin::types::TrustLevel {
match fn_name {
$(
$(
FunctionName::$func_name => crate::bindings::permissions::plugin::types::TrustLevel::$func_level,
)*
)*
}
}
}
pub fn is_authorized(fn_name: FunctionName) -> Result<bool, Error> {
is_authorized_with_whitelist(fn_name, vec![])
}
pub fn assert_authorized(fn_name: FunctionName) -> Result<(), Error> {
if !is_authorized(fn_name)? {
panic!("Unauthorized call to: {}", fn_name.as_str());
}
Ok(())
}
pub fn is_authorized_with_whitelist(fn_name: FunctionName, whitelist: Vec<String>) -> Result<bool, Error> {
Permissions::is_authorized(
&get_sender(),
TrustRequirement::get_level(fn_name),
&TrustRequirement::get_descriptions(),
fn_name.as_str(),
&whitelist,
)
}
pub fn assert_authorized_with_whitelist(fn_name: FunctionName, whitelist: Vec<String>) -> Result<(), Error> {
if !is_authorized_with_whitelist(fn_name, whitelist)? {
let err_msg = format!("Unauthorized call to: {}", fn_name.as_str());
panic!("{}", err_msg);
}
Ok(())
}
}
};
}