| Crates.io | musubi-rs |
| lib.rs | musubi-rs |
| version | 0.4.0 |
| created_at | 2025-12-03 04:41:56.841943+00 |
| updated_at | 2025-12-12 15:28:11.034889+00 |
| description | Rust bindings for musubi diagnostic renderer |
| homepage | https://github.com/starwing/musubi |
| repository | https://github.com/starwing/musubi |
| max_upload_size | |
| id | 1963361 |
| size | 242,280 |
A beautiful diagnostics renderer for compiler errors and warnings
Overview • Key Features • Installation • Quick Start • C API • Lua API • Testing
📦 For Rust Users: This README covers the complete Musubi project (Lua/C/Rust implementations).
Looking for Rust API documentation? → See the comprehensive Rust API docs with examples and usage guides.
Rust crate:musubi-rs
Musubi (結び, "connection" in Japanese) is a high-performance diagnostics renderer inspired by Rust's Ariadne library. It produces beautiful, color-coded diagnostic messages with precise source location highlighting, multi-line spans, and intelligent label clustering.
Originally ported from Rust's Ariadne library, Musubi has evolved into a production-ready multi-language implementation:
musubi.h, musubi.c)musubi-rs)Both implementations produce identical output and are thoroughly tested (26 Rust unit tests + 30 doc tests, 100 Lua tests).
✨ Beautiful Output
🚀 Performance Optimized
🎯 Improved Implementation
🛡️ Production Ready
local mu = require "musubi"
local cg = mu.colorgen()
print(
mu.report(12)
:code "3"
:title("Error", "Incompatible types")
:label(33, 33):message("This is of type Nat"):color(cg:next())
:label(43, 45):message("This is of type Str"):color(cg:next())
:label(12, 48):message("This values are outputs of this match expression"):color(cg:next())
:label(1, 48):message("The definition has a problem"):color(cg:next())
:label(51, 76):message("Usage of definition here"):color(cg:next())
:note "Outputs of match expressions must coerce to the same type"
:source([[
def five = match () in {
() => 5,
() => "5",
}
def six =
five
+ 1
]], "sample.tao")
:render())
Output:
Rust Crate:
C Library with Lua Bindings:
lcov for coverage reportsRust:
cargo add musubi-rs
C Library with Lua Bindings:
# Compile shared library
gcc -O3 -Wall -shared -fPIC -o musubi.so musubi.c -llua
# Or with coverage instrumentation
gcc -shared -fPIC --coverage -o musubi.so musubi.c -llua
macOS:
gcc -O3 -Wall -shared -undefined dynamic_lookup -o musubi.so musubi.c
local mu = require "musubi"
-- Create a color generator for automatic color cycling
local cg = mu.colorgen()
-- Build a report
local report = mu.report(14) -- Primary error position
:title("Error", "Something went wrong")
:code("E001")
:label(14, 14):message("This is the problem"):color(cg:next())
:note("Try fixing this by...")
:source("local x = 10 + 'hello'", "example.js")
:render()
print(report)
local mu = require "musubi"
local cfg = mu.config()
:compact(true) -- Enable compact mode
:cross_gap(true) -- Draw arrows across line gaps
:tab_width(4) -- Tab expansion width
:limit_width(80) -- Truncate long lines to 80 columns
:char_set "unicode" -- Use Unicode box-drawing characters
:index_type "char" -- Use character offsets (vs "byte")
:ambi_width(1) -- Ambiguous character width (1 or 2)
:column_order(false) -- Use natural label ordering (default)
:align_messages(true) -- Align label messages (default)
mu.report(0)
:config(cfg)
-- ... rest of report
local mu = require "musubi"
mu.report(0)
:label(10, 20, 1):message("Defined here") -- src_id=1, first source
:label(50, 60, 2):message("Used here") -- src_id=2, second source
:source("fn foo() { ... }", "foo.rs")
:source("fn bar() { foo(); }", "bar.rs")
:render()
local mu = require "musubi"
local io = require "io"
local fp = io.open("large_file.txt", "r")
mu.report(0)
:source(fp, "large_file.txt") -- Streams file on-demand
:label(100, 150):message("Error in large file")
:render()
Notice that if you use file handle on Windows, the musubi.so must not be built as static linking (/MT).
musubi is an stb-style single-header library. You only need musubi.h - no separate compilation or linking required. Also see sokol for more examples of stb-style libraries.
In ONE C/C++ file (typically your main file), define MU_IMPLEMENTATION before including:
#define MU_IMPLEMENTATION
#include "musubi.h"
In all other files, just include the header normally:
#include "musubi.h" // Only declarations, no implementation
For single-file projects, use MU_STATIC_API to make all functions static:
#define MU_STATIC_API // Automatically defines MU_IMPLEMENTATION
#include "musubi.h"
#define MU_IMPLEMENTATION
#include <stdio.h>
#include <string.h>
#include "musubi.h"
static int stdout_writer(void *ud, const char *data, size_t len) {
fwrite(data, 1, len, stdout);
return 0; /* Success */
}
int main(void) {
mu_Report *R;
mu_Cache *C = NULL;
mu_ColorGen cg;
mu_ColorCode color1;
/* Initialize color generator */
mu_initcolorgen(&cg, 0.5f);
mu_gencolor(&cg, &color1);
/* Create Cache and add a source */
mu_addmemory(&C, mu_literal("local x = 10 + 'hello'"),
mu_literal("example.lua"));
/* Create Report and configure */
R = mu_new(NULL, NULL); /* NULL, NULL = use default malloc */
mu_title(R, MU_ERROR, mu_literal(""), mu_literal("Type mismatch"));
mu_code(R, mu_literal("E001"));
/* Add a label with message and color */
mu_label(R, 15, 22, 0);
mu_message(R, mu_literal("expected number, got string"), 0);
mu_color(R, mu_fromcolorcode, &color1);
/* Render to stdout */
mu_writer(R, stdout_writer, NULL);
mu_render(R, 14, C);
/* Cleanup */
mu_delete(R);
mu_delcache(C);
return 0;
}
Key Concept: mu_Source IS-A mu_Cache. A single Source can be used wherever Cache is expected:
mu_Cache *C = NULL; /* Start with NULL Cache */
mu_Source *S = mu_addmemory(&C, content, name); /* Auto-upgrades C if needed */
mu_render(R, pos, (mu_Cache*)S); /* Source can be used as Cache */
mu_render(R, pos, C); /* Or use C directly, as it have been updated with same source */
Auto-Upgrade Mechanism:
mu_addmemory(&C, ...) where C == NULL creates a single Sourcemu_addmemory(&C, ...) automatically upgrades to multi-Source Cachemu_addsource(&C, ...) with double pointerLifecycle Management:
C = mu_newcache(allocf, ud) with allocator or start with C = NULLmu_addmemory(&C, ...) or mu_addfile(&C, ...)mu_render(R, pos, C) uses Cache to fetch source lines, pos is always pointed to the first source (id 0) in cache.mu_delcache(C) frees Cache and all SourcesOwnership Rules:
mu_addmemory / mu_addfilemu_delcache(C) manuallymu_Slice) must outlive mu_render() callmu_Cache *C = NULL; /* Start with NULL */
mu_Source *S1 = mu_addmemory(&C, mu_lslice("fn foo() { }", 12),
mu_lslice("foo.c", 5));
mu_Source *S2 = mu_addmemory(&C, mu_lslice("fn bar() { foo(); }", 19),
mu_lslice("bar.c", 5));
/* Cross-file diagnostic */
mu_Report *R = mu_new(NULL, NULL);
mu_title(R, MU_ERROR, mu_literal(""), mu_literal("Undefined reference"));
mu_label(R, 11, 14, 1); /* bar.c: source id 1 */
mu_message(R, mu_literal("called here"), 0);
mu_label(R, 3, 6, 0); /* foo.c: source id 0 */
mu_message(R, mu_literal("defined here"), 0);
mu_writer(R, stdout_writer, NULL); /* See Basic Example for stdout_writer */
mu_render(R, 11, C); /* Position in foo.c (source id 0) */
mu_delete(R);
mu_delcache(C); /* Frees both S1 and S2 */
For large files, use mu_addfile to stream content on-demand:
mu_Cache *C = NULL;
FILE *fp = fopen("large_file.c", "r");
mu_Source *S = mu_addfile(&C, fp, mu_lslice("large_file.c", 12));
/* musubi reads lines only when needed for rendering */
mu_render(R, pos, C);
fclose(fp); /* Close after rendering */
mu_delcache(C);
Important:
mu_render() callmu_addfile(&C, NULL, path) opens file internally - musubi will close it on mu_delcache()FILE*, you must close it yourself after renderingAll API functions return int error codes:
int err;
err = mu_label(R, 10, 20, 0);
if (err != MU_OK) {
switch (err) {
case MU_ERRPARAM: fprintf(stderr, "Invalid parameter\n"); break;
case MU_ERRSRC: fprintf(stderr, "Source not found\n"); break;
case MU_ERRFILE: fprintf(stderr, "File I/O error\n"); break;
}
mu_delete(R);
return 1;
}
err = mu_render(R, pos, C);
if (err != MU_OK) {
/* Handle error */
}
Provide custom allocator for memory control:
void* my_alloc(void *ud, void *ptr, size_t nsize, size_t osize) {
void *newptr;
if (nsize == 0) {
free(ptr);
return NULL;
}
newptr = realloc(ptr, nsize);
if (newptr == NULL) {
/* handle out-of-memory yourself, or musubi may abort */
}
return newptr;
}
void *my_userdata = /* your context */;
mu_Cache *C = mu_newcache(my_alloc, my_userdata);
mu_Report *R = mu_new(my_alloc, my_userdata);
If alloc fails (returns NULL), you must jumps out of current flow (e.g., longjmp), or musubi may abort due to out-of-memory.
Allocator signature: void* (*mu_Allocf)(void *ud, void *ptr, size_t nsize, size_t osize)
ptr == NULL: Allocate nsize bytesnsize == 0: Free ptr (allocated with osize bytes)ptr from osize to nsize bytesTypes:
mu_Report - Diagnostic report buildermu_Cache - Multi-source containermu_Source - Single source (can be used as Cache)mu_Slice - String slice {const char *p, *e}mu_ColorGen - Color generator statemu_ColorCode - Pre-generated color code buffer char[32]mu_Allocf - Allocator function typemu_Writer - Output writer function type int (*)(void *ud, const char *data, size_t len)mu_Color - Color generator function type mu_Chunk (*)(void *ud, mu_ColorKind kind)Cache Management:
mu_Cache* mu_newcache(mu_Allocf *allocf, void *ud) - Create empty Cachevoid mu_delcache(mu_Cache *C) - Free Cache and all Sourcesmu_Source* mu_addmemory(mu_Cache **pC, mu_Slice content, mu_Slice name) - Add in-memory sourcemu_Source* mu_addfile(mu_Cache **pC, FILE *fp, mu_Slice path) - Add file sourceunsigned mu_sourcecount(const mu_Cache *C) - Get number of sourcesReport Building:
mu_Report* mu_new(mu_Allocf *allocf, void *ud) - Create new Reportvoid mu_delete(mu_Report *R) - Free Reportvoid mu_reset(mu_Report *R) - Reset Report for reuseint mu_title(mu_Report *R, mu_Level level, mu_Slice custom, mu_Slice msg) - Set kind and titleint mu_code(mu_Report *R, mu_Slice code) - Set error codeint mu_label(mu_Report *R, size_t start, size_t end, mu_Id src_id) - Add label spanint mu_message(mu_Report *R, mu_Slice msg, int width) - Set message for last labelint mu_color(mu_Report *R, mu_Color *color, void *ud) - Set color function for last labelint mu_order(mu_Report *R, int order) - Set order for last labelint mu_priority(mu_Report *R, int priority) - Set priority for last labelint mu_note(mu_Report *R, mu_Slice note) - Add footer noteint mu_help(mu_Report *R, mu_Slice help) - Add help textRendering:
int mu_writer(mu_Report *R, mu_Writer *fn, void *ud) - Set output writer functionint mu_render(mu_Report *R, size_t pos, const mu_Cache *C) - Render diagnosticConfiguration:
void mu_initconfig(mu_Config *cfg) - Initialize config with defaultsint mu_config(mu_Report *R, const mu_Config *cfg) - Apply configurationColor Generation:
void mu_initcolorgen(mu_ColorGen *cg, float min_brightness) - Initialize color generatorvoid mu_gencolor(mu_ColorGen *cg, mu_ColorCode *out) - Generate next color codemu_Chunk mu_fromcolorcode(void *ud, mu_ColorKind kind) - Color function for pre-generated codesmu_Chunk mu_default_color(void *ud, mu_ColorKind kind) - Default color schemeUtilities:
mu_Slice mu_lslice(const char *s, size_t len) - Create slice with explicit lengthmu_literal("text") - Macro: create slice from string literal (compile-time length)mu_slice(str) - Macro: create slice from C string (uses strlen)Constants:
MU_OK (0), MU_ERRPARAM (-1), MU_ERRSRC (-2), MU_ERRFILE (-3)MU_ERROR, MU_WARNING, MU_CUSTOM_LEVELFor complete API documentation, see musubi.h header file and .github/c_port.md.
| Method | Description |
|---|---|
mu.report(pos, src_id?) |
Create a new report at position pos |
:title(level, message) |
Set report level ("Error", "Warning") and title |
:code(code) |
Set optional error code (e.g., "E0308") |
:label(start, end?, src_id?) |
Add a label span (half-open interval [start, end)) |
:message(text, width?) |
Attach message to the last added label |
:color(color) |
Set color for the last added label |
:order(n) |
Set display order for the last label |
:priority(n) |
Set priority for clustering |
:note(text) |
Add a note to the footer |
:help(text) |
Add a help message to the footer |
:source(content, name?, offset?) |
Register a source (string or FILE*) with line offset (0 default) |
:render(writer?) |
Render the report (returns string or calls writer function) |
| Option | Type | Default | Description |
|---|---|---|---|
compact |
boolean | false |
Compact mode (works with underlines) |
cross_gap |
boolean | true |
Draw arrows across skipped lines |
underlines |
boolean | true |
Draw underlines for single-line labels |
column_order |
boolean | false |
Simple column order (true) vs natural ordering (false) |
align_messages |
boolean | true |
Align label messages to same column |
multiline_arrows |
boolean | true |
Use arrows for multi-line spans |
tab_width |
integer | 4 |
Number of spaces per tab |
limit_width |
integer | 0 |
Max line width (0 = unlimited) |
ambi_width |
integer | 1 |
Width of ambiguous Unicode characters |
label_attach |
string | "middle" |
Label attachment point ("start", "middle", "end") |
index_type |
string | "char" |
Position indexing ("char" or "byte") |
char_set |
string | "unicode" |
Glyph set ("unicode" or "ascii") |
color |
boolean | true |
Enable ANSI color codes |
Multi-source diagnostics: Use mu.cache() to manage multiple source files:
local cache = mu.cache()
:source("local x = 1 + '2'", "main.lua")
:file("lib.lua") -- Loads from file system
local report = mu.report(15, 0) -- Position in source 0 (main.lua)
:label(15, 18):message("error here")
cache:render(report)
Length operator: #cache returns the number of sources.
For detailed Lua API documentation with examples, see musubi.def.lua.
local cg = mu.colorgen(min_brightness?) -- min_brightness ∈ [0, 1], default 0.5
local color_func = cg:next() -- Get next color in cycle
Report:render()
├─ Context Creation (group labels by source, calculate widths)
├─ Header Rendering (error level, code, message)
├─ For each source group:
│ ├─ Reference Header (file:line:col)
│ ├─ Line Rendering:
│ │ ├─ Label Clustering (group overlapping labels)
│ │ ├─ Window Calculation (when limit_width > 0)
│ │ ├─ Virtual Row Splitting (multi-line labels)
│ │ └─ For each cluster:
│ │ ├─ Line Content (with label highlighting)
│ │ └─ Arrow Drawing (underlines, connectors, messages)
│ └─ Empty Line
└─ Footer Rendering (notes, help messages)
Intervals:
start/end use half-open intervals [start, end)first/last use close intervals [fist, last]Width Caching:
muC_widthindex) for O(log n) position lookupsLabel Clustering:
Memory Management (C):
malloc/free)# Compile with coverage
gcc -ggdb -shared --coverage -o musubi.so musubi.c
# Run tests (uses C bindings by default)
lua test.lua
# Generate coverage report
lcov -d . -c -o lcov.info
genhtml lcov.info -o coverage/
Both implementations maintain 100% test coverage:
Test Categories:
Improvements:
Limitations:
\n newlines (not Unicode line separators)See .github/c_port.md for detailed implementation notes:
See .github/project-structure.md for:
Contributions are welcome! Please:
lua test.lua# Run tests with coverage
lua -lluacov test.lua
luacov ariadne.lua
# Find uncovered lines
grep '^\*\+0 ' luacov.report.out
# Run specific test
lua test.lua TestBasic.test_simple_label
MIT License - See LICENSE for details.
Made with ❤️ for better compiler diagnostics