31 releases (17 breaking)

new 1.0.0-rc.9 Nov 6, 2024
1.0.0-rc.7 Aug 23, 2024
1.0.0-rc.6 Jun 6, 2024
1.0.0-rc.3 Feb 26, 2024
0.1.2 Oct 19, 2020

#104 in Encoding

Download history 1507/week @ 2024-07-17 2591/week @ 2024-07-24 2651/week @ 2024-07-31 3543/week @ 2024-08-07 4172/week @ 2024-08-14 3597/week @ 2024-08-21 3217/week @ 2024-08-28 3648/week @ 2024-09-04 2794/week @ 2024-09-11 2199/week @ 2024-09-18 2732/week @ 2024-09-25 4616/week @ 2024-10-02 4811/week @ 2024-10-09 4369/week @ 2024-10-16 4762/week @ 2024-10-23 7226/week @ 2024-10-30

22,256 downloads per month
Used in 16 crates (14 directly)

Apache-2.0

2MB
45K SLoC

Amazon Ion Rust

Crate Docs License CI Build codecov

A Rust implementation of the Amazon Ion data format.

Example

For more information, please see the documentation.

use crate::{ion_seq, Element, IonResult, Timestamp};

fn main() -> IonResult<()> {
    // Read a single value from a string/slice/Vec
    let element = Element::read_one("\"Hello, world!\"")?;
    let text = element.expect_string()?;
    assert_eq!(text, "Hello, world!");

    // Read a series of values from a string/slice/Vec
    let elements = Element::read_all("1 2 3")?;
    let mut sum = 0;
    for element in elements {
        sum += element.expect_i64()?;
    }
    assert_eq!(sum, 6);

    // Iterate over values in a file
    let ion_file = std::fs::File::open("/foo/bar/baz.ion").unwrap();
    for element in Element::iter(ion_file)? {
        println!("{}", element?)
    }

    // Construct a sequence of Ion elements from Rust values
    let values = ion_seq!(
        "foo",
        Timestamp::with_ymd(2016, 5, 11).build()?,
        3.1416f64,
        true
    );

    // Write values to a String using generously-spaced text
    let text_ion: String = values.encode_as(v1_0::Text.with_format(TextFormat::Pretty))?;
    assert_eq!(values, Element::read_all(text_ion)?);

    // Write values to a buffer in compact binary
    let binary_buffer: Vec<u8> = values.encode_as(v1_0::Binary)?;
    assert_eq!(values, Element::read_all(binary_buffer)?);

    Ok(())
}

Experimental features

The ion_rs library has a number of features that users can opt into. While the following features are complete and well-tested, their APIs are not stable and are subject to change without notice between minor versions of the library.

  1. experimental-reader-writer, a streaming reader and writer API.
  2. experimental-tooling-apis, APIs for accessing the encoding-level details of the stream.
  3. experimental-serde, a serde serializer and deserializer.
  4. experimental-ion-hash, an implementation of Ion Hash.

Development

This project uses a submodule to pull in Ion Tests and Ion Hash Tests. The easiest way to pull everything in is to clone the repository recursively:

$ git clone --recursive https://github.com/amazon-ion/ion-rust

You can also initialize the submodules as follows:

$ git submodule update --init --recursive

Building the project:

$ cargo build --all-features

Running all tests for ion-rust:

$ cargo test --all-features

Our continuous integration builds/tests, checks formatting, builds all API docs including private, and clippy linting, you will likely want to check most of these to avoid having to wait until the CI finds it:

$ ./clean-rebuild.sh

Dependencies

~3–9MB
~84K SLoC