1use crate::error::{VtxError, VtxResult};
4use crate::modules::event::PluginEvent;
5use crate::modules::net::http::{Request, Response, ResponseBuilder};
6use crate::{Capabilities, Manifest, UserContext};
7
8pub trait VtxPlugin {
14 fn handle(_req: Request) -> VtxResult<Response> {
15 Ok(ResponseBuilder::not_found())
16 }
17
18 fn handle_event(_event: PluginEvent) -> VtxResult<()> {
19 Ok(())
20 }
21
22 fn get_migrations() -> Vec<String> {
23 Vec::new()
24 }
25
26 fn get_manifest() -> Manifest;
27
28 fn get_resources() -> Vec<String> {
29 Vec::new()
30 }
31
32 fn get_capabilities() -> Capabilities;
33
34 fn authenticate(_headers: &[(String, String)]) -> VtxResult<UserContext> {
40 Err(VtxError::AuthDenied(401))
41 }
42}
43
44#[macro_export]
51macro_rules! export_plugin {
52 ($plugin:ty) => {
53 $crate::export_plugin!($plugin => __VtxSdkGuest);
54 };
55 ($plugin:ty => $guest:ident) => {
56 struct $guest;
57
58 impl $crate::bindings::Guest for $guest {
59 fn handle(req: $crate::http::Request) -> $crate::http::Response {
60 match <$plugin as $crate::plugin::VtxPlugin>::handle(req) {
61 Ok(resp) => resp,
62 Err(err) => $crate::http::ResponseBuilder::error(err),
63 }
64 }
65
66 fn handle_event(event: $crate::event::PluginEvent) -> Result<(), String> {
67 match <$plugin as $crate::plugin::VtxPlugin>::handle_event(event) {
68 Ok(()) => Ok(()),
69 Err(err) => Err(err.to_string()),
70 }
71 }
72
73 fn get_migrations() -> Vec<String> {
74 <$plugin as $crate::plugin::VtxPlugin>::get_migrations()
75 }
76
77 fn get_manifest() -> $crate::Manifest {
78 <$plugin as $crate::plugin::VtxPlugin>::get_manifest()
79 }
80
81 fn get_resources() -> Vec<String> {
82 <$plugin as $crate::plugin::VtxPlugin>::get_resources()
83 }
84
85 fn get_capabilities() -> $crate::Capabilities {
86 <$plugin as $crate::plugin::VtxPlugin>::get_capabilities()
87 }
88
89 fn authenticate(
90 headers: Vec<(String, String)>,
91 ) -> Result<$crate::UserContext, u16> {
92 use $crate::auth::IntoAuthResult as _;
93 <$plugin as $crate::plugin::VtxPlugin>::authenticate(&headers).into_auth_result()
94 }
95 }
96
97 $crate::export!($guest);
98 };
99}