[][src]Trait sml::Small

pub trait Small {
    fn from_data(data: Data) -> Result<Self, SmallError>
    where
        Self: Sized
; fn sml(data: &Data, 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
, { ... } }

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

Required methods

fn from_data(data: Data) -> Result<Self, SmallError> where
    Self: Sized

The from_data() 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::{Data, Small, SmallError};
 
#[derive(Debug)]
struct Hobbit {
    name:    String,
    age:     u32,
    friends: Vec<Hobbit>,
    bicycle: Option<String>,
}
 
impl Small for Hobbit {
    fn from_data(data: Data) -> Result<Self, SmallError> {
        Ok(Self {
            name:    String::sml(&data, "hobbit::name")?,
            age:     u32::sml(&data, "hobbit::age")?,
            friends: Vec::<Hobbit>::sml(&data, "hobbit::friends::hobbit")?,
            bicycle: Option::<String>::sml(&data, "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(data: &Data, key_path: &str) -> Result<Self, SmallError> where
    Self: Sized

Applies a keypath to Data and returns a Vec of Data. 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 Data element,

            name:         "Frodo Baggins"

Applying "hobbit::name::friends" to the original string returns a Vec with two Data 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.

Loading content...

Implementations on Foreign Types

impl Small for String[src]

Converts a Vec of one Data element to a String.

fn sml(data: &Data, 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]

impl Small for u32[src]

Converts a Vec with one Data element to a u32.

fn sml(data: &Data, 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]

impl Small for usize[src]

Converts a Vec with one Data element to a usize.

fn sml(data: &Data, 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]

impl Small for f32[src]

Converts a Vec with one Data element to a f32.

fn sml(data: &Data, 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]

impl Small for bool[src]

Converts a Vec with one Data element to a bool.

fn sml(data: &Data, 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]

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

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

fn sml(data: &Data, 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]

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

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

fn sml(data: &Data, 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]

Loading content...

Implementors

Loading content...