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