Skip to main content

ordinary_modify/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(clippy::all, clippy::pedantic)]
3#![allow(clippy::missing_errors_doc)]
4
5// Copyright (C) 2026 Ordinary Labs, LLC.
6//
7// SPDX-License-Identifier: AGPL-3.0-only
8
9pub mod content;
10pub mod project;
11
12use anyhow::bail;
13use heck::ToTitleCase;
14use ordinary_config::{
15    ActionConfig, ActionFfi, ActionFfiSerialization, ActionFfiVersion, ErrorConfig,
16    IntegrationConfig, IntegrationProtocol, ModelConfig, OrdinaryConfig, TemplateConfig,
17    TemplateFfi, TemplateFfiSerialization, TemplateFfiVersion, TemplateRef, UuidVersion,
18};
19use std::{io::Write, path::Path};
20use tracing::instrument;
21
22#[instrument(err)]
23#[allow(
24    clippy::too_many_arguments,
25    clippy::used_underscore_binding,
26    clippy::too_many_lines
27)]
28pub fn add_action(
29    path: &str,
30    name: &str,
31    lang: &str,
32    _access: (),
33    _accepts: (),
34    _returns: (),
35    transactional: &Option<bool>,
36    _protected: (),
37) -> anyhow::Result<()> {
38    let proj_path = Path::new(path);
39    let mut app_config = OrdinaryConfig::get(path)?;
40
41    let mut actions = app_config.actions.unwrap_or_default();
42
43    if actions.len() <= 225 {
44        let next_idx = actions.len();
45
46        let language = match lang {
47            "Rust" | "rust" | "rs" => ordinary_config::ActionLang::Rust,
48            "JavaScript" | "javascript" | "JS" | "js" => ordinary_config::ActionLang::JavaScript,
49            _ => bail!("language {lang} not yet supported"),
50        };
51
52        let action_dir_path = proj_path.join("actions").join(name);
53        fs_err::create_dir_all(&action_dir_path)?;
54
55        match language {
56            ordinary_config::ActionLang::Rust => {
57                let gitignore = "/target";
58
59                let cargo_toml = format!(
60                    r#"[workspace]
61
62[package]
63name = "action"
64version = "0.1.0"
65edition = "2024"
66
67[dependencies]
68ordinary = {{ path = "../../.ordinary/gen/actions/{name}" }}
69
70[profile.release]
71strip = "symbols"
72lto = "fat"
73opt-level = "z"
74codegen-units = 1
75panic = "abort"
76"#
77                );
78
79                let main_rs = r#"use std::error::Error;
80use ordinary::{recv_in, send_out};
81
82fn main() -> Result<(), Box<dyn Error>> {
83    let _input = recv_in()?;
84
85    ordinary::trace(2, "Hello, Ordinary!")?;
86
87    send_out(())
88}
89"#;
90
91                let mut gitignore_file = fs_err::File::create(action_dir_path.join(".gitignore"))?;
92                gitignore_file.write_all(gitignore.as_bytes())?;
93
94                let mut cargo_toml_file = fs_err::File::create(action_dir_path.join("Cargo.toml"))?;
95                cargo_toml_file.write_all(cargo_toml.as_bytes())?;
96
97                fs_err::create_dir_all(action_dir_path.join("src"))?;
98                let mut main_rs_file =
99                    fs_err::File::create(action_dir_path.join("src").join("main.rs"))?;
100                main_rs_file.write_all(main_rs.as_bytes())?;
101            }
102            ordinary_config::ActionLang::JavaScript => {
103                let gitignore = r"/target
104node_modules";
105
106                let package_json = r#"{
107  "name": "action",
108  "version": "0.1.0",
109  "main": "index.js",
110  "files": [
111    "index.js",
112    "index.d.ts"
113  ],
114  "types": "client.d.ts",
115  "scripts": {
116    "build": "esbuild main.js --bundle --minify --outfile=index.js --target=es2020 --format=iife --global-name=action"
117  },
118  "devDependencies": {
119    "esbuild": "0.25.10"
120  }
121}
122                "#;
123
124                let main_js = r#"export function main(input) {
125    console.log("Hello, Ordinary!");
126}"#;
127
128                let mut gitignore_file = fs_err::File::create(action_dir_path.join(".gitignore"))?;
129                gitignore_file.write_all(gitignore.as_bytes())?;
130
131                let mut package_json_file =
132                    fs_err::File::create(action_dir_path.join("package.json"))?;
133                package_json_file.write_all(package_json.as_bytes())?;
134
135                let mut main_js_file = fs_err::File::create(action_dir_path.join("main.js"))?;
136                main_js_file.write_all(main_js.as_bytes())?;
137            }
138        }
139
140        actions.push(ActionConfig {
141            ffi: ActionFfi {
142                version: ActionFfiVersion::V1,
143                serialization: ActionFfiSerialization::FlexBufferVector,
144            },
145            idx: u8::try_from(next_idx)?,
146            dir_path: Some(format!("./actions/{name}")),
147            name: name.to_string(),
148            lang: language,
149            access: vec![],
150            accepts: ordinary_types::Kind::Void,
151            returns: ordinary_types::Kind::Void,
152            triggered_by: vec![],
153            transactional: *transactional,
154            protected: None,
155            wasm_opt: None,
156            timeout: None,
157            cors: None,
158            privileged: None,
159            variables: None,
160        });
161
162        app_config.actions = Some(actions);
163
164        let ordinary_json = serde_json::to_string_pretty(&app_config)?;
165
166        let mut file = fs_err::File::create(proj_path.join("ordinary.json"))?;
167        file.write_all(ordinary_json.as_bytes())?;
168    } else {
169        tracing::error!("cannot support more than 255 actions for a single project.");
170    }
171
172    Ok(())
173}
174
175#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
176#[instrument(skip_all, err)]
177pub fn add_template(
178    path: &str,
179    name: &str,
180    route: &str,
181    mime: &str,
182    head_block: &str,
183    header_block: &str,
184    footer_block: &str,
185    is_error: bool,
186    globals: Option<Vec<String>>,
187    content_refs: Option<Vec<TemplateRef>>,
188) -> anyhow::Result<()> {
189    let proj_path = Path::new(path);
190    let mut app_config = OrdinaryConfig::get(path)?;
191
192    let mut templates = app_config.templates.unwrap_or_default();
193
194    if templates.len() <= 255 {
195        let next_idx = templates.len();
196
197        let cache = None;
198
199        let file_ext = match mime {
200            "text/html" | "text/html; charset=utf-8" => "html",
201            "text/xml" | "application/rss+xml" => "xml",
202            "text/plain" | "text/plain; charset=utf-8" => "txt",
203            _ => "",
204        };
205
206        fs_err::create_dir_all(proj_path.join("templates"))?;
207
208        let file_name = format!("{name}.{file_ext}");
209        let mut file = fs_err::File::create(proj_path.join("templates").join(&file_name))?;
210
211        let header_block = if header_block.is_empty() {
212            r#"<header>
213                <a href="/">{{ canonical }}</a>
214            </header>"#
215        } else {
216            header_block
217        };
218
219        let footer_block = if footer_block.is_empty() {
220            r#"<footer>
221                © 2026 Your Name | powered by&nbsp;<a href="https://codeberg.org/ordinarylabs/Ordinary">Ordinary</a>
222            </footer>"#
223        } else {
224            footer_block
225        };
226
227        // todo: have a default file for each extension/mime type
228        if mime == "text/html" {
229            let main_block = if is_error {
230                "<h1>Status Code {{ error.code }}</h1>
231                <p>{{ error.message }}</p>"
232                    .to_string()
233            } else {
234                format!("<h1>{}</h1>", name.to_title_case())
235            };
236
237            let default_html = format!(
238                r#"<!DOCTYPE html>
239        <html lang="en">
240
241        <head>
242            <meta charset="UTF-8">
243            <meta name="viewport" content="width=device-width, initial-scale=1.0">
244
245            <title>{name} | {{{{ canonical }}}}</title>
246            <meta name="generator" content="{{{{ generator }}}}">
247        {head_block}
248        </head>
249
250        <body>
251            {header_block}
252            <main>
253                {main_block}
254            </main>
255            {footer_block}
256        </body>
257        "#,
258            );
259            file.write_all(default_html.as_bytes())?;
260            file.flush()?;
261        } else {
262            file.write_all(&[][..])?;
263            file.flush()?;
264        }
265
266        templates.push(TemplateConfig {
267            ffi: TemplateFfi {
268                version: TemplateFfiVersion::V1,
269                serialization: TemplateFfiSerialization::FlexBufferVector,
270            },
271            idx: u8::try_from(next_idx)?,
272            name: name.to_string(),
273            // todo: verify that route begins with "/" and is valid HTTP route
274            route: route.to_string(),
275            // todo: check against valid mime types
276            mime: mime.to_string(),
277            cache,
278            path: Some(format!("./templates/{file_name}")),
279            globals,
280            content: content_refs,
281            ..Default::default()
282        });
283
284        app_config.templates = Some(templates);
285        if is_error {
286            app_config.error = Some(ErrorConfig {
287                template: Some(name.to_string()),
288                asset: None,
289            });
290        }
291
292        let ordinary_json = serde_json::to_string_pretty(&app_config)?;
293
294        let mut file = fs_err::File::create(proj_path.join("ordinary.json"))?;
295        file.write_all(ordinary_json.as_bytes())?;
296    } else {
297        tracing::error!("cannot support more than 255 templates for a single project.");
298    }
299
300    Ok(())
301}
302
303#[instrument(err)]
304pub fn add_integration(
305    path: &str,
306    name: &str,
307    endpoint: &str,
308    protocol: &str,
309) -> anyhow::Result<()> {
310    let proj_path = Path::new(path);
311    let mut app_config = OrdinaryConfig::get(path)?;
312
313    let mut integrations = app_config.integrations.unwrap_or_default();
314
315    if integrations.len() <= 255 {
316        let next_idx = integrations.len();
317
318        integrations.push(IntegrationConfig {
319            idx: u8::try_from(next_idx)?,
320            name: name.to_string(),
321            protocol: match protocol {
322                "JSON" => IntegrationProtocol::Http {
323                    method: "GET".to_string(),
324                    headers: vec![],
325                    send_encoding: ordinary_config::IntegrationProtocolHttpEncoding::Text,
326                    recv_encoding: ordinary_config::IntegrationProtocolHttpEncoding::Text,
327                },
328                _ => bail!("invalid protocol"),
329            },
330            endpoint: endpoint.to_string(),
331            send: ordinary_types::Kind::Json,
332            recv: ordinary_types::Kind::Json,
333            secrets: None,
334            timeout: None,
335        });
336
337        app_config.integrations = Some(integrations);
338
339        let ordinary_json = serde_json::to_string_pretty(&app_config)?;
340
341        let mut file = fs_err::File::create(proj_path.join("ordinary.json"))?;
342        file.write_all(ordinary_json.as_bytes())?;
343    } else {
344        tracing::error!("cannot support more than 255 integrations for a single project.");
345    }
346
347    Ok(())
348}
349
350#[instrument(err)]
351pub fn add_model(path: &str, name: &str, uuid: Option<&str>) -> anyhow::Result<()> {
352    let proj_path = Path::new(path);
353    let mut app_config = OrdinaryConfig::get(path)?;
354
355    let mut models = app_config.models.unwrap_or_default();
356
357    if models.len() <= 255 {
358        let next_idx = models.len();
359
360        models.push(ModelConfig {
361            idx: u8::try_from(next_idx)?,
362            name: name.to_string(),
363            fields: vec![],
364            uuid: match uuid {
365                Some(uuid) => match uuid {
366                    "v4" => Some(UuidVersion::V4),
367                    "v7" => Some(UuidVersion::V7),
368                    _ => bail!("invalid UUID version"),
369                },
370                None => None,
371            },
372        });
373
374        app_config.models = Some(models);
375
376        let ordinary_json = serde_json::to_string_pretty(&app_config)?;
377
378        let mut file = fs_err::File::create(proj_path.join("ordinary.json"))?;
379        file.write_all(ordinary_json.as_bytes())?;
380    } else {
381        tracing::error!("cannot support more than 255 models for a single project.");
382    }
383
384    Ok(())
385}