Skip to content

Commit

Permalink
Adding no-std to readme, added HeaplessDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Jan 23, 2023
1 parent c75207f commit 968b786
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
14 changes: 12 additions & 2 deletions .rustme/config.ron
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ Configuration(
for_docs: "crate::JsonNumber",
),
"document": (
default: "https://khonsulabs.github.io/justjson/main/justjson/doc/struct.Document.html",
release: "https://docs.rs/justjson/*/justjson/doc/struct.Document.html",
default: "https://khonsulabs.github.io/justjson/main/justjson/doc/type.Document.html",
release: "https://docs.rs/justjson/*/justjson/doc/type.Document.html",
for_docs: "crate::doc::Document",
),
"heapless-document": (
default: "https://khonsulabs.github.io/justjson/main/justjson/doc/type.HeaplessDocument.html",
release: "https://docs.rs/justjson/*/justjson/doc/type.HeaplessDocument.html",
for_docs: "crate::doc::HeaplessDocument",
),
"generic-document": (
default: "https://khonsulabs.github.io/justjson/main/justjson/doc/struct.GenericDocument.html",
release: "https://docs.rs/justjson/*/justjson/doc/struct.GenericDocument.html",
for_docs: "crate::doc::GenericDocument",
)
}
],
)
24 changes: 24 additions & 0 deletions .rustme/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,36 @@ This crate uses unsafe code only when converting from raw incoming data to UTF-8
data. The parser fully verifies that the data is valid UTF-8 before these
functions are used.

## `no_std` support

By default, this crate enables the `std` feature, which adds support for Rust's
standard library types. By disabling default features, this crate can be used in
`no_std` projects. For example, in your Cargo.toml:

```toml
[dependencies]
justjson = { version = "*", default-features = false }
```

The [`Value`][value] type requires the `alloc` feature to be enabled.

The [`Document`][document] type alias requires the `alloc` feature, but the
[`GenericDocument`][generic-doc] type allows providing your own collection type.

With the `heapless` feature enabled, the [`HeaplessDocument`][heapless-doc] type
can be used to parse documents using the [heapless][heapless] crate's `Vec` type
for storage. This enables parsing JSON in environments where the `alloc` crate
isn't supported.

[value]: $value$
[string]: $string$
[number]: $number$
[document]: $document$
[generic-doc]: $generic-document$
[heapless-doc]: $heapless-document$
[json-ld]: https://www.w3.org/TR/json-ld11/#compacted-document-form
[fediverse]: https://en.wikipedia.org/wiki/Fediverse
[activitypub]: https://www.w3.org/TR/activitypub/
[simd-json]: https://github.com/simd-lite/simd-json
[serde-json]: https://github.com/serde-rs/json
[heapless]: https://github.com/japaric/heapless
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,39 @@ This crate uses unsafe code only when converting from raw incoming data to UTF-8
data. The parser fully verifies that the data is valid UTF-8 before these
functions are used.

## `no_std` support

By default, this crate enables the `std` feature, which adds support for Rust's
standard library types. By disabling default features, this crate can be used in
`no_std` projects. For example, in your Cargo.toml:

```toml
[dependencies]
justjson = { version = "*", default-features = false }
```

The [`Value`][value] type requires the `alloc` feature to be enabled.

The [`Document`][document] type alias requires the `alloc` feature, but the
[`GenericDocument`][generic-doc] type allows providing your own collection type.

With the `heapless` feature enabled, the [`HeaplessDocument`][heapless-doc] type
can be used to parse documents using the [heapless][heapless] crate's `Vec` type
for storage. This enables parsing JSON in environments where the `alloc` crate
isn't supported.

[value]: https://khonsulabs.github.io/justjson/main/justjson/enum.Value.html
[string]: https://khonsulabs.github.io/justjson/main/justjson/struct.JsonString.html
[number]: https://khonsulabs.github.io/justjson/main/justjson/struct.JsonNumber.html
[document]: https://khonsulabs.github.io/justjson/main/justjson/doc/struct.Document.html
[document]: https://khonsulabs.github.io/justjson/main/justjson/doc/type.Document.html
[generic-doc]: https://khonsulabs.github.io/justjson/main/justjson/doc/struct.GenericDocument.html
[heapless-doc]: https://khonsulabs.github.io/justjson/main/justjson/doc/type.HeaplessDocument.html
[json-ld]: https://www.w3.org/TR/json-ld11/#compacted-document-form
[fediverse]: https://en.wikipedia.org/wiki/Fediverse
[activitypub]: https://www.w3.org/TR/activitypub/
[simd-json]: https://github.com/simd-lite/simd-json
[serde-json]: https://github.com/serde-rs/json
[heapless]: https://github.com/japaric/heapless

## Open-source Licenses

Expand Down
15 changes: 9 additions & 6 deletions examples/heapless.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use heapless::Vec;
use justjson::doc::{GenericDocument, Node};
use justjson::doc::{HeaplessDocument, Node};
use justjson::{ErrorKind, JsonString};

fn main() {
// Using a heapless vec, we can parse directly to the stack.
let doc: GenericDocument<Vec<Node<'_>, 3>> =
GenericDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json");
let doc: HeaplessDocument<'_, 3> =
HeaplessDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json");
let mut nodes = doc.into_iter();
assert_eq!(nodes.next(), Some(Node::Object { length: 1 }));
assert_eq!(nodes.next(), Some(Node::String(JsonString::from("hello"))));
assert_eq!(nodes.next(), Some(Node::String(JsonString::from("world"))));

// When parsing a document too large for the heapless Vec, an error will be
// returned instead of panicing.
let error = GenericDocument::<Vec<Node<'_>, 3>>::from_json("[1, 2, 3, 4]")
.expect_err("shouldn't have space");
let error = HeaplessDocument::<3>::from_json("[1, 2, 3, 4]").expect_err("shouldn't have space");
assert_eq!(error.kind(), &ErrorKind::PaylodTooLarge);
}

#[test]
fn runs() {
main();
}
24 changes: 24 additions & 0 deletions src/.crate-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,36 @@ This crate uses unsafe code only when converting from raw incoming data to UTF-8
data. The parser fully verifies that the data is valid UTF-8 before these
functions are used.

## `no_std` support

By default, this crate enables the `std` feature, which adds support for Rust's
standard library types. By disabling default features, this crate can be used in
`no_std` projects. For example, in your Cargo.toml:

```toml
[dependencies]
justjson = { version = "*", default-features = false }
```

The [`Value`][value] type requires the `alloc` feature to be enabled.

The [`Document`][document] type alias requires the `alloc` feature, but the
[`GenericDocument`][generic-doc] type allows providing your own collection type.

With the `heapless` feature enabled, the [`HeaplessDocument`][heapless-doc] type
can be used to parse documents using the [heapless][heapless] crate's `Vec` type
for storage. This enables parsing JSON in environments where the `alloc` crate
isn't supported.

[value]: crate::Value
[string]: crate::JsonString
[number]: crate::JsonNumber
[document]: crate::doc::Document
[generic-doc]: crate::doc::GenericDocument
[heapless-doc]: crate::doc::HeaplessDocument
[json-ld]: https://www.w3.org/TR/json-ld11/#compacted-document-form
[fediverse]: https://en.wikipedia.org/wiki/Fediverse
[activitypub]: https://www.w3.org/TR/activitypub/
[simd-json]: https://github.com/simd-lite/simd-json
[serde-json]: https://github.com/serde-rs/json
[heapless]: https://github.com/japaric/heapless
4 changes: 4 additions & 0 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,7 @@ impl<'a, const N: usize> NodeCollection<'a> for heapless::Vec<Node<'a>, N> {
/// `std`/`alloc`.
#[cfg(feature = "alloc")]
pub type Document<'a> = GenericDocument<'a, Vec<Node<'a>>>;

/// A convenience alias for a [`GenericDocument`] that uses a `heapless::Vec`.
#[cfg(feature = "heapless")]
pub type HeaplessDocument<'a, const N: usize> = GenericDocument<'a, heapless::Vec<Node<'a>, N>>;

0 comments on commit 968b786

Please sign in to comment.