daemonbit_core/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) $year Kamil Becmer
3
4use std::fmt;
5
6#[const_env::from_env]
7const DAEMON_NAME: &'static str = "";
8
9pub const fn daemon_name() -> &'static str {
10    if DAEMON_NAME.len() == 0 {
11        panic!("You have to set DAEMON_NAME env var")
12    }
13    DAEMON_NAME
14}
15
16pub trait DaemonScope: sealed::Sealed {
17    fn key() -> ScopeKey;
18}
19
20pub enum Global {}
21impl DaemonScope for Global {
22    fn key() -> ScopeKey {
23        ScopeKey::Global
24    }
25}
26
27pub enum Local {}
28impl DaemonScope for Local {
29    fn key() -> ScopeKey {
30        ScopeKey::Local
31    }
32}
33
34#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
35pub enum ScopeKey {
36    Global,
37    Local,
38}
39impl ScopeKey {
40    pub fn from_str<S: AsRef<str>>(scope: S) -> Option<Self> {
41        match scope.as_ref() {
42            "global" => Some(Self::Global),
43            "local" => Some(Self::Local),
44            _ => None,
45        }
46    }
47    pub const fn as_str(&self) -> &str {
48        match *self {
49            Self::Global => "global",
50            Self::Local => "local",
51        }
52    }
53}
54impl fmt::Display for ScopeKey {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str(self.as_str())
57    }
58}
59
60pub trait Scoped {
61    fn scope(&self) -> ScopeKey;
62}
63pub trait Acquire<T: DaemonScope>: Sized {
64    fn acquire() -> Self;
65}
66pub trait TryAcquire<T: DaemonScope>: Sized {
67    type Error: std::error::Error;
68    fn try_acquire() -> Result<Self, Self::Error>;
69}
70
71mod sealed {
72    pub trait Sealed {}
73    impl Sealed for super::Global {}
74    impl Sealed for super::Local {}
75}