pub trait Parsewhere
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> { ... }
}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§
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>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Implementors§
Source§impl<T: for<'a> Deserialize<'a>> Parse for Json<T>
Available on crate feature json only.
impl<T: for<'a> Deserialize<'a>> Parse for Json<T>
json only.Source§impl<T: for<'a> Deserialize<'a>> Parse for Toml<T>
Available on crate feature toml only.
impl<T: for<'a> Deserialize<'a>> Parse for Toml<T>
toml only.