oslquery-petite
A lightweight Rust library for parsing and querying OSL (Open Shading Language) shader metadata from compiled .oso files. Aims to match OSL's liboslquery in the feature subset it implements 1:1.
Overview
oslquery-petite provides a pure Rust implementation compatible with OpenShadingLanguage's liboslquery. This is designed for integration with 3D rendering pipelines. It can parse OSO files generated by OSL compilers and extract shader parameters, types, default values, and metadata.
Features
- Parse OSO files from OSL 1.00+ format.
- Extract shader parameters with complete type information.
- Support for all OSL base types (
int,float,string,color,point,vector,normal,matrix). - Handle arrays (fixed and variable-length).
- Parse default values for all parameter types.
- Extract metadata hints and attributes.
- Support for closure types.
- Compatible with 3Delight, Cycles, and other OSL implementations.
- Optional
oslq(likeoslinfo) CLI tool for querying shaders.
Installation
Add to your Cargo.toml:
[]
= "0.1"
With optional features:
[]
= { = "0.1", = ["cli", "json", "hash"] }
Features
cli- Enables theoslqcommand-line tool.json- Enables JSON serialization support.hash- DerivesHashfor all public types.
Quick Start
use ;
// Parse an OSO file.
let query = open?;
// Get shader information.
println!;
println!;
// Iterate through parameters with type-safe access.
for param in query.params
Type-Safe API
The API provides complete type safety where types and values are unified:
use ;
let query = open?;
for param in query.params
With this API, it's impossible to have type mismatches - a Color parameter always has exactly 3 floats, a Matrix always has 16, and you can't accidentally mix types.
API Overview
OslQuery
The main entry point for querying shader information:
// Parse from file.
let query = open?;
// Parse from string.
let content = read_to_string?;
let query = from_string?;
// Query shader info.
let name = query.shader_name; // e.g., "lambert"
let shader_type = query.shader_type; // e.g., "surface"
let param_count = query.param_count;
// Access parameters.
let param = query.param_by_name; // By name
let param = query.param_at; // By index
let all_params = query.params; // All parameters
Parameter
Represents a shader parameter with type-safe access:
TypedParameter
Type-safe parameter representation where type and (default) value are unified:
oslq – A Petite oslinfo Clone
# Query a shader.
# Query multiple shaders/
# Query specific parameter.
# Use search path.
# Verbose output.
# JSON output (requires json feature).
# Benchmark parsing.
Examples
Parsing with Shader Search Path
use OslQuery;
// Search for shader.oso in multiple directories.
let searchpath = "/usr/local/shaders:/project/shaders";
let query = open_with_searchpath?;
Checking for Specific Metadata
use MetadataValue;
for param in query.params
Arrays Handling
for param in query.params
Differences from C++ liboslquery
While maintaining API compatibility where possible, this Rust implementation:
- Uses idiomatic Rust patterns (
Resulttypes, iterators, etc.). - Supports incremental parsing.
- Has optional JSON serialization.
- Smaller binary size and faster compilation.
License
Licensed under Apache-2.0 or BSD-3-Clause or MIT or Zlib at your option.
Contributing
Contributions are welcome! Please ensure:
- All tests pass with
cargo test. - Code is formatted with
cargo fmt. - No clippy warnings with
cargo clippy -- -W warnings. - Documentation is updated for API changes.
Acknowledgments
Based on the OSL specification and inspired by with Open Shading Language's liboslquery/oslinfo.