[][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

Takes a String in the small data format to a data-structure. Data is a partial compilation of the the small data-formatted string. You don't need to know anything about Data other than that it is a argument to the sml() function.

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

If stepping through the key path results in a key: value pair (usually representing a field) then the value is passed to the from_data() functon as a String. If stepping through the key path resulting in a key: only then a String of all the children (usually representing a data-structure) of that key is returned.

Takes a partial compilation of a small data-formatted string, and a series of keys stepping into the data format, and returns a data structure or a value. See the main example for a better understanding.

If you need something more complex than a u32, String etc. you can either implement this using from_data() or simply convert from one of the existing conversions, for example,

let first_name = String::sml(&data, "hobbit::name")?.first_word();
let last_name = String::sml(&data, "hobbit::name")?.last_word();

The from_data() function is the same as the sml() function except that it doesn't do any transformation by stepping through a key path, so its main role is to convert from Data to some other type.

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

Converts a Small markup into a data-structure.

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

Converts a small data-formatted string into a data-structure. This function is designed to give helpful error messages for debugging and then to exit. See the main example for a better understanding.

Loading content...

Implementations on Foreign Types

impl Small for String[src]

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]

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]

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]

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]

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]

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...