Skip to main content

armour_core/
lib.rs

1#![warn(clippy::unwrap_used)]
2
3pub mod enc;
4pub mod error;
5pub mod fuid;
6pub mod get_type;
7pub mod id64;
8pub mod key_part;
9pub mod key_type;
10pub mod num_ops;
11pub mod record_status;
12pub mod typ;
13pub mod zbase;
14
15#[cfg(feature = "std")]
16pub mod persist;
17
18use arrayvec::ArrayString;
19
20pub use armour_derive::*;
21pub use error::ArmourError;
22pub use fuid::{Fuid, OptFuid};
23pub use get_type::GetType;
24pub use id64::{Id64, OptId64};
25pub use key_type::{KeyScheme, KeyType};
26pub use typ::{EnumType, Fields, SimpleEnumType, StructType, Typ};
27
28/// Compatibility module for armour-derive generated code paths.
29pub mod dyn_types {
30    pub use crate::get_type;
31    pub use crate::typ::{EnumType, Fields, SimpleEnumType, StructType, Typ};
32}
33
34pub type IdStr = ArrayString<13>;
35
36pub type Result<T, E = ArmourError> = core::result::Result<T, E>;
37
38pub const ID_ENC_FLAG: u64 = 1u64 << 8;
39
40/// key should be Base64Url decoded 56 bytes long (~ 75 chars)
41///
42/// `const_hasher!(module_name, "KEY_ENV_NAME");`
43///
44/// env name concatenated with "APP_ID_KEY_" will be used to get the key
45#[macro_export]
46macro_rules! const_hasher {
47    ($m_name: ident, $key_name: literal) => {
48        pub mod $m_name {
49            use $crate::enc::{Cipher, IdHasher};
50
51            #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
52            pub struct Hasher;
53
54            impl IdHasher for Hasher {
55                const HASHER: Cipher = Cipher::new(env!(concat!("APP_ID_KEY_", $key_name)));
56            }
57        }
58    };
59}
60
61/// Like `const_hasher!`, but in debug builds falls back to `$default` if the env var is not set.
62///
63/// `const_hasher_or!(module_name, "KEY_ENV_NAME", "default_base64url_key");`
64///
65/// env name concatenated with "APP_ID_KEY_" will be used to get the key
66#[macro_export]
67macro_rules! const_hasher_or {
68    ($m_name:ident, $key_name:literal, $default:literal) => {
69        pub mod $m_name {
70            use $crate::enc::{Cipher, IdHasher};
71
72            #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
73            pub struct Hasher;
74
75            impl IdHasher for Hasher {
76                const HASHER: Cipher = Cipher::new({
77                    #[cfg(debug_assertions)]
78                    {
79                        match option_env!(concat!("APP_ID_KEY_", $key_name)) {
80                            Some(v) => v,
81                            None => $default,
82                        }
83                    }
84                    #[cfg(not(debug_assertions))]
85                    {
86                        env!(concat!("APP_ID_KEY_", $key_name))
87                    }
88                });
89            }
90        }
91    };
92}
93
94/// key should be Base64Url decoded 56 bytes long (~ 75 chars)
95///
96/// `hasher!(module_name, "_mKbKGF2IrkGvIJvl97HuCgWjgt6QRZ7Ye8DHBQ2anvyi18BdMz8uN6Ej3YJApooY6qDu0obqq4");`
97#[macro_export]
98macro_rules! hasher {
99    ($m_name: ident, $key: literal) => {
100        pub mod $m_name {
101            use $crate::enc::{Cipher, IdHasher};
102
103            #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
104            pub struct Hasher;
105
106            impl IdHasher for Hasher {
107                const HASHER: Cipher = Cipher::new($key);
108            }
109        }
110    };
111}