sysmonk/
constant.rs

1use fernet::Fernet;
2use std::collections::HashMap;
3use std::env;
4use std::sync::{Arc, Mutex};
5
6/// Struct to store the cargo information gathered at compile time using the `env!` macro.
7#[allow(dead_code)]
8#[derive(Debug, Clone)]
9pub struct MetaData {
10    pub crate_name: String,
11    pub manifest_dir: String,
12    pub authors: Vec<String>,
13    pub description: String,
14    pub homepage: String,
15    pub pkg_name: String,
16    pub pkg_repo: String,
17    pub pkg_version: String,
18    pub pkg_version_major: String,
19    pub pkg_version_minor: String,
20    pub pkg_version_patch: String,
21    pub pkg_version_pre: String,
22}
23
24/// Uses compile time macros to load Cargo metadata via environment variables during compilation process
25///
26/// ## References
27/// - [Official Docs](https://doc.rust-lang.org/cargo/reference/environment-variables.html)
28/// - [GitHub Issues](https://github.com/rust-lang/cargo/issues/8251#issuecomment-631731144)
29/// - [GitHub Issues](https://github.com/rust-lang/cargo/issues/11966#issue-1664748892)
30pub fn build_info() -> Arc<MetaData> {
31    let metadata = MetaData {
32        crate_name: env!("CARGO_CRATE_NAME").to_string(),
33        manifest_dir: env!("CARGO_MANIFEST_DIR").to_string(),
34        authors: env!("CARGO_PKG_AUTHORS").split(',').map(String::from).collect(),
35        description: env!("CARGO_PKG_DESCRIPTION").to_string(),
36        homepage: env!("CARGO_PKG_HOMEPAGE").to_string(),
37        pkg_name: env!("CARGO_PKG_NAME").to_string(),
38        pkg_repo: env!("CARGO_PKG_REPOSITORY").to_string(),
39        pkg_version: env!("CARGO_PKG_VERSION").to_string(),
40        pkg_version_major: env!("CARGO_PKG_VERSION_MAJOR").to_string(),
41        pkg_version_minor: env!("CARGO_PKG_VERSION_MINOR").to_string(),
42        pkg_version_patch: env!("CARGO_PKG_VERSION_PATCH").to_string(),
43        pkg_version_pre: env!("CARGO_PKG_VERSION_PRE").to_string(),
44    };
45    Arc::new(metadata)
46}
47
48/// Struct to store the session information.
49///
50/// ## Fields
51///
52/// * `mapping` - Used to store username and session token's payload as key value pairs.
53///
54/// ## See Also:
55///
56/// These fields are updated and used only for authenticated sessions.
57pub struct Session {
58    pub mapping: Mutex<HashMap<String, String>>,
59}
60
61
62/// Instantiates the `Session` struct with empty `HashMap` for `mapping` fields.
63///
64/// ## See Also
65///
66/// Creates new `Mutex` in an unlocked state for each of the fields.
67///
68/// # Returns
69///
70/// Returns the constructed `Arc` for the `Session` struct.
71pub fn session_info() -> Arc<Session> {
72    Arc::new(Session {
73        mapping: Mutex::new(HashMap::new()),
74    })
75}
76
77/// Create a [Fernet](https://docs.rs/fernet/latest/fernet/) object to encrypt and decrypt session token.
78///
79/// Generates a random key, that can be safely passed to `Fernet::new()`
80///
81/// # Returns
82///
83/// Returns the constructed `Arc` for the `Fernet` instance, with the generated key.
84pub fn fernet_object() -> Arc<Fernet> {
85    Arc::new(Fernet::new(&Fernet::generate_key()).unwrap())
86}