Compare commits
2 Commits
b9d5aeec2d
...
a8e97b7745
Author | SHA1 | Date |
---|---|---|
Dmitriy Pleshevskiy | a8e97b7745 | |
Dmitriy Pleshevskiy | dff33d2b46 |
27
src/core.rs
27
src/core.rs
|
@ -11,7 +11,7 @@ pub trait FormatFragment {
|
|||
|
||||
/// Parse a value fragment from a ``EString``.
|
||||
///
|
||||
/// ``ParseFragment``’s `parse_frag` method is often used imlicitly, through ``EString``’s parse.
|
||||
/// ``ParseFragment``’s `parse_frag` method is often used implicitly, through ``EString``’s parse.
|
||||
/// See [parse](EString::parse)’s documentation for examples.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -75,12 +75,33 @@ pub trait ParseFragment: Sized {
|
|||
pub struct EString(pub String);
|
||||
|
||||
impl EString {
|
||||
/// Parses inner string by type annotations and returns result.
|
||||
/// Parses this inner string into another type.
|
||||
///
|
||||
/// `parse` can parse into any type that implements the ``ParseFragment`` trait.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return `Err` if estring cannot parse inner fragment
|
||||
/// Will return `Err` if estring cannot parse inner fragment into the desired type.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage
|
||||
///
|
||||
/// ```rust
|
||||
/// # use estring::{EString, ParseFragment};
|
||||
/// let fragment = EString::from("5");
|
||||
/// let res = i32::parse_frag(fragment);
|
||||
/// assert_eq!(res, Ok(5));
|
||||
/// ```
|
||||
///
|
||||
/// Failing to parse:
|
||||
///
|
||||
/// ```rust
|
||||
/// # use estring::{EString, ParseFragment, Error, Reason};
|
||||
/// let fragment = EString::from("j");
|
||||
/// let res = i32::parse_frag(fragment.clone());
|
||||
/// assert_eq!(res, Err(Error(fragment, Reason::Parse)));
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn parse<T: ParseFragment>(self) -> crate::Result<T> {
|
||||
T::parse_frag(self)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::core::EString;
|
||||
|
||||
/// The error type for operations interacting with ``EString``’s fragments.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Error(pub EString, pub Reason);
|
||||
|
||||
/// The reason for the failure to parse.
|
||||
|
|
|
@ -63,9 +63,10 @@ where
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
type EqTrio<A, B, C> = Trio<A, '=', B, '=', C>;
|
||||
|
||||
#[test]
|
||||
fn should_parse_into_trio() {
|
||||
type EqTrio<A, B, C> = Trio<A, '=', B, '=', C>;
|
||||
let estr = EString::from("hello=world=hello");
|
||||
match estr.parse::<EqTrio<&str, &str, &str>>() {
|
||||
Ok(res) => assert_eq!((res.0, res.1, res.2), ("hello", "world", "hello")),
|
||||
|
@ -75,9 +76,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn should_parse_into_trio_with_alternate_delims() {
|
||||
type EqTrio<A, B, C> = Trio<A, '-', B, '^', C>;
|
||||
let estr = EString::from("hello-world^hello");
|
||||
match estr.parse::<EqTrio<&str, &str, &str>>() {
|
||||
match estr.parse::<Trio<&str, '-', &str, '^', &str>>() {
|
||||
Ok(res) => assert_eq!((res.0, res.1, res.2), ("hello", "world", "hello")),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
@ -85,7 +85,6 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn should_parse_rest_as_trio() {
|
||||
type EqTrio<A, B, C> = Trio<A, '=', B, '=', C>;
|
||||
let estr = EString::from("hello=world=hello=world=hello");
|
||||
match estr.parse::<EqTrio<&str, &str, EqTrio<&str, &str, &str>>>() {
|
||||
Ok(res) => assert_eq!(res, Trio("hello", "world", Trio("hello", "world", "hello"))),
|
||||
|
|
Loading…
Reference in New Issue