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