Writing mods
A mod is one or more .luma files that you share, so others can import them. Luma’s own database drivers and AI clients are mods, written in Luma just like yours. There’s nothing to register and no package format: a mod is code.
Make one
A mod is an ordinary Luma file. What other files may use is marked pub; everything else stays inside. Start the file with a comment that says what the mod is for and how to use it.
File slug.luma:
// slug: turns titles into the words of a web address.
//
// import "slug"
// print(slug.make("Hello, World!")) // hello-world
// make gives the title in lower case, its words joined by "-",
// with everything but letters a-z and digits left out.
pub fn make(title: str): str {
words: [str] = []
word: str = ""
title.lower().chars().walk(c) {
if (c >= "a" && c <= "z") || (c >= "0" && c <= "9") {
word = "${word}${c}"
} else if len(word) > 0 {
words[] = word
word = ""
}
}
if len(word) > 0 {
words[] = word
}
return words.join("-")
}A program beside it uses it:
import "slug"
print(slug.make("Hello, World!"))hello-worldA bigger mod can have several files: sqlite.luma imports sqlite/pager.luma, sqlite/btree.luma and others. Keep them in the mod’s own folder. See Imports and mods for how pub and imports work.
Test it
Put tests in a file named after the mod, with _test. They see all of the mod’s names, pub or not.
File slug_test.luma:
fn test_make() {
assert make("Hello, World!") == "hello-world"
assert make(" Luma 0.2.0 ") == "luma-0-2-0"
assert make("") == ""
}luma test slug.luma # interpreted
luma test -build slug.luma # and compiledRun both: a mod should behave the same in both modes, and Luma makes sure it does. See Testing.
Publish it
Put the files on any web server that serves plain files: your own site, a file host, or a code-hosting service that serves raw files. Keep the folders as they are on your disk. Then anyone can get it:
luma get https://example.com/mods/slug.lumaluma get downloads the file and every file it imports, from beside it on the web, and checks each one before writing anything. To publish an update, replace the files; running luma get again shows what changed.
Good mods
- Pure Luma. Mods are written in Luma, so they run wherever Luma runs, in both modes, and people can read them.
- A clear header comment: what the mod does, the main functions, a short example.
- Small and focused. One job, done well, with few
pubnames. - Errors with meaning.
throw error(code, message)with a message that says what went wrong and what to do. - Tests, run with
luma testandluma test -build. - Tell people: share it in the issue tracker, so others can find it.