๐ฎ Mystical-Runic - Ancient Symbols for Modern Web Magic
"In the beginning was the Word, and the Word was {{mustache}}..."
Welcome, brave developer, to the mystical realm of Mystical-Runic - where ancient Nordic symbols meet modern HTML templating in a beautiful dance of curly braces and digital sorcery!
โจ Features
๐ Security First: XSS-safe by default with comprehensive HTML escaping
โก High Performance: Template caching, bytecode compilation, parallel processing
๐ฏ Simple API: Clean, intuitive interface for Rust developers
๐งช Well Tested: 166+ tests with extensive security and performance tests
๐๏ธ Template Inheritance: Advanced layout system with nested inheritance and {{super}}
๐ง Powerful Filters: Built-in filters like upper, lower, currency, truncate with chaining support
๐ฆ Reusable Macros: Define and reuse template components with parameters
๐ Deep Object Navigation: Unlimited depth dot notation (e.g., {{user.profile.stats.level}})
๐ Internationalization (i18n): Multi-language support with {{t "key"}} syntax and locale switching
๐ข Advanced Math Filters: Mathematical operations with add, multiply, divide, percentage, round
๐จ Custom Filter API: Register your own filters for domain-specific transformations
๐ญ Dual Naming System: Choose between professional (TemplateEngine) or mystical (RuneEngine) naming styles
๐ Smart Pluralization: Automatic plural forms with {{plural count "item" "items"}}
๐ Zero Dependencies: Pure Rust implementation with no external dependencies
๐ฆ Modern Rust: Rust 2021 edition with 1.74.0+ MSRV, future Rust 2024 ready
โก The Sacred Incantations
Core Magic
- Whisper Variables:
{{name}}- Speak a name and it shall manifest (safely escaped from evil XSS spirits) - Summon Raw Power:
{{& html}}- Unleash unescaped HTML with great responsibility and greater danger - Divine Conditionals:
{{if chosen_one}}...{{/if}}- The HTML appears only for the worthy - Mystical Loops:
{{for spell in grimoire}}...{{/for}}- Repeat incantations until magic happens - Ancient Includes:
{{include "scrolls/wisdom.html"}}- Import wisdom from other sacred texts - Silent Whispers:
{{! This is but a comment, invisible to mortals }}- Notes for future wizards - Deep Path Traversal:
{{user.profile.stats.level}}- Navigate through nested object realms with unlimited depth
Advanced Sorcery (v0.2.0)
- Sacred Inheritance:
{{extends "base.html"}}- Inherit the power of ancestral templates - Mystical Blocks:
{{block content}}...{{/block}}- Define regions of power in your layouts - Ancestral Wisdom:
{{super}}- Channel the content of parent templates - Transformation Filters:
{{name|upper|truncate:10}}- Transform values with ancient filters - Reusable Spells (Macros):
{{macro spell(power)}}...{{/macro}}- Create reusable incantations - Spell Invocation:
{{spell("lightning")}}- Call upon your defined macros
Global Sorcery (v0.3.0)
- Universal Translation:
{{t "welcome"}}- Speak all tongues with i18n support - Locale Switching: Switch between languages with
engine.set_locale("en") - Quantity Wisdom:
{{plural count "item" "items"}}- Smart pluralization magic - Mathematical Alchemy:
{{price|add:10|multiply:2|percentage}}- Advanced math transformations - Custom Enchantments:
engine.register_filter("reverse", |input, _| Ok(input.chars().rev().collect()))- Forge your own filters
๐ Quick Start
Installation
[]
= "0.3.3"
Basic Usage - Choose Your Style! ๐ญ
๐ข Professional Style (Conventional)
Perfect for corporate environments, team projects, and those who prefer explicit naming:
use ;
let mut engine = new;
let mut context = new;
// Set variables in your template context
context.set;
context.set;
context.set;
let result = engine.render_string.unwrap;
๐ฎ Mystical Style (Themed)
For the adventurous, creative projects, and those who enjoy a touch of magic:
use ;
// Summon the ancient engine from the template realm
let mut engine = new;
let mut scroll = new;
// Inscribe your desires upon the scroll
scroll.set;
scroll.set;
scroll.set;
// Speak the incantation and witness the transformation
let result = engine.render_string.unwrap;
๐ก Pro Tip: Both styles are completely interchangeable! Use whichever feels right for your project. You can even mix them in the same codebase - they're just aliases for the same underlying types.
Advanced Example: Character Sheet Generator
use ;
use HashMap;
let mut engine = new;
let mut scroll = new;
// Create a magical character
let mut character = new;
character.insert;
character.insert;
character.insert;
character.insert;
character.insert;
scroll.set;
// โ๏ธ Create spell list
let spells = vec!;
scroll.set;
let character_sheet = r#"
๐งโโ๏ธ Name: {{character.name}}
๐ Class: {{character.class}} (Level {{character.level}})
๐ฎ Mana: {{character.mana}}
{{if character.has_staff}}
๐ช Equipment: Magical Staff of Power
{{/if}}
โก KNOWN SPELLS:
{{for spell in spells}}
{{spell.icon}} {{spell.name}} - Power: {{spell.damage}}
{{/for}}
"#;
let result = engine.render_string.unwrap;
println!;
Deep Dot Notation Example
use ;
use HashMap;
let mut engine = new;
let mut scroll = new;
// Create deeply nested game data structure
let mut stats = new;
stats.insert;
stats.insert;
stats.insert;
let mut equipment = new;
equipment.insert;
equipment.insert;
let mut character = new;
character.insert;
character.insert;
character.insert;
character.insert;
let mut game_data = new;
game_data.insert;
scroll.set;
// Use deep dot notation to access nested values
let template = r#"
๐ฎ GAME CHARACTER SHEET ๐ฎ
๐ค Name: {{game.character.name}}
๐ญ Class: {{game.character.class}}
๐ STATS:
โค๏ธ Health: {{game.character.stats.health}}
๐ Mana: {{game.character.stats.mana}}
โญ Level: {{game.character.stats.level}}
โ๏ธ EQUIPMENT:
{{if game.character.equipment.weapon}}
๐ก๏ธ Weapon: {{game.character.equipment.weapon}}
{{/if}}
{{if game.character.equipment.armor}}
๐ก๏ธ Armor: {{game.character.equipment.armor}}
{{/if}}
{{if game.character.stats.level}}
๐ Status: {{if game.character.stats.health}}Combat Ready{{/if}}
{{/if}}
"#;
let result = engine.render_string.unwrap;
println!;
๐ฐ Template Inheritance (v0.2.0)
Create sophisticated layouts with template inheritance:
<!-- base.html -->
{{block title}}My Site{{/block}}
{{block header}}Default Header{{/block}}
{{block content}}{{/block}}
{{block footer}}ยฉ 2024 My Site{{/block}}
<!-- admin.html -->
{{extends "base.html"}}
{{block title}}Admin Panel - {{block page_title}}{{/block}}{{/block}}
{{block content}}
{{block sidebar}}Default Sidebar{{/block}}
{{block admin_content}}{{/block}}
{{/block}}
<!-- admin_users.html -->
{{extends "admin.html"}}
{{block page_title}}User Management{{/block}}
{{block admin_content}}
Users
{{for user in users}}
{{user.name}} - {{user.role}}
{{/for}}
{{/block}}
๐ง Powerful Filters (v0.2.0)
Transform your data with built-in filters:
{{title|upper}} <!-- HELLO WORLD -->
{{description|lower}} <!-- hello world -->
${{price|currency}} <!-- $12.99 -->
{{content|truncate:50}} <!-- Truncated text... -->
{{date|date:"Y-m-d"}} <!-- 2024-01-15 -->
<!-- Chain multiple filters -->
{{name|lower|capitalize}} <!-- John Doe -->
{{text|strip|truncate:20|upper}} <!-- TRIMMED TEXT... -->
๐ฆ Reusable Macros (v0.2.0)
Create reusable template components:
<!-- Define macros -->
{{macro button(text, type="button", class="btn")}}
{{text}}
{{/macro}}
{{macro card(title, content, class="card")}}
{{title}}
{{content}}
{{/macro}}
{{macro user_card(user)}}
{{user.name}}
{{user.email}}
{{if user.active}}
Online
{{/if}}
{{/macro}}
<!-- Use macros -->
{{button("Save", type="submit", class="btn btn-primary")}}
{{card("Welcome", "This is a welcome message", class="card highlight")}}
{{for user in users}}
{{user_card(user)}}
{{/for}}
๐ Template Syntax Guide
Variables
<!-- Safe HTML escaping (default) -->
{{user_input}}
<!-- Raw HTML output (use with caution) -->
{{& trusted_html}}
<!-- Object properties -->
{{user.name}} ({{user.email}})
<!-- Deep nested properties -->
Level: {{player.character.stats.level}}
{{config.database.connection.host}}:{{config.database.connection.port}}
Conditionals
{{if user.is_admin}}
Admin Controls
{{/if}}
{{if items}}
<!-- items exist -->
{{/if}}
<!-- Deep conditionals -->
{{if user.settings.notifications.email.enabled}}
Email notifications are on
{{/if}}
{{if config.features.advanced.enabled}}
Advanced mode active
{{/if}}
Truthiness Rules:
- Strings: non-empty = true, empty = false
- Numbers: non-zero = true, zero = false
- Booleans: as expected
- Arrays: non-empty = true, empty = false
- Objects: non-empty = true, empty = false
Loops
{{for product in products}}
{{product.name}}
Price: ${{product.price}}
{{if product.on_sale}}
ON SALE!
{{/if}}
{{/for}}
Template Includes
<!-- main.html -->
{{include "partials/head.html"}}
{{include "partials/header.html"}}
{{content}}
{{include "partials/footer.html"}}
Comments
{{! This comment will not appear in the output }}
{{!
Multi-line comments
are also supported
}}
Visible content
๐ Security Features
Mystical-Runic takes security seriously and provides multiple layers of protection:
XSS Prevention
let mut context = new;
context.set_string;
let result = engine.render_string.unwrap;
// Output: <script>alert('xss')</script>
Path Traversal Protection
// These will safely fail:
engine.render; // โ Blocked
engine.render; // โ Blocked
Template Injection Prevention
context.set_string;
let result = engine.render_string.unwrap;
// Output: {{admin_password}} (literal text, not executed)
๐จ API Reference
RuneEngine (TemplateEngine)
let mut engine = new;
// Render from file
let result = engine.render?;
// Render from string
let result = engine.render_string?;
// Load template (with caching)
let template_content = engine.load_template?;
RuneScroll (TemplateContext)
let mut scroll = new;
// Set different value types
scroll.set_string;
scroll.set_number;
scroll.set_bool;
// Set complex values
scroll.set;
scroll.set;
// Retrieve values
let value = scroll.get_string;
RuneSymbol (TemplateValue)
// Create different value types
let string_val = String;
let number_val = Number;
let bool_val = Bool;
let array_val = Array;
let object_val = Object;
๐งช Testing
Run the comprehensive test suite:
# Run all tests
# Run specific test categories
# Run with output
Internationalization Example (v0.3.0)
use ;
use HashMap;
let mut engine = new;
let mut context = new;
context.set;
// Set up English translations
let mut en_translations = new;
en_translations.insert;
engine.set_translations;
// Set up French translations
let mut fr_translations = new;
fr_translations.insert;
engine.set_translations;
// Use English
engine.set_locale;
let welcome_en = engine.render_string.unwrap;
// Output: "Welcome Alice!"
// Switch to French
engine.set_locale;
let welcome_fr = engine.render_string.unwrap;
// Output: "Bienvenue Alice!"
Custom Filters Example (v0.3.0)
use ;
let mut engine = new;
let mut context = new;
context.set;
// Register custom filters
engine.register_filter;
engine.register_filter;
let result = engine.render_string.unwrap;
// Output: "DLROW OLLEHDLROW OLLEH"
Math Filters Example (v0.3.0)
use ;
let mut engine = new;
let mut context = new;
context.set;
context.set;
// Complex calculations with chaining
let template = "Price: ${{price}}, Total: {{price|multiply:tax_rate|divide:100|add:price|round:2}}";
let result = engine.render_string.unwrap;
// Output: "Price: $100, Total: 108.00"
Pluralization Example (v0.3.0)
use ;
let mut engine = new;
let mut context = new;
let template = "You have {{count}} {{plural count \"apple\" \"apples\"}}";
context.set;
let result = engine.render_string.unwrap;
// Output: "You have 1 apple"
context.set;
let result = engine.render_string.unwrap;
// Output: "You have 5 apples"
๐ฎ Examples
Check out the examples/ directory for more magical demonstrations:
spell_casting.rs- Fantasy RPG character sheet generator- More examples coming soon!
๐ฆ Rust Compatibility
Minimum Supported Rust Version (MSRV): 1.74.0+
Edition: Rust 2021 (with future Rust 2024 readiness)
Tested on: Rust 1.74.0 through latest stable
Future-Proof Design
- Modern Rust patterns and idioms
- Prepared for Rust 2024 edition migration
- Zero unsafe code
- Comprehensive test coverage (166+ tests)
- Zero compilation warnings
Development Setup
๐งช Test-Driven Development (TDD) Methodology
Mystical-Runic follows strict Test-Driven Development practices. When contributing, please observe the sacred TDD ritual:
๐ด Red โ ๐ข Green โ ๐ต Refactor Cycle
-
๐ด RED - Write a Failing Test First
# Write your test before any implementation # โ Should fail - good! -
๐ข GREEN - Write Minimal Code to Pass
# Write just enough code to make the test pass # โ Should pass - excellent! -
๐ต REFACTOR - Improve Without Breaking
# Clean up code while keeping tests green
"Tests are the safety net that lets you refactor fearlessly." โ TDD Proverb
๐ Changelog
v0.2.0 (Latest Release) - The Advanced Sorcery Edition
- ๐ฐ Template Inheritance: Advanced layout system with nested inheritance support
- ๐ง Powerful Filters: Built-in filters (
upper,lower,currency,truncate,date) with chaining - ๐ฆ Reusable Macros: Define and invoke template components with parameters
- ๐ Enhanced Deep Navigation: Unlimited depth dot notation (
{{game.player.stats.level}}) - โก Performance Boost: Bytecode compilation, parallel processing, memory mapping
- ๐งช 127+ Tests: Comprehensive test coverage including v0.2.0 features
- ๐ง Bug Fixes: Fixed nested layout inheritance and function call error handling
- ๐ Zero Dependencies: Pure Rust implementation
v0.1.4 (Stability Release)
- ๐ง Fixed nested layout inheritance block replacement boundary calculation
- ๐ก๏ธ Enhanced loop error handling for unsupported function calls
- ๐ Maintained backward compatibility for missing variables in loops
- โ All 127 tests passing with comprehensive coverage
v0.1.1 (Security & Testing Release)
- ๐ก๏ธ Comprehensive security testing suite
- ๐งช 85+ tests with 100% coverage following TDD methodology
- ๐ Complete documentation with TDD development guidelines
- ๐ Advanced XSS and injection protection
- โก Performance optimizations and stress testing
- ๐ด๐ข๐ต Strict Test-Driven Development practices implemented
v0.1.0 (Initial Release)
- โจ Core template engine with Mustache-inspired syntax
- ๐ XSS-safe HTML escaping by default
- โก Template caching for performance
- ๐ฏ Support for variables, conditionals, loops, includes, and comments
- ๐งช Comprehensive test suite with high coverage
- ๐ Complete documentation and examples
๐ Why "Mystical-Runic"?
Because templating is basically ancient magic:
- You write mysterious symbols (
{{}}) that transform into reality - Variables appear and disappear like spirits
- One wrong bracket and your entire spell explodes
- Senior developers guard the template secrets like ancient druids
- Documentation is written in a language only the initiated understand
- And just like real magic, it works perfectly until production ๐ฅ
๐ Links
- Documentation: docs.rs/mystical-runic
- Crates.io: crates.io/crates/mystical-runic
- Repository: github.com/yrbane/mystical-runic
- Issues: github.com/yrbane/mystical-runic/issues
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
"May your templates be bug-free and your variables always defined."
โ Ancient DevOps Proverb
๐ฎโจ Happy templating! โจ๐ฎ