preserves/implementations/rust/preserves/src/value/text/mod.rs

51 lines
1.7 KiB
Rust
Raw Normal View History

2023-10-26 22:46:27 +00:00
//! Implements the Preserves [human-oriented text
//! syntax](https://preserves.dev/preserves-text.html).
//!
2023-10-27 09:40:55 +00:00
//! The main entry points for reading are functions [iovalue_from_str],
//! [annotated_iovalue_from_str], [from_str], and [annotated_from_str].
//!
//! The main entry points for writing are [TextWriter::encode_iovalue] and
//! [TextWriter::encode].
//!
2023-10-26 22:46:27 +00:00
//! # Summary of Text Syntax
#![doc = include_str!("../../../doc/cheatsheet-text-plaintext.md")]
2021-08-02 09:42:48 +00:00
pub mod reader;
pub mod writer;
pub use reader::TextReader;
pub use reader::ToplevelWhitespaceMode;
2021-08-02 09:42:48 +00:00
pub use writer::TextWriter;
use crate::value::reader::BytesBinarySource;
2021-08-02 09:42:48 +00:00
use std::io;
use super::{DomainParse, IOValue, IOValueDomainCodec, NestedValue, Reader, ViaCodec};
2021-08-02 09:42:48 +00:00
2023-10-26 22:46:27 +00:00
/// Reads a value from the given string using the text syntax, discarding annotations.
pub fn from_str<N: NestedValue, Dec: DomainParse<N::Embedded>>(
2021-08-02 09:42:48 +00:00
s: &str,
decode_embedded: Dec,
) -> io::Result<N> {
TextReader::new(&mut BytesBinarySource::new(s.as_bytes()), decode_embedded).demand_next(false)
2021-08-02 09:42:48 +00:00
}
2023-10-26 22:46:27 +00:00
/// Reads an [IOValue] from the given string using the text syntax, discarding annotations.
2021-08-02 09:42:48 +00:00
pub fn iovalue_from_str(s: &str) -> io::Result<IOValue> {
from_str(s, ViaCodec::new(IOValueDomainCodec))
}
2023-10-26 22:46:27 +00:00
/// As [from_str], but includes annotations.
pub fn annotated_from_str<N: NestedValue, Dec: DomainParse<N::Embedded>>(
2021-08-02 09:42:48 +00:00
s: &str,
decode_embedded: Dec,
) -> io::Result<N> {
TextReader::new(&mut BytesBinarySource::new(s.as_bytes()), decode_embedded).demand_next(true)
2021-08-02 09:42:48 +00:00
}
2023-10-26 22:46:27 +00:00
/// As [iovalue_from_str], but includes annotations.
2021-08-02 09:42:48 +00:00
pub fn annotated_iovalue_from_str(s: &str) -> io::Result<IOValue> {
annotated_from_str(s, ViaCodec::new(IOValueDomainCodec))
}