Skip to main content

trailbase_wasm_common/
manifest.rs

1use serde::{Deserialize, Serialize};
2use ts_rs::TS;
3
4#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, TS)]
5#[serde(rename_all = "snake_case")]
6pub enum HttpMethodType {
7  Get,
8  Post,
9  Head,
10  Options,
11  Patch,
12  Delete,
13  Put,
14  Trace,
15  Connect,
16}
17
18#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, TS)]
19#[serde(rename_all = "snake_case")]
20pub enum Subsystem {
21  Metadata,
22  Http,
23  Jobs,
24  SqliteFunctions,
25  #[serde(other)]
26  Unknown,
27}
28
29#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, TS)]
30#[serde(rename_all = "snake_case")]
31pub enum GuestRuntime {
32  Rust,
33  EcmaScript,
34  #[serde(other)]
35  Unknown,
36}
37
38#[derive(Clone, Debug, Deserialize, Serialize, TS)]
39pub struct HttpRoute {
40  pub method: HttpMethodType,
41  pub path: String,
42}
43
44#[derive(Clone, Debug, Deserialize, Serialize, TS)]
45pub struct Job {
46  /// Name of the job.
47  pub name: String,
48  /// Cron spec, e.g. "37 * * * * *".
49  pub spec: String,
50  /// Optional timeout in [ms].
51  pub timeout: Option<u64>,
52}
53
54#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, TS)]
55pub enum SqliteFunctionFlag {
56  /// Specifies UTF-8 as the text encoding this SQL function prefers for its parameters.
57  Utf8,
58  /// Specifies UTF-16 using little-endian byte order as the text encoding this SQL function prefers
59  /// for its parameters.
60  Utf16le,
61  /// Specifies UTF-16 using big-endian byte order as the text encoding this SQL function prefers
62  /// for its parameters.
63  Utf16be,
64  /// Specifies UTF-16 using native byte order as the text encoding this SQL function prefers for
65  /// its parameters.
66  Utf16,
67  /// Means that the function always gives the same output when the input parameters are the same.
68  Deterministic,
69  /// Means that the function may only be invoked from top-level SQL.
70  DirectOnly,
71  /// Indicates to SQLite that a function may call `sqlite3_value_subtype()` to inspect the subtypes
72  /// of its arguments.
73  Subtype,
74  /// Means that the function is unlikely to cause problems even if misused.
75  Innocuous,
76  /// Indicates to SQLite that a function might call `sqlite3_result_subtype()` to cause a subtype
77  /// to be associated with its result.
78  ResultSubtype,
79  /// Indicates that the function is an aggregate that internally orders the values provided to the
80  /// first argument.
81  Selforder1,
82}
83
84#[derive(Clone, Debug, Deserialize, Serialize, TS)]
85pub struct SqliteScalarFunction {
86  pub name: String,
87  pub num_args: u32,
88  pub flags: Vec<SqliteFunctionFlag>,
89}
90
91#[derive(Clone, Debug, Deserialize, Serialize, TS)]
92pub enum SqliteFunction {
93  Scalar(SqliteScalarFunction),
94}
95
96/// Metadata a component can self-report for display in the admin WASM modules page.
97#[derive(Clone, Debug, Default, Deserialize, Serialize, TS)]
98#[ts(optional_fields)]
99pub struct Metadata {
100  /// Name to show in the admin UI component browser.
101  pub display_name: Option<String>,
102  /// Optional description to show in the admin UI.
103  pub description: Option<String>,
104  /// Icon to show in the admin UI component browser, e.g. "<svg ..." or
105  /// "data:<mime-type>;base64,<base64-encoded-data>".
106  pub icon: Option<String>,
107  /// Which guest runtime is used by the component.
108  pub guest_runtime: Option<GuestRuntime>,
109  /// The component's version.
110  pub version: Option<String>,
111  /// An HTTP endpoint for an admin UI to present in the admin dashboard's, e.g.: "/comp0/admin".
112  pub admin_ui_path: Option<String>,
113}
114
115impl std::fmt::Display for Metadata {
116  fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117    return write!(
118      fmt,
119      "Metadata(display_name={}, rt={:?})",
120      self.display_name.as_deref().unwrap_or("??"),
121      self.guest_runtime.unwrap_or(GuestRuntime::Unknown),
122    );
123  }
124}
125
126#[derive(Clone, Debug, Default, Deserialize, Serialize, TS)]
127#[ts(export)]
128pub struct InitArguments {
129  // Host version.
130  pub version: Option<String>,
131
132  // List of subsystems to initialize.
133  pub subsystems: Option<Vec<Subsystem>>,
134}
135
136#[derive(Clone, Debug, Default, Deserialize, Serialize, TS)]
137#[ts(export)]
138pub struct InitManifest {
139  /// Metadata for the WASM component, useful e.g. for the admin page.
140  pub metadata: Option<Metadata>,
141
142  /// Registered HTTP handlers tuple of Method + Path. May contain wild cards.
143  pub http_handlers: Option<Vec<HttpRoute>>,
144
145  /// Registered JobHandlers.
146  pub job_handlers: Option<Vec<Job>>,
147
148  /// Registered Sqlite functions.
149  pub sqlite_functions: Option<Vec<SqliteFunction>>,
150}