apalis_core/codec/
mod.rs

1use std::marker::PhantomData;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::BoxDynError;
6
7/// A codec allows backends to encode and decode data
8pub trait Codec {
9    /// The mode of storage by the codec
10    type Compact;
11    /// Error encountered by the codec
12    type Error: Into<BoxDynError>;
13    /// The encoding method
14    fn encode<I>(input: I) -> Result<Self::Compact, Self::Error>
15    where
16        I: Serialize;
17    /// The decoding method
18    fn decode<O>(input: Self::Compact) -> Result<O, Self::Error>
19    where
20        O: for<'de> Deserialize<'de>;
21}
22
23/// A noop codec to use as a placeholder when backend does not support encoding and decoding
24/// Panics if any of its methods are called
25#[derive(Debug, Clone)]
26pub struct NoopCodec<Compact> {
27    compact: PhantomData<Compact>,
28}
29
30impl<Compact> Codec for NoopCodec<Compact> {
31    type Compact = Compact;
32    type Error = BoxDynError;
33
34    fn decode<O>(_: Self::Compact) -> Result<O, Self::Error>
35    where
36        O: for<'de> Deserialize<'de>,
37    {
38        unreachable!("NoopCodec doesn't have decoding functionality")
39    }
40    fn encode<I>(_: I) -> Result<Self::Compact, Self::Error>
41    where
42        I: Serialize,
43    {
44        unreachable!("NoopCodec doesn't have decoding functionality")
45    }
46}
47
48/// Encoding for tasks using json
49#[cfg(feature = "json")]
50pub mod json;