Skip to main content

Crate raoh

Crate raoh 

Source
Expand description

Raoh turns untyped boundary input into typed domain values.

It follows parse, don’t validate: a domain value is built only once everything it is built from has been read, so a value that exists is a valid one. What was wrong with the input comes back as Issues, every one of them with the JSON Pointer of where it was found, rather than the first.

Serde reads JSON text into a serde_json::Value; Raoh reads that value into the domain.

use raoh::json::prelude::*;

#[derive(Debug)]
struct Email(String);
#[derive(Debug)]
struct User { email: Email, age: u32 }

fn email() -> impl Decoder<Value, Output = Email> {
    string().trim().lowercase().email().map(Email)
}

fn user() -> impl Decoder<Value, Output = User> {
    object((
        field("email", email()),
        field("age", u32().range(0..=150)),
    ))
    .map(|(email, age)| User { email, age })
}

let issues = user().decode(&json!({"email": "nope", "age": 200})).unwrap_err();
let paths: Vec<String> = issues.iter().map(|i| i.path().to_string()).collect();
assert_eq!(paths, ["/email", "/age"]);

Independent parts are combined with a tuple, which reports the issues of every part; Decoder::and_then checks what depends on several parts together, once they have all been read.

Re-exports§

pub use combinator::lazy;
pub use combinator::one_of;

Modules§

codes
What kind of problem an issue reports.
combinator
The adapters Decoder’s methods return, and the decoders that combine several.
json
Decoders over a serde_json::Value.
message_keys
Keys that name which check produced an issue, where one code covers several.

Structs§

FnDecoder
The decoder decoder_fn returns.
Issue
One problem found in the input: where it is, what kind it is, and what else its code says.
Issues
Every issue a decode found, in the order it found them.
Messages
A catalogue of message templates keyed by message key or code, over the catalogue it falls back to.
Path
The position a decoder is reading at, as a list borrowed from the caller’s stack.
Pointer
An owned path, written as a JSON Pointer (RFC 6901) when displayed.
PropertiesError
Where Messages::from_properties stopped reading.

Enums§

Presence
Whether a member was absent, present as null, or present with a value.
Segment
One step below a parent: an object key or an array index.

Traits§

Decoder
Reads an input of type I into a value, or reports every issue it found.
MessageResolver
Writes the sentence for an issue that has no custom message.

Functions§

decoder_fn
A decoder from a function, for a one-off decoder not worth a type of its own.

Type Aliases§

BoxDecoder
A decoder whose concrete type is erased.