apalis_core/task/
namespace.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::convert::From;
use std::fmt::{self, Display, Formatter};
use std::ops::Deref;

use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::request::Request;
use crate::service_fn::FromRequest;

/// A wrapper type that defines a task's namespace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Namespace(pub String);

impl Deref for Namespace {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Display for Namespace {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for Namespace {
    fn from(s: String) -> Self {
        Namespace(s)
    }
}

impl From<Namespace> for String {
    fn from(value: Namespace) -> String {
        value.0
    }
}

impl AsRef<str> for Namespace {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl<Req, Ctx> FromRequest<Request<Req, Ctx>> for Namespace {
    fn from_request(req: &Request<Req, Ctx>) -> Result<Self, Error> {
        let msg = "Missing `Namespace`. This is a bug, please file a report with the backend you are using".to_owned();
        req.parts.namespace.clone().ok_or(Error::MissingData(msg))
    }
}