14 unstable releases (4 breaking)

new 0.5.0 Mar 19, 2023
0.4.0 Nov 11, 2022
0.3.0 May 18, 2022
0.2.1 Mar 16, 2022
0.1.7 Mar 5, 2021

#225 in Parser implementations

Download history 2/week @ 2022-12-01 20/week @ 2022-12-08 2/week @ 2022-12-15 2/week @ 2022-12-22 2/week @ 2022-12-29 2/week @ 2023-01-05 2/week @ 2023-01-12 3/week @ 2023-01-19 5/week @ 2023-01-26 6/week @ 2023-02-02 25/week @ 2023-02-09 42/week @ 2023-02-16 10/week @ 2023-03-02 36/week @ 2023-03-09 24/week @ 2023-03-16

71 downloads per month

MIT license

2.5K SLoC


Crates.io Documentation MIT licensed Build Status codecov

Rust bindings for the libyang2 library.

For raw FFI bindings for libyang2, see libyang2-sys.


yang2 = "0.5"

Design Goals

  • Provide high-level bindings for libyang2 using idiomatic Rust
  • Leverage Rust's ownership system to detect API misuse problems at compile time
  • Automatic resource management
  • Zero-cost abstractions

Feature flags

By default, yang2-rs uses pre-generated FFI bindings and uses dynamic linking to load libyang2. The following feature flags, however, can be used to change that behavior:

  • bundled: instructs cargo to download and build libyang2 from the sources. The resulting objects are grouped into a static archive linked to this crate. This feature can be used when having a libyang2 dynamic link dependency isn't desirable.
    • Additional build requirements: cc 1.0, cmake 0.1, a C compiler and CMake.
  • use_bindgen: generate new C FFI bindings dynamically instead of using the pre-generated ones. Useful when updating this crate to use newer libyang2 versions.
    • Additional build requirements: bindgen 0.55.0


A basic example that parses and validates JSON instance data, and then converts it to the XML format:

use std::fs::File;
use yang2::context::{Context, ContextFlags};
use yang2::data::{
    Data, DataFormat, DataParserFlags, DataPrinterFlags, DataTree,

static SEARCH_DIR: &str = "./assets/yang/";

fn main() -> std::io::Result<()> {
    // Initialize context.
    let ctx = Context::new(ContextFlags::NO_YANGLIBRARY)
        .expect("Failed to create context");
        .expect("Failed to set YANG search directory");

    // Load YANG modules.
    for module_name in &["ietf-interfaces", "iana-if-type"] {
        ctx.load_module(module_name, None, &[])
            .expect("Failed to load module");

    // Parse and validate data tree in the JSON format.
    let dtree = DataTree::parse_file(
    .expect("Failed to parse data tree");

    // Print data tree in the XML format.
            DataPrinterFlags::WD_ALL | DataPrinterFlags::WITH_SIBLINGS,
        .expect("Failed to print data tree");


More examples can be found here.


This project is licensed under the MIT license.


Bug reports and pull requests are welcome on GitHub at https://github.com/rwestphal/yang2-rs.


~163K SLoC