Skip to main content

coil_wasm/
ids.rs

1use std::fmt;
2
3use crate::error::WasmModelError;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub struct ContractVersion {
7    pub major: u16,
8    pub minor: u16,
9    pub patch: u16,
10}
11
12impl ContractVersion {
13    pub const fn new(major: u16, minor: u16, patch: u16) -> Self {
14        Self {
15            major,
16            minor,
17            patch,
18        }
19    }
20
21    pub const fn is_compatible_with(self, host: Self) -> bool {
22        self.major == host.major && self.minor == host.minor && self.patch <= host.patch
23    }
24}
25
26impl fmt::Display for ContractVersion {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
29    }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct ExtensionId(String);
34
35impl ExtensionId {
36    pub fn new(value: impl Into<String>) -> Result<Self, WasmModelError> {
37        Ok(Self(crate::validation::validate_token(
38            "extension_id",
39            value.into(),
40        )?))
41    }
42
43    pub fn as_str(&self) -> &str {
44        &self.0
45    }
46}
47
48impl fmt::Display for ExtensionId {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.write_str(&self.0)
51    }
52}
53
54#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
55pub struct HandlerId(String);
56
57impl HandlerId {
58    pub fn new(value: impl Into<String>) -> Result<Self, WasmModelError> {
59        Ok(Self(crate::validation::validate_token(
60            "handler_id",
61            value.into(),
62        )?))
63    }
64
65    pub fn as_str(&self) -> &str {
66        &self.0
67    }
68}
69
70impl fmt::Display for HandlerId {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        f.write_str(&self.0)
73    }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77pub enum HttpMethod {
78    Get,
79    Head,
80    Post,
81    Put,
82    Patch,
83    Delete,
84}
85
86impl fmt::Display for HttpMethod {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self {
89            Self::Get => f.write_str("GET"),
90            Self::Head => f.write_str("HEAD"),
91            Self::Post => f.write_str("POST"),
92            Self::Put => f.write_str("PUT"),
93            Self::Patch => f.write_str("PATCH"),
94            Self::Delete => f.write_str("DELETE"),
95        }
96    }
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
100pub enum ExtensionPointKind {
101    Page,
102    Api,
103    Job,
104    ScheduledJob,
105    Webhook,
106    AdminWidget,
107    RenderHook,
108}
109
110impl fmt::Display for ExtensionPointKind {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            Self::Page => f.write_str("page"),
114            Self::Api => f.write_str("api"),
115            Self::Job => f.write_str("job"),
116            Self::ScheduledJob => f.write_str("scheduled_job"),
117            Self::Webhook => f.write_str("webhook"),
118            Self::AdminWidget => f.write_str("admin_widget"),
119            Self::RenderHook => f.write_str("render_hook"),
120        }
121    }
122}