| Crates.io | yamlstar |
| lib.rs | yamlstar |
| version | 0.1.0 |
| created_at | 2023-04-19 14:11:23.027062+00 |
| updated_at | 2026-01-13 16:55:27.860114+00 |
| description | Pure YAML 1.2 loader - Rust binding for libyamlstar |
| homepage | |
| repository | https://github.com/yaml/yamlstar |
| max_upload_size | |
| id | 843583 |
| size | 33,661 |
Pure YAML 1.2 loader for Rust using the libyamlstar shared library.
YAMLStar is a Rust binding for the YAMLStar library - a pure YAML 1.2 implementation written in Clojure. This binding provides a fast, spec-compliant YAML parser with zero Rust dependencies beyond the FFI layer.
use yamlstar::YAMLStar;
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
host: String,
port: u16,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let ys = YAMLStar::new()?;
// Load YAML to dynamic type
let data: serde_json::Value = ys.load("key: value")?;
// Load YAML to typed struct
let config: Config = ys.load("host: localhost\nport: 8080")?;
// Load multiple documents
let docs: Vec<String> = ys.load_all("---\ndoc1\n---\ndoc2")?;
Ok(())
}
Add to your Cargo.toml:
[dependencies]
yamlstar = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
The binding requires libyamlstar.so to be available at runtime. You have several options:
Option 1: Build from source
cd libyamlstar
make native
Option 2: Install to system
# Copy to system library directory
sudo cp libyamlstar/lib/libyamlstar.so.0.1.0-SNAPSHOT /usr/local/lib/
Option 3: Use LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/path/to/libyamlstar/lib:$LD_LIBRARY_PATH
cargo run
YAMLStar::new() -> Result<Self, Error>Creates a new YAMLStar instance. This initializes the GraalVM isolate for thread-safe YAML parsing.
YAMLStar::load<T>(&self, yaml: &str) -> Result<T, Error>Loads a single YAML document and deserializes it into type T. The type must implement serde::de::DeserializeOwned.
Example:
let config: Config = ys.load("host: localhost\nport: 8080")?;
YAMLStar::load_all<T>(&self, yaml: &str) -> Result<Vec<T>, Error>Loads all YAML documents from a multi-document string and returns them as a Vec<T>.
Example:
let docs: Vec<String> = ys.load_all("---\ndoc1\n---\ndoc2")?;
YAMLStar::version(&self) -> Result<String, Error>Returns the version string of the underlying YAMLStar library.
YAMLStar instance has its own GraalVM isolate.inf, -.inf, and .nan, these values cannot be serialized to JSON by the underlying library and will result in an errorRun the included example:
cd rust
make example
Or with cargo:
LD_LIBRARY_PATH=../libyamlstar/lib cargo run --example load_yaml
make test
Or with cargo:
LD_LIBRARY_PATH=../libyamlstar/lib cargo test
YAMLStar implements the YAML 1.2 Core Schema with the following type mappings:
| YAML Type | Rust Type |
|---|---|
!!null |
None, () |
!!bool |
bool |
!!int |
i64, u64, etc. |
!!float |
f64, f32 |
!!str |
String, &str |
!!map |
HashMap, structs |
!!seq |
Vec, arrays |
libyamlstar shared library (built with GraalVM)MIT License - Copyright 2024 yaml.org
See License file for details.