apalis_core/task/
namespace.rs

1use std::convert::From;
2use std::fmt::{self, Display, Formatter};
3use std::ops::Deref;
4
5use serde::{Deserialize, Serialize};
6
7use crate::error::Error;
8use crate::request::Request;
9use crate::service_fn::FromRequest;
10
11/// A wrapper type that defines a task's namespace.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Namespace(pub String);
14
15impl Deref for Namespace {
16    type Target = String;
17
18    fn deref(&self) -> &Self::Target {
19        &self.0
20    }
21}
22
23impl Display for Namespace {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        write!(f, "{}", self.0)
26    }
27}
28
29impl From<String> for Namespace {
30    fn from(s: String) -> Self {
31        Namespace(s)
32    }
33}
34
35impl From<Namespace> for String {
36    fn from(value: Namespace) -> String {
37        value.0
38    }
39}
40
41impl AsRef<str> for Namespace {
42    fn as_ref(&self) -> &str {
43        &self.0
44    }
45}
46
47impl<Req, Ctx> FromRequest<Request<Req, Ctx>> for Namespace {
48    fn from_request(req: &Request<Req, Ctx>) -> Result<Self, Error> {
49        let msg = "Missing `Namespace`. This is a bug, please file a report with the backend you are using".to_owned();
50        req.parts.namespace.clone().ok_or(Error::MissingData(msg))
51    }
52}