Examples
This section contains practical Rix examples.
The examples use the public Rix facade:
#include <rix.hpp>and access packages through:
rix.csv
rix.debug
rix.auth
rix.pdfRix examples are written to show how the packages are used from application code, not only how the internal package APIs are structured.
What the examples cover
The examples are organized by package.
| Package | What it shows |
|---|---|
rix/auth | Registration, login, password hashing, sessions, tokens |
rix/csv | Parsing CSV text, writing CSV output, options |
rix/debug | Printing, formatting, inspection, simple debugging helpers |
rix/pdf | Creating PDF documents, text, tables, drawing, metadata, saving |
Running examples
Most examples can be run as simple files:
vix run examples/auth/memory-register-login.cppIf Rix is not available globally for single-file usage:
vix install -g rix/rix
vix run examples/auth/memory-register-login.cppFor a Vix project, prefer project dependencies:
vix add rix/rix
vix installand keep Rix in vix.app:
deps = [
"rix/rix",
]Example style
The examples prefer the unified facade:
#include <rix.hpp>
int main()
{
rix.debug.print("Hello from Rix");
return 0;
}This style is used because it matches the public Rix API:
rix.<package>.<feature>For example:
rix.csv.parse(...)
rix.debug.print(...)
rix.auth.memory()
rix.pdf.document()Auth examples
The auth examples show the high-level authentication API through:
rix.authAvailable examples:
CSV examples
The CSV examples show how to parse and write CSV data through:
rix.csvTypical usage:
const auto table = rix.csv.parse("name,language\nAda,C++\n");Continue with:
Debug examples
The debug examples show simple output helpers through:
rix.debugCommon helpers:
rix.debug.print(...)
rix.debug.eprint(...)
rix.debug.format(...)
rix.debug.inspect(...)For logging in real Vix applications, prefer the Vix logging system.
Continue with:
PDF examples
The PDF examples show how to generate documents through:
rix.pdfCommon helpers:
rix.pdf.document()
rix.pdf.save(...)
rix.pdf.write(...)
rix.pdf.make_text(...)Available examples:
Use examples in a project
Create a project:
vix new rix-examples --app
cd rix-examplesAdd Rix:
vix add rix/rix
vix installMake sure vix.app contains:
deps = [
"rix/rix",
]Then use the facade in src/main.cpp:
#include <rix.hpp>
int main()
{
rix.debug.print("Hello from Rix");
const auto table = rix.csv.parse(
"name,language\n"
"Ada,C++\n");
rix.debug.print("rows:", table.size());
return 0;
}Build and run:
vix build
vix runSingle-file usage
For small examples and experiments, a single file is enough.
Create a file:
mkdir -p ~/rix-examples
cd ~/rix-examples
touch main.cppAdd:
#include <rix.hpp>
int main()
{
rix.debug.print("Hello from Rix");
return 0;
}Run:
vix run main.cppIf needed, install Rix globally first:
vix install -g rix/rix
vix run main.cppFacade-only examples
The examples normally use:
#include <rix.hpp>This mounts all available Rix packages by default.
If you want a lighter facade, define only the packages you need before including rix.hpp.
Example:
#define RIX_ENABLE_CSV
#define RIX_ENABLE_DEBUG
#include <rix.hpp>
int main()
{
const auto table = rix.csv.parse("name\nAda\n");
rix.debug.print("rows:", table.size());
return 0;
}When at least one RIX_ENABLE_* macro is defined, only selected modules are mounted.
Independent package examples
You can also use a package independently.
For example, with PDF only:
vix add rix/pdf
vix installThen:
#include <rix/pdf.hpp>
int main()
{
auto pdf = rixlib::pdf::module();
auto doc = pdf.document();
auto &page = doc.add_page();
page.text(
page.x_left(),
page.y_top(),
"Hello from rix/pdf");
return pdf.save(doc, "hello.pdf").ok() ? 0 : 1;
}For most examples in this documentation, prefer the unified facade:
#include <rix.hpp>Common mistakes
Forgetting to install Rix
If the compiler cannot find rix.hpp, install the package first.
For a project:
vix add rix/rix
vix installFor single-file usage:
vix install -g rix/rixPutting Rix packages in packages
Wrong:
packages = [
"rix/rix",
]Correct:
deps = [
"rix/rix",
]deps is for Vix Registry packages.
packages is for CMake package discovery.
Using internal package APIs first
The examples prefer:
rix.auth.memory()
rix.pdf.document()
rix.csv.parse(...)
rix.debug.print(...)Use direct package headers only when you intentionally want independent package usage.
Ignoring explicit errors
Packages such as rix/auth and rix/pdf return explicit results or statuses.
Check failures before using values:
auto saved = rix.pdf.save(doc, "output.pdf");
if (saved.failed())
{
rix.debug.eprint(
"pdf error:",
rix.pdf.error.to_string(saved.error()),
saved.error().message());
return 1;
}What you should remember
Use the public facade:
#include <rix.hpp>Use packages through:
rix.csv
rix.debug
rix.auth
rix.pdfFor project usage:
vix add rix/rix
vix installand:
deps = [
"rix/rix",
]For single-file usage:
vix install -g rix/rix
vix run main.cppNext step
Start with the auth examples.