Skip to main content

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    V7 = 7,
13}
14
15const CONTENT_TYPE_V1: &str = "application/vnd.restate.invocation.v1";
16const CONTENT_TYPE_V2: &str = "application/vnd.restate.invocation.v2";
17const CONTENT_TYPE_V3: &str = "application/vnd.restate.invocation.v3";
18const CONTENT_TYPE_V4: &str = "application/vnd.restate.invocation.v4";
19const CONTENT_TYPE_V5: &str = "application/vnd.restate.invocation.v5";
20const CONTENT_TYPE_V6: &str = "application/vnd.restate.invocation.v6";
21const CONTENT_TYPE_V7: &str = "application/vnd.restate.invocation.v7";
22
23impl Version {
24    pub const fn content_type(&self) -> &'static str {
25        match self {
26            Version::V1 => CONTENT_TYPE_V1,
27            Version::V2 => CONTENT_TYPE_V2,
28            Version::V3 => CONTENT_TYPE_V3,
29            Version::V4 => CONTENT_TYPE_V4,
30            Version::V5 => CONTENT_TYPE_V5,
31            Version::V6 => CONTENT_TYPE_V6,
32            Version::V7 => CONTENT_TYPE_V7,
33        }
34    }
35
36    pub const fn minimum_supported_version() -> Self {
37        Version::V5
38    }
39
40    pub const fn maximum_supported_version() -> Self {
41        Version::V7
42    }
43}
44
45impl fmt::Display for Version {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "{}", self.content_type())
48    }
49}
50
51#[derive(Debug, thiserror::Error)]
52#[error("unsupported protocol version '{0}'")]
53pub enum ContentTypeError {
54    #[error("unsupported protocol version '{0}'")]
55    RestateContentType(String),
56    #[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.")]
57    OtherContentType(String),
58}
59
60impl FromStr for Version {
61    type Err = ContentTypeError;
62
63    fn from_str(s: &str) -> Result<Self, Self::Err> {
64        match s {
65            CONTENT_TYPE_V1 => Ok(Version::V1),
66            CONTENT_TYPE_V2 => Ok(Version::V2),
67            CONTENT_TYPE_V3 => Ok(Version::V3),
68            CONTENT_TYPE_V4 => Ok(Version::V4),
69            CONTENT_TYPE_V5 => Ok(Version::V5),
70            CONTENT_TYPE_V6 => Ok(Version::V6),
71            CONTENT_TYPE_V7 => Ok(Version::V7),
72            s if s.starts_with("application/vnd.restate.invocation.") => {
73                Err(ContentTypeError::RestateContentType(s.to_owned()))
74            }
75            s => Err(ContentTypeError::OtherContentType(s.to_owned())),
76        }
77    }
78}