preserves/value/text/
mod.rs

1//! Implements the Preserves [human-oriented text
2//! syntax](https://preserves.dev/preserves-text.html).
3//!
4//! The main entry points for reading are functions [iovalue_from_str],
5//! [annotated_iovalue_from_str], [from_str], and [annotated_from_str].
6//!
7//! The main entry points for writing are [TextWriter::encode_iovalue] and
8//! [TextWriter::encode].
9//!
10//! # Summary of Text Syntax
11#![doc = include_str!("../../../doc/cheatsheet-text-plaintext.md")]
12
13pub mod reader;
14pub mod writer;
15
16pub use reader::TextReader;
17pub use reader::ToplevelWhitespaceMode;
18pub use writer::TextWriter;
19
20use crate::value::reader::BytesBinarySource;
21
22use std::io;
23
24use super::{DomainParse, IOValue, IOValueDomainCodec, NestedValue, Reader, ViaCodec};
25
26/// Reads a value from the given string using the text syntax, discarding annotations.
27pub fn from_str<N: NestedValue, Dec: DomainParse<N::Embedded>>(
28    s: &str,
29    decode_embedded: Dec,
30) -> io::Result<N> {
31    TextReader::new(&mut BytesBinarySource::new(s.as_bytes()), decode_embedded).demand_next(false)
32}
33
34/// Reads an [IOValue] from the given string using the text syntax, discarding annotations.
35pub fn iovalue_from_str(s: &str) -> io::Result<IOValue> {
36    from_str(s, ViaCodec::new(IOValueDomainCodec))
37}
38
39/// As [from_str], but includes annotations.
40pub fn annotated_from_str<N: NestedValue, Dec: DomainParse<N::Embedded>>(
41    s: &str,
42    decode_embedded: Dec,
43) -> io::Result<N> {
44    TextReader::new(&mut BytesBinarySource::new(s.as_bytes()), decode_embedded).demand_next(true)
45}
46
47/// As [iovalue_from_str], but includes annotations.
48pub fn annotated_iovalue_from_str(s: &str) -> io::Result<IOValue> {
49    annotated_from_str(s, ViaCodec::new(IOValueDomainCodec))
50}