Skip to main content

Parse

Trait Parse 

Source
pub trait Parse
where Self: Sized,
{ type Output; // Required method fn parse_borrowed(x: &str) -> Result<Self::Output>; // Provided methods fn parse_owned(x: String) -> Result<Self::Output> { ... } fn parse_read(x: impl Read) -> Result<Self::Output> { ... } }
Available on crate feature parse only.
Expand description

The parsing utilities in cu provides wrapper to the std library for parsing common data, such as different radix for numbers, and support for common formats like json, yaml and toml.

Everything only works with UTF-8.

The entry points to parsing are cu::parse::<T>, and "...".parse_to::<T>(). Use whichever is more readable or preferrable at the call sites.

use std::path::PathBuf;
use cu::pre::*;

// bool: empty string, 0, 1, "true", or "false", case-insensitive
let x = cu::parse::<bool>("0")?;
assert_eq!(x, false);

// numbers: hex, oct and binary are supported with 0x, 0b, and 0o prefix
let x = cu::parse::<u32>("0")?;
assert_eq!(x, 0);
let x = cu::parse::<i32>("-123")?;
assert_eq!(x, -123);
let x = cu::parse::<usize>("0x123")?;
assert_eq!(x, 0x123);

// .parse_to() can be used when chaining,
// instead of wrapping in cu::parse or use a temporary variable.
let _x = "imagine some long chaining is used to get this value"
    .parse_to::<PathBuf>()?;

If the value is owned, you can use parse_owned instead of parse. For readers, use cu::read or cu::co_read for async readers.

§From Owned and Read

When possible, implementation of parsing from a io::Read is provided for optimization (for example for json inputs using serde_json::from_reader).

Async read is also supported, however, at the time of writing, the inputs are first read into a buffer asynchronously, then parsed synchronously from the buffer.

§Remote Derive

Common types are implemented in this library. You can derive this trait by using the Parse derive macro for any type that implements FromStr.

§Common Formats

When json/yaml/toml features are enabled, the respective module will be included in the prelude. See json/yaml/toml for more details.

Required Associated Types§

Required Methods§

Provided Methods§

Source

fn parse_owned(x: String) -> Result<Self::Output>

Source

fn parse_read(x: impl Read) -> Result<Self::Output>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Parse for PathBuf

Source§

impl Parse for String

Source§

impl Parse for bool

Source§

impl Parse for f32

Source§

impl Parse for f64

Source§

impl Parse for i8

Source§

impl Parse for i16

Source§

impl Parse for i32

Source§

impl Parse for i64

Source§

impl Parse for i128

Source§

impl Parse for isize

Source§

impl Parse for u8

Source§

impl Parse for u16

Source§

impl Parse for u32

Source§

impl Parse for u64

Source§

impl Parse for u128

Source§

impl Parse for usize

Implementors§

Source§

impl<T: for<'a> Deserialize<'a>> Parse for Json<T>

Available on crate feature json only.
Source§

impl<T: for<'a> Deserialize<'a>> Parse for Toml<T>

Available on crate feature toml only.
Source§

impl<T: for<'a> Deserialize<'a>> Parse for Yaml<T>

Available on crate feature yaml only.