restate_sdk_shared_core/service_protocol/
version.rs

1use std::fmt;
2use std::str::FromStr;
3
4#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
5pub enum Version {
6    V1 = 1,
7    V2 = 2,
8    V3 = 3,
9    V4 = 4,
10    V5 = 5,
11    V6 = 6,
12}
13
14const CONTENT_TYPE_V1: &str = "application/vnd.restate.invocation.v1";
15const CONTENT_TYPE_V2: &str = "application/vnd.restate.invocation.v2";
16const CONTENT_TYPE_V3: &str = "application/vnd.restate.invocation.v3";
17const CONTENT_TYPE_V4: &str = "application/vnd.restate.invocation.v4";
18const CONTENT_TYPE_V5: &str = "application/vnd.restate.invocation.v5";
19const CONTENT_TYPE_V6: &str = "application/vnd.restate.invocation.v6";
20
21impl Version {
22    pub const fn content_type(&self) -> &'static str {
23        match self {
24            Version::V1 => CONTENT_TYPE_V1,
25            Version::V2 => CONTENT_TYPE_V2,
26            Version::V3 => CONTENT_TYPE_V3,
27            Version::V4 => CONTENT_TYPE_V4,
28            Version::V5 => CONTENT_TYPE_V5,
29            Version::V6 => CONTENT_TYPE_V6,
30        }
31    }
32
33    pub const fn minimum_supported_version() -> Self {
34        Version::V5
35    }
36
37    pub const fn maximum_supported_version() -> Self {
38        Version::V6
39    }
40}
41
42impl fmt::Display for Version {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{}", self.content_type())
45    }
46}
47
48#[derive(Debug, thiserror::Error)]
49#[error("unsupported protocol version '{0}'")]
50pub enum ContentTypeError {
51    #[error("unsupported protocol version '{0}'")]
52    RestateContentType(String),
53    #[error("unrecognized content-type '{0}', this is not a restate protocol content type. Make sure you're invoking the service though restate-server, rather than directly.")]
54    OtherContentType(String),
55}
56
57impl FromStr for Version {
58    type Err = ContentTypeError;
59
60    fn from_str(s: &str) -> Result<Self, Self::Err> {
61        match s {
62            CONTENT_TYPE_V1 => Ok(Version::V1),
63            CONTENT_TYPE_V2 => Ok(Version::V2),
64            CONTENT_TYPE_V3 => Ok(Version::V3),
65            CONTENT_TYPE_V4 => Ok(Version::V4),
66            CONTENT_TYPE_V5 => Ok(Version::V5),
67            CONTENT_TYPE_V6 => Ok(Version::V6),
68            s if s.starts_with("application/vnd.restate.invocation.") => {
69                Err(ContentTypeError::RestateContentType(s.to_owned()))
70            }
71            s => Err(ContentTypeError::OtherContentType(s.to_owned())),
72        }
73    }
74}