Skip to main content
KeyOS API Reference

server/
macros.rs

1// SPDX-FileCopyrightText: 2024 Foundation Devices, Inc. <hello@foundation.xyz>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#[macro_export]
5macro_rules! wrapped_scalar {
6    ($name:ty) => {
7        impl $crate::FromScalar<4> for $name {
8            fn from_scalar(value: [u32; 4]) -> Self { Self($crate::FromScalar::from_scalar(value)) }
9        }
10
11        impl $crate::AsScalar<4> for $name {
12            fn as_scalar(&self) -> [u32; 4] { self.0.as_scalar() }
13        }
14    };
15}
16
17/// Define a named set of message permissions, plus the blanket impl that makes every
18/// permissions type granting those messages a member of it.
19///
20/// The set always requires [`CheckedPermissions`](crate::CheckedPermissions); listing other
21/// sets after the colon composes them.
22///
23/// ```rust,ignore
24/// permission_set!(pub trait ThemePermissions { GetSystemTheme, SetSystemTheme });
25///
26/// permission_set!(pub trait FileBackedPermissions: DurableFilePermissions {
27///     CreateDirMessage, CloseDir
28/// });
29/// ```
30#[macro_export]
31macro_rules! permission_set {
32    (
33        $(#[doc = $doc:expr])*
34        $vis:vis trait $name:ident $(: $($set:path),+)? {
35            $($msg:path),+ $(,)?
36        }
37    ) => {
38        $(#[doc = $doc])*
39        $vis trait $name:
40            $crate::CheckedPermissions
41            $($(+ $set)+)?
42            $(+ $crate::MessageAllowed<$msg>)+
43        {
44        }
45
46        impl<P> $name for P
47        where
48            P: $crate::CheckedPermissions
49                $($(+ $set)+)?
50                $(+ $crate::MessageAllowed<$msg>)+,
51        {
52        }
53    };
54}