Skip to main content

sark_json/
lib.rs

1use std::{mem, result};
2
3use o3::{
4    alloc,
5    buffer::{segments, storage, write::consume},
6    mem::rc,
7};
8
9mod bytes;
10pub mod input;
11mod nesting;
12pub mod parsing;
13pub mod segment;
14mod source;
15mod token;
16pub mod writing;
17
18pub use bytes::{
19    Bytes,
20    proofs::{Backing, Plain, PlainError, Raw, Text, TextValue},
21};
22pub(crate) use nesting::Nesting;
23#[doc(hidden)]
24pub use o3;
25/// Derives decoding/encoding on nongeneric structs; `encode`/`try_encode` also accept generics.
26/// `seq, nested` accepts `&Owner: IntoIterator` of owned/borrowed encoders; decoding requires `Vec<T>`.
27/// Exact writes are checked independently of sizing, even if the owner changes between traversals.
28pub use sark_gen::json;
29#[doc(hidden)]
30pub use sark_http;
31pub use source::Source;
32pub use token::Token;
33
34#[derive(Debug)]
35pub enum Error {
36    Invalid,
37    Depth,
38    Token(consume::Error),
39    Storage(Storage),
40    Allocation(alloc::Error),
41    Exhausted(alloc::Exhausted),
42    Retain(rc::Error),
43    InputLength {
44        current: usize,
45        add: usize,
46    },
47    InputSegments {
48        current: segments::Count,
49        add: segments::Count,
50    },
51    OutputLength(consume::Error),
52}
53
54#[derive(Debug)]
55pub enum Storage {
56    Capacity(consume::Error),
57    Exhausted(alloc::Exhausted),
58}
59
60impl From<storage::AllocError> for Error {
61    fn from(error: storage::AllocError) -> Self {
62        Self::Storage(match error {
63            storage::AllocError::Capacity(error) => Storage::Capacity(error),
64            storage::AllocError::Exhausted(error) => Storage::Exhausted(error),
65        })
66    }
67}
68
69type Result<T> = result::Result<T, Error>;
70
71const _: () = assert!(mem::size_of::<Error>() == mem::size_of::<usize>() * 3);