[![Crate](https://img.shields.io/crates/v/unstructured.svg)](https://crates.io/crates/unstructured) [![Documentation](https://img.shields.io/badge/docs-current-important.svg)](https://docs.rs/unstructured/) [![MIT License](https://img.shields.io/github/license/proctorlabs/unstructured-rs.svg)](LICENSE)

Unstructured Documents

This library provides types for usage with unstructured data. This is based on functionality from both serdejson and serdevalue. Depending on your use case, it may make sense to use one of those instead.

These structures for serialization and deserialization into an intermediate container with serde and manipulation of this data while in this intermediate state.

Purpose

So why not use one of the above libraries?

In addition to many of the futures provided by the above libraries, unstructured also provides:

Example Usage

The primary struct used in this repo is Document. Document provides methods for easy type conversion and manipulation.

```rust use unstructured::Document; use std::collections::BTreeMap;

let mut map = BTreeMap::new(); // Will be inferred as BTreeMap though root element can be any supported type map.insert("test".into(), (100 as u64).into()); // From<> is implement for most basic data types let doc: Document = map.into(); // Create a new Document where the root element is the map defined above assert_eq!(doc["test"], Document::U64(100)); ```

Document implements serialize and deserialize so that it can be easily used where the data format is unknown and manipulated after it has been received.

```rust

[macro_use]

extern crate serde; use unstructured::Document;

[derive(Deserialize, Serialize)]

struct SomeStruct { key: String, }

fn main() { let fromservice = "{\"key\": \"value\"}"; let doc: Document = serdejson::fromstr(fromservice).unwrap(); let expected: Document = "value".into(); assert_eq!(doc["key"], expected);

let some_struct: SomeStruct = doc.try_into().unwrap();
assert_eq!(some_struct.key, "value");

let another_doc = Document::new(some_struct).unwrap();
assert_eq!(another_doc["key"], expected);

} ```

Selectors can be used to retrieve a reference to nested values, regardless of the incoming format.

```rust use unstructured::Document;

let doc: Document = serdejson::fromstr("{\"some\": {\"nested\": {\"value\": \"is this value\"}}}").unwrap(); let docelement = doc.select("/some/nested/value").unwrap(); // Returns an Option, None if not found let expected: Document = "is this value".into(); asserteq!(*doc_element, expected); ```

In addition to selectors, filters can be used to create new documents from an array of input documents.

```rust use unstructured::Document;

let docs: Vec = vec![ serdejson::fromstr(r#"{"some": {"nested": {"vals": [1,2,3]}}}"#).unwrap(), serdejson::fromstr(r#"{"some": {"nested": {"vals": [4,5,6]}}}"#).unwrap(), ]; let result = Document::filter(&docs, "[0].some.nested.vals | [1].some.nested.vals").unwrap(); assert_eq!(result["some"]["nested"]["vals"][4], Document::U64(5)); ```