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