r-resources
A Rust library inspired by Android/Kotlin's R system for managing resources at build time.
Stop scattering magic numbers across 12 files! Centralize all your constants, strings, colors, and configuration in one place. Modify them quickly without hunting through your codebase.
Features
- Build Time: Resources are compiled directly into your binary
- Type-safe: Strongly typed constants
- Zero-cost: No runtime overhead
- Thread-safe: All resources are
const- safe to use in multi-threaded contexts - Async-safe: Works perfectly with tokio, async-std, and other async runtimes
- Simple: Clear and elegant syntax
- Centralized: All constants in one place - modify quickly without searching 12 files
- Framework-agnostic: Works great with Leptos, egui, or any Rust UI framework
Why r-resources?
The Problem
// Magic numbers scattered everywhere 😞
const MAX_RETRIES: i64 = 3; // main.rs
const TIMEOUT: i64 = 5000; // api.rs
const RATE: f64 = 0.75; // billing.rs
// ... 12 more files to update when changing a value
The Solution
<!-- res/values.xml - One place to rule them all! -->
3
5000
0.75
// Access anywhere, type-safe, zero-cost
use r;
let retries = MAX_RETRIES;
let timeout = TIMEOUT_MS;
Supported Types
string: String valuesint: Integer values (i64)float: Floating-point values (f64)bool: Boolean valuescolor: Color hex stringsurl: URL stringsdimension: Dimension values with units (e.g., "16dp", "24px")string-array: String arraysint-array: Integer arraysfloat-array: Float arrays
Installation
Add this to your Cargo.toml:
[]
= "0.7.6"
Note: r-resources is a build dependency, not a runtime dependency. It generates code at compile time. All XML files in the res/ directory are automatically loaded and merged.
Quick Start
1. Create your resources
Create res/values.xml at the root of your project:
My Awesome App
3
0.20
true
2. Use your resources
use r;
Advanced Features
Namespaces (v0.5.0+)
Organize resources hierarchically:
Login
Invalid credentials
#3366FF
Access via type-organized modules:
use string;
TITLE
INVALID_CREDENTIALS
Access via Kotlin-style r:: module:
use r;
TITLE
INVALID_CREDENTIALS
PRIMARY
String Interpolation (v0.6.0+)
Resolve references at build-time:
https://api.example.com
v2
Welcome to @string/app_name!
@string/base_url/@string/api_version
Generated:
WELCOME_TITLE // "Welcome to My Awesome App!"
API_URL_WITH_VERSION // "https://api.example.com/v2"
All references are resolved at compile-time - no runtime concatenation!
Template Functions (v0.6.0+)
Generate reusable functions with typed parameters:
Generated:
greeting // "Hello Alice, you have 5 messages!"
Supports string, int, float, and bool parameter types.
Multiple Resource Files
Support for multiple XML files in the res/ directory:
res/
├── values.xml # Main resources
├── config.xml # Configuration
└── theme.xml # Theme-specific resources
All XML files in res/ are automatically loaded and merged at build time.
Simulating Locales
Use namespaces to organize by language - no need for locale-specific files:
Bienvenue!
Welcome!
// Switch based on user locale
let welcome = if locale == "fr" else ;
Access Patterns
Kotlin-style Flat Access (Recommended)
use r;
// Root level resources
APP_NAME
MAX_RETRIES
// Namespaced resources
TITLE
INVALID_CREDENTIALS
PRIMARY
Type-Organized Access
use *;
// Explicit type organization
APP_NAME
MAX_RETRIES
TITLE
PRIMARY
Both patterns are equally performant - choose what fits your style!
Thread Safety
All resources are const values, making them completely thread-safe:
use thread;
use r;
// Safe to access from multiple threads
let handles: =
.map
.collect;
Performance
- Compilation: Resources parsed once at build time
- Runtime: Zero overhead - direct constant access
- Memory: Resources live in binary's data segment
- Concurrency: No locks, no synchronization needed
Examples
Run the examples to see r-resources in action:
# Basic usage
# New resource types
# Resource references
# Namespaces
# String interpolation and templates
Philosophy
Centralize. Type-safe. Zero-cost.
- Centralize: All constants in
res/- modify quickly without searching your codebase - Type-safe: Compile-time errors catch typos and mismatches
- Zero-cost: Direct constant access - no runtime overhead
- Simple: Familiar XML syntax, elegant Rust API
Perfect for projects where you need to:
- Avoid magic numbers scattered across 12 different files
- Quickly and simply modify constants without hunting through your codebase
- Share constants across multiple modules
- Build type-safe UI applications with Leptos, egui, or any Rust framework
- Centralize all configuration in one place for easy maintenance
Development
Building from source
Code quality
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.