[][src]Trait sml::FromSmall

pub trait FromSmall {
    fn from_small(small: Small) -> Result<Self, SmallError>
    where
        Self: Sized
; fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError>
    where
        Self: Sized
, { ... }
fn from_str(s: &str) -> Result<Self, SmallError>
    where
        Self: Sized
, { ... }
fn from_str_debug(s: &str) -> Self
    where
        Self: Sized
, { ... }
fn to_sml(small: &Small) -> String { ... } }

Data-structures that implement the FromSmall trait can be constructed from a small-formatted string.

Required methods

fn from_small(small: Small) -> Result<Self, SmallError> where
    Self: Sized

The from_small() function describes how to create a data-structure from the internal components of the input String. It maps a selection from the original input String to Self. For example,

use small::{Small, FromSmall, SmallError};
 
#[derive(Debug)]
struct Hobbit {
    name:    String,
    age:     u32,
    friends: Vec<Hobbit>,
    bicycle: Option<String>,
}
 
impl FromSmall for Hobbit {
    fn from_small(small: Small) -> Result<Self, SmallError> {
        Ok(Self {
            name:    String::sml(&small, "hobbit::name")?,
            age:     u32::sml(&small, "hobbit::age")?,
            friends: Vec::<Hobbit>::sml(&small, "hobbit::friends::hobbit")?,
            bicycle: Option::<String>::sml(&small, "hobbit::bicycle")?,
        })
    }
}
 
fn main() {
    let s = r#"
        hobbit:
            name:         "Frodo Baggins"
            age:          "98"
            friends:
                hobbit:
                    name: "Bilbo Baggins"
                    age:  "176"
                hobbit:
                    name: "Samwise Gamgee"
                    age:  "66""#;
     
    let frodo = Hobbit::from_str_debug(s);
}
Loading content...

Provided methods

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized

Applies a keypath to Small and returns a Vec of Smalls. For example applying "hobbit::name" to

        hobbit:
            name:         "Frodo Baggins"
            age:          "98"
            friends:
                hobbit:
                    name: "Bilbo Baggins"
                    age:  "176"
                hobbit:
                    name: "Samwise Gamgee"
                    age:  "66""#;

returns a Vec with one Small element,

            name:         "Frodo Baggins"

Applying "hobbit::name::friends" to the original string returns a Vec with two Small elements which represent

                hobbit:
                    name: "Bilbo Baggins"
                    age:  "176"

and

                hobbit:
                    name: "Samwise Gamgee"
                    age:  "66""#;

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized

Converts a String into Self.

fn from_str_debug(s: &str) -> Self where
    Self: Sized

Converts a String into Self. This function is designed to give helpful error messages for debugging.

fn to_sml(small: &Small) -> String

Loading content...

Implementations on Foreign Types

impl FromSmall for String[src]

Converts a Vec of one Small element to a String.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl FromSmall for u32[src]

Converts a Vec with one Small element to a u32.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl FromSmall for usize[src]

Converts a Vec with one Small element to a usize.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl FromSmall for f32[src]

Converts a Vec with one Small element to a f32.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl FromSmall for bool[src]

Converts a Vec with one Small element to a bool.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl<T> FromSmall for Option<T> where
    T: FromSmall
[src]

Converts a Vec with either one or no Small elements to an Option<T>.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

impl<T> FromSmall for Vec<T> where
    T: FromSmall
[src]

Converts a Vec of Small elements to a Vec<T>.

fn sml(small: &Small, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str(s: &str) -> Result<Self, SmallError> where
    Self: Sized
[src]

fn from_str_debug(s: &str) -> Self where
    Self: Sized
[src]

fn to_sml(small: &Small) -> String[src]

Loading content...

Implementors

Loading content...