Function bakery::load_from_string[][src]

pub fn load_from_string<T>(dat: &str) -> Result<T, LoadError> where
    T: Recipe + DeserializeOwned
Expand description

Load data from a string, with recipe built using Recipe trait.

Arguments

  • dat - Data string

Example

This example shows how to load a structure from a string:

use bakery::load_from_string;
use bakery_derive::Recipe;
use serde::Deserialize;

#[derive(Recipe, Deserialize, Debug, PartialEq)]
struct GameConfig {
    width: u32,
    height: u32,
    fullscreen: bool
}

let config: GameConfig = load_from_string("width: 1024, height: 768, fullscreen: true").unwrap();
assert_eq!(config, GameConfig { width: 1024, height: 768, fullscreen: true });

This example shows how to load a list from a string. Note that the Recipe trait for Vec is implemented by the library.

use bakery::load_from_string;

let values: Vec<i32> = load_from_string("[1, 2, 3]").unwrap();
assert_eq!(values, vec![1, 2, 3]);