Skip to main content

coil_wasm/invocation/
input.rs

1use crate::error::WasmModelError;
2use crate::ids::{ExtensionPointKind, HttpMethod};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct PageInvocation {
6    pub route: String,
7    pub method: HttpMethod,
8}
9
10impl PageInvocation {
11    pub fn new(route: impl Into<String>, method: HttpMethod) -> Result<Self, WasmModelError> {
12        Ok(Self {
13            route: crate::validation::validate_route("page_invocation_route", route.into())?,
14            method,
15        })
16    }
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct ApiInvocation {
21    pub route: String,
22    pub method: HttpMethod,
23}
24
25impl ApiInvocation {
26    pub fn new(route: impl Into<String>, method: HttpMethod) -> Result<Self, WasmModelError> {
27        Ok(Self {
28            route: crate::validation::validate_route("api_invocation_route", route.into())?,
29            method,
30        })
31    }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct JobInvocation {
36    pub job_name: String,
37    pub attempt: u32,
38}
39
40impl JobInvocation {
41    pub fn new(job_name: impl Into<String>, attempt: u32) -> Result<Self, WasmModelError> {
42        Ok(Self {
43            job_name: crate::validation::validate_token("job_invocation_name", job_name.into())?,
44            attempt,
45        })
46    }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct ScheduledJobInvocation {
51    pub job_name: String,
52}
53
54impl ScheduledJobInvocation {
55    pub fn new(job_name: impl Into<String>) -> Result<Self, WasmModelError> {
56        Ok(Self {
57            job_name: crate::validation::validate_token(
58                "scheduled_job_invocation_name",
59                job_name.into(),
60            )?,
61        })
62    }
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct WebhookInvocation {
67    pub source: String,
68    pub event: String,
69    pub verified: bool,
70    pub replay_protected: bool,
71}
72
73impl WebhookInvocation {
74    pub fn new(
75        source: impl Into<String>,
76        event: impl Into<String>,
77        verified: bool,
78        replay_protected: bool,
79    ) -> Result<Self, WasmModelError> {
80        Ok(Self {
81            source: crate::validation::validate_token("webhook_invocation_source", source.into())?,
82            event: crate::validation::validate_token("webhook_invocation_event", event.into())?,
83            verified,
84            replay_protected,
85        })
86    }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
90pub struct AdminWidgetInvocation {
91    pub slot: String,
92}
93
94impl AdminWidgetInvocation {
95    pub fn new(slot: impl Into<String>) -> Result<Self, WasmModelError> {
96        Ok(Self {
97            slot: crate::validation::validate_token("admin_widget_invocation_slot", slot.into())?,
98        })
99    }
100}
101
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub struct RenderHookInvocation {
104    pub slot: String,
105}
106
107impl RenderHookInvocation {
108    pub fn new(slot: impl Into<String>) -> Result<Self, WasmModelError> {
109        Ok(Self {
110            slot: crate::validation::validate_token("render_hook_invocation_slot", slot.into())?,
111        })
112    }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq)]
116pub enum InvocationInput {
117    Page(PageInvocation),
118    Api(ApiInvocation),
119    Job(JobInvocation),
120    ScheduledJob(ScheduledJobInvocation),
121    Webhook(WebhookInvocation),
122    AdminWidget(AdminWidgetInvocation),
123    RenderHook(RenderHookInvocation),
124}
125
126impl InvocationInput {
127    pub fn kind(&self) -> ExtensionPointKind {
128        match self {
129            Self::Page(_) => ExtensionPointKind::Page,
130            Self::Api(_) => ExtensionPointKind::Api,
131            Self::Job(_) => ExtensionPointKind::Job,
132            Self::ScheduledJob(_) => ExtensionPointKind::ScheduledJob,
133            Self::Webhook(_) => ExtensionPointKind::Webhook,
134            Self::AdminWidget(_) => ExtensionPointKind::AdminWidget,
135            Self::RenderHook(_) => ExtensionPointKind::RenderHook,
136        }
137    }
138}