Languages: English | ηΉι«δΈζ
A blazing-fast PHP extension for encoding and decoding TOON (Token-Oriented Object Notation) format, built with Rust for maximum performance and safety.
- β‘ Lightning-fast Performance β Rust-powered for unparalleled speed
- π Bidirectional Support β
toon_encode()andtoon_decode() - π― Smart Type Detection β Auto-detects arrays vs. associative maps
- π Order Preservation β Maintains insertion order
- π Type-Safe β Memory-safe with zero unsafe code
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install PHP development headers
sudo apt install php8.2-dev clang # Ubuntu/Debian
# or
brew install php clang # macOS# Clone repository
git clone https://github.com/mesak/php-rs-toon.git
cd php-rs-toon
# Build release version
cargo build --release
# Install extension
sudo cp target/release/libphp_rs_toon.so $(php-config --extension-dir)/
# Enable extension
echo "extension=libphp_rs_toon.so" | sudo tee -a $(php-config --ini-path)/20-toon.ini
# Verify installation
php -m | grep php_rs_toon# Debug build (faster compilation)
cargo build
# Release build (optimized)
cargo build --release
# Format code
cargo fmt
# Check code quality
cargo clippy --release# Build test environment
docker build -f Dockerfile.test -t php-rs-toon:test .
# Build production
docker build -f Dockerfile.prod -t php-rs-toon:prod .cargo test# Using installed extension
php test.php
# Using built extension (without installation)
php -d extension=target/release/libphp_rs_toon.so test.php
# Docker testing
docker build -f Dockerfile.test -t php-rs-toon:test .
docker run --rm php-rs-toon:test
# Test against multiple PHP versions (8.1 β 8.4)
for ver in 8.1 8.2 8.3 8.4; do
docker build \
-f Dockerfile.test \
-t php-rs-toon:test-$ver \
--build-arg PHP_VERSION=$ver .
docker run --rm php-rs-toon:test-$ver php test.php || exit 1
done# Single performance test
php -d extension=target/release/libphp_rs_toon.so perf-test.php
# Comparison with pure PHP implementation
php -d extension=target/release/libphp_rs_toon.so perf-compare.phpcd benchmark
composer install
./run-benchmarks.sh
# Docker benchmarking
docker build -f Dockerfile.benchmark -t php-rs-toon:bench .
docker run --rm php-rs-toon:benchPerformance Results:
- 10-30x faster than pure PHP implementation
- Optimized memory usage with pre-allocation
- Recursion depth protection (max depth: 60)
<?php
$data = [
"user" => [
"id" => 123,
"email" => "ada@example.com",
"metadata" => [
"active" => true,
"score" => 9.5
]
]
];
$toon = toon_encode($data);
echo $toon;Output:
user:
id: 123
email: ada@example.com
metadata:
active: true
score: 9.5
<?php
$toon = <<<'TOON'
user:
id: 123
name: Alice
tags: 1, 2, 3
TOON;
$data = toon_decode($toon);
print_r($data);Output:
Array
(
[user] => Array
(
[id] => 123
[name] => Alice
[tags] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
<?php
$data = [
"company" => [
"name" => "TechCorp",
"departments" => [
["name" => "Engineering", "employees" => 50],
["name" => "Sales", "employees" => 30],
],
"metadata" => [
"founded" => 2020,
"public" => false
]
]
];
$toon = toon_encode($data);
$decoded = toon_decode($toon);
assert($data === $decoded); // Round-trip consistency<?php
try {
$result = toon_decode("invalid: : syntax");
} catch (Exception $e) {
echo "Parse error: " . $e->getMessage();
}See examples/ directory:
basic-encode.php- Simple encodingnested-structures.php- Complex nested datallm-optimization.php- LLM-friendly formatting
Encodes PHP data into TOON format string.
Parameters:
$data- PHP value (array, string, int, float, bool, null)
Returns: TOON formatted string
Throws: Exception on recursion depth limit (>100)
Decodes TOON string into PHP data.
Parameters:
$toon- TOON formatted string
Returns: PHP value (array, string, int, float, bool, null)
Throws: Exception on parse error
php-rs-toon/
βββ src/
β βββ lib.rs # PHP FFI bridge
β βββ toon.rs # TOON parser & encoder
βββ examples/ # Usage examples
βββ benchmark/ # Performance benchmarks
βββ test.php # Integration tests
βββ perf-test.php # Quick performance test
βββ perf-compare.php # Rust vs PHP comparison
βββ Cargo.toml # Rust dependencies
βββ Dockerfile.* # Docker configurations
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Make changes with tests
- Run
cargo fmt && cargo clippy && cargo test - Submit pull request
MIT License - see LICENSE for details
Languages: English | ηΉι«δΈζ